Skip to content

Commit 11de7ca

Browse files
committed
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.
1 parent 0fb7847 commit 11de7ca

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

book/forms.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,11 @@ Now the form will skip your validation constraints. It will still validate
616616
basic integrity constraints, such as checking whether an uploaded file was too
617617
large or whether you tried to submit text in a number field.
618618

619+
.. seealso::
620+
621+
To see how to use a service with dependencies to resolve ``validation_groups``
622+
checkout cookbook article :doc:`/cookbook/validation/validation-groups-service-resolver`.
623+
619624
.. index::
620625
single: Forms; Built-in field types
621626

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@
249249

250250
* :doc:`/cookbook/validation/custom_constraint`
251251
* :doc:`/cookbook/validation/severity`
252+
* :doc:`/cookbook/validation/validation-group-service-resolver`
252253

253254
* :doc:`/cookbook/web_server/index`
254255

cookbook/validation/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Validation
66

77
custom_constraint
88
severity
9+
validation-group-service-resolver
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
How to Dynamically Configure Validation Groups
2+
==============================================
3+
4+
Sometimes you need advanced logic to determine the validation groups. If this
5+
can't be determined by a simple callback, you can use a service. Create a
6+
service that implements ``__invoke`` which accepts a ``FormInterface`` as a
7+
parameter.
8+
9+
.. code-block:: php
10+
11+
namespace AppBundle\Validation;
12+
13+
use Symfony\Component\Form\FormInterface;
14+
15+
class ValidationGroupResolver
16+
{
17+
private $service1;
18+
19+
private $service2;
20+
21+
public function __construct($service1, $service2)
22+
{
23+
$this->service1 = $service1;
24+
$this->service2 = $service2;
25+
}
26+
27+
/**
28+
* @param FormInterface $form
29+
* @return array
30+
*/
31+
public function __invoke(FormInterface $form)
32+
{
33+
$groups = array();
34+
35+
// ... determine which groups to apply and return an array
36+
37+
return $groups;
38+
}
39+
}
40+
41+
Then in your form, inject the resolver and set it as the ``validation_groups``.
42+
43+
.. code-block:: php
44+
45+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
46+
use Symfony\Component\Form\AbstractType
47+
use AppBundle\Validator\ValidationGroupResolver;
48+
49+
class MyClassType extends AbstractType
50+
{
51+
/**
52+
* @var ValidationGroupResolver
53+
*/
54+
private $groupResolver;
55+
56+
public function __construct(ValidationGroupResolver $groupResolver)
57+
{
58+
$this->groupResolver = $groupResolver;
59+
}
60+
61+
// ...
62+
public function setDefaultOptions(OptionsResolverInterface $resolver)
63+
{
64+
$resolver->setDefaults(array(
65+
'validation_groups' => $this->groupResolver,
66+
));
67+
}
68+
}
69+
70+
This will result in the form validator invoking your group resolver to
71+
set the validation groups returned when validating.

0 commit comments

Comments
 (0)