From b81389fc81aa739788a41a99cb7ac0166bf19baf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandru=20Furculita=20=E2=99=BB?= Date: Sat, 5 Aug 2017 16:59:09 +0300 Subject: [PATCH 1/6] Create validation_group_service_resolver.rst --- form/validation_group_service_resolver.rst | 72 ++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 form/validation_group_service_resolver.rst diff --git a/form/validation_group_service_resolver.rst b/form/validation_group_service_resolver.rst new file mode 100644 index 00000000000..e5ba3824fe0 --- /dev/null +++ b/form/validation_group_service_resolver.rst @@ -0,0 +1,72 @@ +How to Dynamically Configure Form Validation Groups +============================================== + +Sometimes you need advanced logic to determine the validation groups. If they +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 + + // src/AppBundle/Validation/ValidationGroupResolver.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 + + // src/AppBundle/Form/MyClassType.php; + namespace AppBundle\Form; + + use AppBundle\Validator\ValidationGroupResolver; + use Symfony\Component\Form\AbstractType + use Symfony\Component\OptionsResolver\OptionsResolver; + + class MyClassType extends AbstractType + { + private $groupResolver; + + public function __construct(ValidationGroupResolver $groupResolver) + { + $this->groupResolver = $groupResolver; + } + + // ... + public function configureOptions(OptionsResolver $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. From 232776217071c32d836e3c97df05f9354fcb65b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandru=20Furculita=20=E2=99=BB?= Date: Sat, 5 Aug 2017 16:59:28 +0300 Subject: [PATCH 2/6] Delete group_service_resolver.rst --- validation/group_service_resolver.rst | 72 --------------------------- 1 file changed, 72 deletions(-) delete mode 100644 validation/group_service_resolver.rst diff --git a/validation/group_service_resolver.rst b/validation/group_service_resolver.rst deleted file mode 100644 index bf731b9613a..00000000000 --- a/validation/group_service_resolver.rst +++ /dev/null @@ -1,72 +0,0 @@ -How to Dynamically Configure Validation Groups -============================================== - -Sometimes you need advanced logic to determine the validation groups. If they -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 - - // src/AppBundle/Validation/ValidationGroupResolver.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 - - // src/AppBundle/Form/MyClassType.php; - namespace AppBundle\Form; - - use AppBundle\Validator\ValidationGroupResolver; - use Symfony\Component\Form\AbstractType - use Symfony\Component\OptionsResolver\OptionsResolver; - - class MyClassType extends AbstractType - { - private $groupResolver; - - public function __construct(ValidationGroupResolver $groupResolver) - { - $this->groupResolver = $groupResolver; - } - - // ... - public function configureOptions(OptionsResolver $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. From bb4efac24bcfb3928a5c3f0f816232d8ac95a8ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandru=20Furculita=20=E2=99=BB?= Date: Sat, 5 Aug 2017 17:01:01 +0300 Subject: [PATCH 3/6] Update button_based_validation.rst --- form/button_based_validation.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/form/button_based_validation.rst b/form/button_based_validation.rst index 4a28aaad405..925d1dcdd40 100644 --- a/form/button_based_validation.rst +++ b/form/button_based_validation.rst @@ -36,4 +36,4 @@ large or whether you tried to submit text in a number field. .. seealso:: To see how to use a service to resolve ``validation_groups`` dynamically - read the :doc:`/validation/group_service_resolver` article. + read the :doc:`/form/validation_group_service_resolver` article. From e38aff8944ed162035f63abec94879179f0232cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandru=20Furculita=20=E2=99=BB?= Date: Sat, 5 Aug 2017 17:02:03 +0300 Subject: [PATCH 4/6] Update redirection_map --- _build/redirection_map | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_build/redirection_map b/_build/redirection_map index fd1d1102822..2f0df97352e 100644 --- a/_build/redirection_map +++ b/_build/redirection_map @@ -262,7 +262,7 @@ /cookbook/upgrade/minor_version /setup/upgrade_major /cookbook/upgrade/patch_version /upgrade/bundles /cookbook/validation/custom_constraint /validation/custom_constraint -/cookbook/validation/group_service_resolver /validation/group_service_resolver +/cookbook/validation/group_service_resolver /form/validation_group_service_resolver /cookbook/validation/index /validation /cookbook/validation/severity /validation/severity /cookbook/web_server/built_in /setup/built_in_web_server From 1275326d5402273977bddfd54ddb63d130db811a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandru=20Furculita=20=E2=99=BB?= Date: Sat, 5 Aug 2017 17:16:22 +0300 Subject: [PATCH 5/6] Update validation_group_service_resolver.rst --- form/validation_group_service_resolver.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/form/validation_group_service_resolver.rst b/form/validation_group_service_resolver.rst index e5ba3824fe0..79d0791d1eb 100644 --- a/form/validation_group_service_resolver.rst +++ b/form/validation_group_service_resolver.rst @@ -1,5 +1,5 @@ How to Dynamically Configure Form Validation Groups -============================================== +=================================================== Sometimes you need advanced logic to determine the validation groups. If they can't be determined by a simple callback, you can use a service. Create a From c4e2004e5880fc275dd18c401c13a1531d5d54f1 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 7 Aug 2017 09:03:49 +0200 Subject: [PATCH 6/6] Added a missing redirection --- _build/redirection_map | 1 + 1 file changed, 1 insertion(+) diff --git a/_build/redirection_map b/_build/redirection_map index 2f0df97352e..f9b02ef98ac 100644 --- a/_build/redirection_map +++ b/_build/redirection_map @@ -339,3 +339,4 @@ /service_container/third_party /service_container /templating/templating_service /templates /components/http_foundation/trusting_proxies /request/load_balancer_reverse_proxy +/validation/group_service_resolver /form/validation_group_service_resolver