|
| 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