Skip to content

Commit 70cbf1f

Browse files
committed
[book][validation] Updating the validation chapter and adding information about validation groups.
Closes #325
1 parent 6af56cc commit 70cbf1f

File tree

2 files changed

+213
-125
lines changed

2 files changed

+213
-125
lines changed

book/forms.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,31 @@ corresponding errors printed out with the form.
348348
Validation is a very powerful feature of Symfony2 and has its own
349349
:doc:`dedicated chapter</book/validation>`.
350350

351+
.. _book-forms-validation-groups:
352+
353+
Validation Groups
354+
~~~~~~~~~~~~~~~~~
355+
356+
If your object takes advantage of :ref:`validation groups <book-validation-validation-groups>`,
357+
you'll need to specify which validation group(s) your form should use::
358+
359+
$form = $this->createFormBuilder($user)
360+
->setAttribute('validation_groups', array('registration'))
361+
// ...
362+
;
363+
364+
If you're creating :ref:`form classes<book-form-creating-form-classes>` (a good
365+
practice), then you'll need to add the following to the ``buildForm()`` method::
366+
367+
public function buildForm(FormBuilder $builder, array $options)
368+
{
369+
// Restrict the constraints to the 'registration' group
370+
$builder->setAttribute('validation_groups', array('registration'));
371+
}
372+
373+
In both of these cases, *only* the ``registration`` validation group will
374+
be used to validate the underlying object.
375+
351376
.. index::
352377
single: Forms; Built-in Field Types
353378

@@ -591,6 +616,8 @@ available in the :doc:`reference manual</reference/forms/twig_reference>`.
591616
.. index::
592617
single: Forms; Creating form classes
593618

619+
.. _book-form-creating-form-classes:
620+
594621
Creating Form Classes
595622
---------------------
596623

0 commit comments

Comments
 (0)