Skip to content

Commit 99da812

Browse files
committed
Improved Form Validation Docs
Added info about adding constraints to each field as opposed to only using DefaultOptions. Also added a tip about settings validation groups.
1 parent 2c2d4ff commit 99da812

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

book/forms.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,36 @@ 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+
::
1590+
use Symfony\Component\Validator\Constraints\MinLength;
1591+
use Symfony\Component\Validator\Constraints\NotBlank;
1592+
1593+
$builder
1594+
->add('firstName', 'text', array(
1595+
'constraints' => new MinLength(3),
1596+
))
1597+
->add('lastName', 'text', array(
1598+
'constraints' => array(
1599+
new NotBlank(),
1600+
new MinLength(3),
1601+
),
1602+
))
1603+
;
1604+
1605+
.. tip::
1606+
If you are using Validation Groups, you need to either reference the
1607+
``Default`` group when creating the form, or set the correct group on
1608+
the constraint you are adding.
1609+
1610+
::
1611+
new NotBlank(array('groups' => array('create', 'update'))
1612+
1613+
15841614
Final Thoughts
15851615
--------------
15861616

0 commit comments

Comments
 (0)