From 70833ef53ad01b3fb4ed904b12b55989fd3afbeb Mon Sep 17 00:00:00 2001 From: David Lumaye Date: Wed, 22 Mar 2017 07:38:41 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Add=20an=20explanation=20about=20=C2=ABcons?= =?UTF-8?q?traints=C2=BB=20validation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added an explanation about the use of «constraints» in Form Classes and the requirement of «ValidatorExtension» if «constraints» keyword is used. --- validation.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/validation.rst b/validation.rst index 72652c13d83..eb48e262881 100644 --- a/validation.rst +++ b/validation.rst @@ -454,6 +454,23 @@ for the constraint or play it safe by always passing in an array of options .. _validator-constraint-targets: +Constraint in Form Classes +------------------ + +When creating your own form classes, you may want to add constraint directly in the formBuilder. +This is done by simply adding them as a parameter in your field options:: + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('myField', TextType::class, ['required' => true, 'constraints' => [new Length(['min' => 3])]]) + } + +Please note the *constraints* keyword will only be available if you have +added the *ValidatorExtention* to the formBuilder:: + + Forms::createFormFactoryBuilder()->addExtension(new ValidatorExtension(Validation::createValidator()))->getFormFactory(); + Constraint Targets ------------------ From 6d2a1e41100431f3a7570d3d53ff68e736fd10c7 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 22 Mar 2017 11:35:04 +0100 Subject: [PATCH 2/2] Rewords and minor fixes --- validation.rst | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/validation.rst b/validation.rst index eb48e262881..2c165f3a30d 100644 --- a/validation.rst +++ b/validation.rst @@ -449,27 +449,33 @@ If you're ever unsure of how to specify an option, either check the API document for the constraint or play it safe by always passing in an array of options (the first method shown above). -.. index:: - single: Validation; Constraint targets - -.. _validator-constraint-targets: - -Constraint in Form Classes ------------------- +Constraints in Form Classes +--------------------------- -When creating your own form classes, you may want to add constraint directly in the formBuilder. -This is done by simply adding them as a parameter in your field options:: +Constraints can be defined while building the form via the ``constraints`` option +of the form fields:: public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('myField', TextType::class, ['required' => true, 'constraints' => [new Length(['min' => 3])]]) + ->add('myField', TextType::class, array( + 'required' => true, + 'constraints' => array(new Length(array('min' => 3))) + )) } -Please note the *constraints* keyword will only be available if you have -added the *ValidatorExtention* to the formBuilder:: +The ``constraints`` option is only available when adding the ValidatorExtention +to the formBuilder:: - Forms::createFormFactoryBuilder()->addExtension(new ValidatorExtension(Validation::createValidator()))->getFormFactory(); + Forms::createFormFactoryBuilder() + ->addExtension(new ValidatorExtension(Validation::createValidator())) + ->getFormFactory() + ; + +.. index:: + single: Validation; Constraint targets + +.. _validator-constraint-targets: Constraint Targets ------------------