Skip to content

Commit ca48e3d

Browse files
committed
[#2058] Refactoring how to add validation to a form, using the constraints option on individual forms, which is much easier than adding things globally, as we did in 2.0
1 parent bc223ac commit ca48e3d

File tree

3 files changed

+24
-61
lines changed

3 files changed

+24
-61
lines changed

book/forms.rst

Lines changed: 13 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,72 +1520,24 @@ Adding Validation
15201520

15211521
The only missing piece is validation. Usually, when you call ``$form->isValid()``,
15221522
the object is validated by reading the constraints that you applied to that
1523-
class. But without a class, how can you add constraints to the data of your
1524-
form?
1525-
1526-
The answer is to setup the constraints yourself, and pass them into your
1527-
form. The overall approach is covered a bit more in the :ref:`validation chapter<book-validation-raw-values>`,
1528-
but here's a short example::
1529-
1530-
// import the namespaces above your controller class
1531-
use Symfony\Component\Validator\Constraints\Email;
1532-
use Symfony\Component\Validator\Constraints\Length;
1533-
use Symfony\Component\Validator\Constraints\Collection;
1534-
1535-
$collectionConstraint = new Collection(array(
1536-
'name' => new Length(array("min" => 5)),
1537-
'email' => new Email(array('message' => 'Invalid email address')),
1538-
));
1539-
1540-
// create a form, no default values, pass in the constraint option
1541-
$form = $this->createFormBuilder(null, array(
1542-
'constraints' => $collectionConstraint,
1543-
))->add('email', 'email')
1544-
// ...
1545-
;
1546-
1547-
Now, when you call `$form->bind($request)`, the constraints setup here are run
1548-
against your form's data. If you're using a form class, override the ``setDefaultOptions()``
1549-
method to specify the option::
1550-
1551-
namespace Acme\TaskBundle\Form\Type;
1552-
1553-
use Symfony\Component\Form\AbstractType;
1554-
use Symfony\Component\Form\FormBuilder;
1555-
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1556-
use Symfony\Component\Validator\Constraints\Email;
1557-
use Symfony\Component\Validator\Constraints\Length;
1558-
use Symfony\Component\Validator\Constraints\Collection;
1559-
1560-
class ContactType extends AbstractType
1561-
{
1562-
// ...
1523+
class. If your form is binding to an object (i.e. you're using the ``data_class``
1524+
option or passing an object to your form), this is almost always the approach
1525+
you want to use. See :doc:`/book/validation` for more details.
15631526

1564-
public function setDefaultOptions(OptionsResolverInterface $resolver)
1565-
{
1566-
$collectionConstraint = new Collection(array(
1567-
'name' => new Length(array("min" => 5)),
1568-
'email' => new Email(
1569-
array('message' => 'Invalid email address')
1570-
),
1571-
));
1527+
.. _form-option-constraints:
15721528

1573-
$resolver->setDefaults(array(
1574-
'constraints' => $collectionConstraint
1575-
));
1576-
}
1577-
}
1529+
But if you're not binding to an object and are instead retrieving a simple
1530+
array of your submitted data, how can you add constraints to the data of your
1531+
form?
15781532

1579-
Now, you have the flexibility to create forms - with validation - that return
1580-
an array of data, instead of an object. In most cases, it's better - and
1581-
certainly more robust - to bind your form to an object. But for simple forms,
1582-
this is a great approach.
1533+
The answer is to setup the constraints yourself, and attach them to the individual
1534+
fields. The overall approach is covered a bit more in the :ref:`validation chapter<book-validation-raw-values>`,
1535+
but here's a short example:
15831536

15841537
.. versionadded:: 2.1
1585-
Constraints can be added directly to fields using the ``constraints`` option,
1586-
which accepts a single constraint or an array of constraints (before 2.1,
1587-
the option was called ``validation_constraint``, and only accepted a single
1588-
constraint).
1538+
The ``constraints`` option, which accepts a single constraint or an array
1539+
of constraints (before 2.1, the option was called ``validation_constraint``,
1540+
and only accepted a single constraint) is new to Symfony 2.1.
15891541

15901542
.. code-block:: php
15911543

reference/forms/types/form.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ on all fields.
1313

1414
.. include:: /reference/forms/types/options/required.rst.inc
1515

16+
.. include:: /reference/forms/types/options/constraints.rst.inc
17+
1618
.. include:: /reference/forms/types/options/cascade_validation.rst.inc
1719

1820
.. include:: /reference/forms/types/options/disabled.rst.inc
1921

22+
.. include:: /reference/forms/types/options/disabled.rst.inc
23+
2024
.. include:: /reference/forms/types/options/trim.rst.inc
2125

2226
.. include:: /reference/forms/types/options/mapped.rst.inc
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
constraints
2+
~~~~~~~~~~~
3+
4+
**type**: array or :class:`Symfony\\Component\\Validator\\Constraint` **default**: ``null``
5+
6+
Allows you to attach one or more validation constraints to a specific field.
7+
For more information, see :ref:`Adding Validation<form-option-constraints>`.

0 commit comments

Comments
 (0)