Skip to content

Commit e6a6f9a

Browse files
author
naitsirch
committed
Added description for the "validation_groups" option
As requested in #4401 a description of the "validation_groups" option of the base ``FormType`` is added.
1 parent 3b241b1 commit e6a6f9a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

reference/forms/types/form.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ on all types for which ``FormType`` is the parent.
3434
| | - `read_only`_ (deprecated as of 2.8) |
3535
| | - `required`_ |
3636
| | - `trim`_ |
37+
| | - `validation_groups`_ |
3738
+-----------+--------------------------------------------------------------------+
3839
| Inherited | - `attr`_ |
3940
| options | - `auto_initialize`_ |
@@ -146,6 +147,8 @@ The actual default value of this option depends on other field options:
146147

147148
.. include:: /reference/forms/types/options/trim.rst.inc
148149

150+
.. include:: /reference/forms/types/options/validation_groups.rst.inc
151+
149152
Inherited Options
150153
-----------------
151154

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
validation_groups
2+
~~~~~~~~~~~~~~~~~
3+
4+
**type**: ``array``, ``string``, ``callable`` 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 array or string they will used by the validator
12+
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+
If the validation groups depend on the form's data a callable may be passed to
26+
the option. Symfony then will pass the form when calling it::
27+
28+
use Symfony\Component\Form\FormInterface;
29+
30+
// ...
31+
public function configureOptions(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefaults(array(
34+
'validation_groups' => function (FormInterface $form) {
35+
$entity = $form->getData();
36+
return $entity->getValidationGroups();
37+
},
38+
));
39+
}
40+
41+
The class that the form is used for may look like this::
42+
43+
class Account
44+
{
45+
public function getValidationGroups()
46+
{
47+
return $this->isUser() ? array('User') : array('Company');
48+
}
49+
}
50+
51+
You can read more about this in :doc:`/form/data_based_validation`.

0 commit comments

Comments
 (0)