-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added callable validation_groups example
Recently I had a need to dynamically set the validation_groups of a small set of forms based on the user that was logged in. In digging about the FormValidator::resolveValidationGroups and saw I could use a class that implements the __invoke method. Figured I might as well write something about it and see if you'd want to add it to the docs.
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -6,3 +6,4 @@ Validation | |
|
||
custom_constraint | ||
severity | ||
validation-group-service-resolver |
This file contains 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,71 @@ | ||
How to Dynamically Configure Validation Groups | ||
============================================== | ||
|
||
Sometimes you need advanced logic to determine the validation groups. If this | ||
can't be determined by a simple callback, you can use a service. Create a | ||
service that implements ``__invoke`` which accepts a ``FormInterface`` as a | ||
parameter. | ||
|
||
.. code-block:: php | ||
namespace AppBundle\Validation; | ||
use Symfony\Component\Form\FormInterface; | ||
class ValidationGroupResolver | ||
{ | ||
private $service1; | ||
private $service2; | ||
public function __construct($service1, $service2) | ||
{ | ||
$this->service1 = $service1; | ||
$this->service2 = $service2; | ||
} | ||
/** | ||
* @param FormInterface $form | ||
* @return array | ||
*/ | ||
public function __invoke(FormInterface $form) | ||
{ | ||
$groups = array(); | ||
// ... determine which groups to apply and return an array | ||
return $groups; | ||
} | ||
} | ||
Then in your form, inject the resolver and set it as the ``validation_groups``. | ||
|
||
.. code-block:: php | ||
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | ||
use Symfony\Component\Form\AbstractType | ||
use AppBundle\Validator\ValidationGroupResolver; | ||
class MyClassType extends AbstractType | ||
{ | ||
/** | ||
* @var ValidationGroupResolver | ||
*/ | ||
private $groupResolver; | ||
public function __construct(ValidationGroupResolver $groupResolver) | ||
{ | ||
$this->groupResolver = $groupResolver; | ||
} | ||
// ... | ||
public function setDefaultOptions(OptionsResolverInterface $resolver) | ||
{ | ||
$resolver->setDefaults(array( | ||
'validation_groups' => $this->groupResolver, | ||
)); | ||
} | ||
} | ||
This will result in the form validator invoking your group resolver to set the | ||
validation groups returned when validating. |