Skip to content

Commit 8aeb9cf

Browse files
committed
Merge pull request #2058 from rdohms/form-validation-update
Improved Form Validation Docs
2 parents 9716567 + 7c7a8e7 commit 8aeb9cf

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

book/forms.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,39 @@ an array of data, instead of an object. In most cases, it's better - and
15811581
certainly more robust - to bind your form to an object. But for simple forms,
15821582
this is a great approach.
15831583

1584+
.. versionadded:: 2.1
1585+
It is now possible to add constraints directly to the fields using the
1586+
``constraints`` option, which accepts a single constraint or an array
1587+
of them.
1588+
1589+
.. code-block:: php
1590+
1591+
use Symfony\Component\Validator\Constraints\MinLength;
1592+
use Symfony\Component\Validator\Constraints\NotBlank;
1593+
1594+
$builder
1595+
->add('firstName', 'text', array(
1596+
'constraints' => new MinLength(3),
1597+
))
1598+
->add('lastName', 'text', array(
1599+
'constraints' => array(
1600+
new NotBlank(),
1601+
new MinLength(3),
1602+
),
1603+
))
1604+
;
1605+
1606+
.. tip::
1607+
1608+
If you are using Validation Groups, you need to either reference the
1609+
``Default`` group when creating the form, or set the correct group on
1610+
the constraint you are adding.
1611+
1612+
.. code-block:: php
1613+
1614+
new NotBlank(array('groups' => array('create', 'update'))
1615+
1616+
15841617
Final Thoughts
15851618
--------------
15861619

0 commit comments

Comments
 (0)