@@ -1520,72 +1520,24 @@ Adding Validation
1520
1520
1521
1521
The only missing piece is validation. Usually, when you call ``$form->isValid() ``,
1522
1522
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.
1563
1526
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 :
1572
1528
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?
1578
1532
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:
1583
1536
1584
1537
.. 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.
1589
1541
1590
1542
.. code-block :: php
1591
1543
0 commit comments