Skip to content

Commit 8abdfc7

Browse files
committed
minor symfony#10514 Added description for the "validation_groups" option (naitsirch)
This PR was submitted for the 2.8 branch but it was squashed and merged into the 3.4 branch instead (closes symfony#10514). Discussion ---------- Added description for the "validation_groups" option As requested in symfony#4401 a description of the "validation_groups" option of the base ``FormType`` has been added. Commits ------- 7d3fe72 Added description for the "validation_groups" option
2 parents b02c3c7 + 7d3fe72 commit 8abdfc7

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

reference/forms/types/form.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ on all types for which ``FormType`` is the parent.
3030
| | - `property_path`_ |
3131
| | - `required`_ |
3232
| | - `trim`_ |
33+
| | - `validation_groups`_ |
3334
+-----------+--------------------------------------------------------------------+
3435
| Inherited | - `attr`_ |
3536
| options | - `auto_initialize`_ |
@@ -130,6 +131,8 @@ The actual default value of this option depends on other field options:
130131

131132
.. include:: /reference/forms/types/options/trim.rst.inc
132133

134+
.. include:: /reference/forms/types/options/validation_groups.rst.inc
135+
133136
Inherited Options
134137
-----------------
135138

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
validation_groups
2+
~~~~~~~~~~~~~~~~~
3+
4+
**type**: ``array``, ``string``, ``callable``, :class:`Symfony\\Component\\Validator\\Constraints\\GroupSequence` or ``null`` **default**: ``null``
5+
6+
This option is only valid on the root form and is used to specify which
7+
groups will be used by the validator.
8+
9+
For ``null`` the validator will just use the ``Default`` group.
10+
11+
If you specify the groups as an array or string they will be used by the
12+
validator as they are::
13+
14+
public function configureOptions(OptionsResolver $resolver)
15+
{
16+
$resolver->setDefaults(array(
17+
'validation_groups' => 'Registration',
18+
));
19+
}
20+
21+
This is equivalent to passing the group as array::
22+
23+
'validation_groups' => array('Registration'),
24+
25+
The form's data will be :doc:`validated against all given groups </form/validation_groups>`.
26+
27+
If the validation groups depend on the form's data a callable may be passed to
28+
the option. Symfony will then pass the form when calling it::
29+
30+
use Symfony\Component\Form\FormInterface;
31+
use Symfony\Component\OptionsResolver\OptionsResolver;
32+
33+
// ...
34+
public function configureOptions(OptionsResolver $resolver)
35+
{
36+
$resolver->setDefaults(array(
37+
'validation_groups' => function (FormInterface $form) {
38+
$entity = $form->getData();
39+
40+
return $entity->isUser() ? array('User') : array('Company');
41+
},
42+
));
43+
}
44+
45+
.. seealso::
46+
47+
You can read more about this in :doc:`/form/data_based_validation`.
48+
49+
.. note::
50+
51+
When your form contains multiple submit buttons, you can change the
52+
validation group depending on :doc:`which button is used</form/button_based_validation>`
53+
to submit the form.
54+
55+
If you need advanced logic to determine the validation groups have
56+
a look at :doc:`/form/validation_group_service_resolver`.
57+
58+
In some cases, you want to validate your groups step by step. To do this, you
59+
can pass a :class:`Symfony\\Component\\Validator\\Constraints\\GroupSequence`
60+
to this option. This enables you to validate against multiple groups,
61+
like when you pass multiple groups in an array, but with the difference that
62+
a group is only validated if the previous groups pass without errors.
63+
Here's an example::
64+
65+
use Symfony\Component\Validator\Constraints\GroupSequence;
66+
use Symfony\Component\Form\AbstractType;
67+
// ...
68+
69+
class MyType extends AbstractType
70+
{
71+
// ...
72+
public function configureOptions(OptionsResolver $resolver)
73+
{
74+
$resolver->setDefaults([
75+
'validation_groups' => new GroupSequence(['First', 'Second']),
76+
]);
77+
}
78+
}
79+
80+
.. seealso::
81+
82+
Read the article :doc:`/validation/sequence_provider` to find out more about this.

0 commit comments

Comments
 (0)