From e2cefae59ec271b4e9165de1db80800d9aee574e Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Sat, 28 Oct 2023 14:37:33 +0200 Subject: [PATCH] [Form] Document "class constraints" for data class-less forms --- form/without_class.rst | 57 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/form/without_class.rst b/form/without_class.rst index d0a44ed6205..b2ebdcc5482 100644 --- a/form/without_class.rst +++ b/form/without_class.rst @@ -80,7 +80,10 @@ But if the form is not mapped to an object and you instead want to retrieve an array of your submitted data, how can you add constraints to the data of your form? -The answer is to set up the constraints yourself, and attach them to the individual +Constraints At Field Level +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +One possibility is to set up the constraints yourself, and attach them to the individual fields. The overall approach is covered a bit more in :doc:`this validation article `, but here's a short example:: @@ -123,3 +126,55 @@ but here's a short example:: When a form is only partially submitted (for example, in an HTTP PATCH request), only the constraints from the submitted form fields will be evaluated. + +Constraints At Class Level +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Another possibility is to add the constraints at the class level. +This can be done by setting the ``constraints`` option in the +``configureOptions()`` method:: + + use Symfony\Component\Form\Extension\Core\Type\TextType; + use Symfony\Component\Form\FormBuilderInterface; + use Symfony\Component\OptionsResolver\OptionsResolver; + use Symfony\Component\Validator\Constraints\Length; + use Symfony\Component\Validator\Constraints\NotBlank; + + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $builder + ->add('firstName', TextType::class) + ->add('lastName', TextType::class); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $constraints = [ + 'firstName' => new Length(['min' => 3]), + 'lastName' => [ + new NotBlank(), + new Length(['min' => 3]), + ], + ]; + + $resolver->setDefaults([ + 'data_class' => null, + 'constraints' => $constraints, + ]); + } + +This means you can also do this when using the ``createFormBuilder()`` method +in your controller:: + + $form = $this->createFormBuilder($defaultData, [ + 'constraints' => [ + 'firstName' => new Length(['min' => 3]), + 'lastName' => [ + new NotBlank(), + new Length(['min' => 3]), + ], + ], + ]) + ->add('firstName', TextType::class) + ->add('lastName', TextType::class) + ->getForm();