Skip to content

Commit 6a96a0c

Browse files
gnat42xabbuh
authored andcommitted
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 e7bc0fa commit 6a96a0c

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
@@ -635,6 +635,11 @@ Now the form will skip your validation constraints. It will still validate
635635
basic integrity constraints, such as checking whether an uploaded file was too
636636
large or whether you tried to submit text in a number field.
637637

638+
.. seealso::
639+
640+
To see how to use a service with dependencies to resolve ``validation_groups``
641+
checkout cookbook article :doc:`/cookbook/validation/validation-group-service-resolver`.
642+
638643
.. index::
639644
single: Forms; Built-in field types
640645

cookbook/map.rst.inc

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

249249
* :doc:`/cookbook/validation/custom_constraint`
250250
* :doc:`/cookbook/validation/severity`
251+
* :doc:`/cookbook/validation/validation-group-service-resolver`
251252

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

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 set the
71+
validation groups returned when validating.

0 commit comments

Comments
 (0)