-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Added description for the "validation_groups" option #10514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
validation_groups | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
**type**: ``array``, ``string``, ``callable``, :class:`Symfony\\Component\\Validator\\Constraints\\GroupSequence` or ``null`` **default**: ``null`` | ||
|
||
This option is only valid on the root form and is used to specify which | ||
groups will be used by the validator. | ||
|
||
For ``null`` the validator will just use the ``Default`` group. | ||
|
||
If you specify the groups as an array or string they will be used by the | ||
validator as they are:: | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults(array( | ||
'validation_groups' => 'Registration', | ||
)); | ||
} | ||
|
||
This is equivalent to passing the group as array:: | ||
|
||
'validation_groups' => array('Registration'), | ||
|
||
The form's data will be :doc:`validated against all given groups </form/validation_groups>`. | ||
|
||
If the validation groups depend on the form's data a callable may be passed to | ||
the option. Symfony will then pass the form when calling it:: | ||
|
||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
// ... | ||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults(array( | ||
'validation_groups' => function (FormInterface $form) { | ||
$entity = $form->getData(); | ||
|
||
return $entity->isUser() ? array('User') : array('Company'); | ||
}, | ||
)); | ||
} | ||
|
||
.. seealso:: | ||
|
||
You can read more about this in :doc:`/form/data_based_validation`. | ||
|
||
.. note:: | ||
|
||
When your form contains multiple submit buttons, you can change the | ||
validation group depending on :doc:`which button is used</form/button_based_validation>` | ||
to submit the form. | ||
|
||
If you need advanced logic to determine the validation groups have | ||
a look at :doc:`/form/validation_group_service_resolver`. | ||
|
||
In some cases, you want to validate your groups step by step. To do this, you | ||
can pass a :class:`Symfony\\Component\\Validator\\Constraints\\GroupSequence` | ||
to this option. This enables you to validate against multiple groups, | ||
like when you pass multiple groups in an array, but with the difference that | ||
a group is only validated if the previous groups pass without errors. | ||
Here's an example:: | ||
|
||
use Symfony\Component\Validator\Constraints\GroupSequence; | ||
use Symfony\Component\Form\AbstractType; | ||
// ... | ||
|
||
class MyType extends AbstractType | ||
{ | ||
// ... | ||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults([ | ||
'validation_groups' => new GroupSequence(['First', 'Second']), | ||
]); | ||
} | ||
} | ||
|
||
.. seealso:: | ||
|
||
Read the article :doc:`/validation/sequence_provider` to find out more about this. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.