From 99da812f88ca938a139fb1286f201dc1798d7371 Mon Sep 17 00:00:00 2001 From: Rafael Dohms Date: Sat, 22 Dec 2012 12:35:49 +0100 Subject: [PATCH 1/3] Improved Form Validation Docs Added info about adding constraints to each field as opposed to only using DefaultOptions. Also added a tip about settings validation groups. --- book/forms.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/book/forms.rst b/book/forms.rst index 37a2dacf214..ead392bc340 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -1581,6 +1581,36 @@ an array of data, instead of an object. In most cases, it's better - and certainly more robust - to bind your form to an object. But for simple forms, this is a great approach. +.. versionadded:: 2.1 + It is now possible to add constraints directly to the fields using the + ``constraints`` option, which accepts a single constraint or an array + of them. + +:: + use Symfony\Component\Validator\Constraints\MinLength; + use Symfony\Component\Validator\Constraints\NotBlank; + + $builder + ->add('firstName', 'text', array( + 'constraints' => new MinLength(3), + )) + ->add('lastName', 'text', array( + 'constraints' => array( + new NotBlank(), + new MinLength(3), + ), + )) + ; + +.. tip:: + If you are using Validation Groups, you need to either reference the + ``Default`` group when creating the form, or set the correct group on + the constraint you are adding. + +:: + new NotBlank(array('groups' => array('create', 'update')) + + Final Thoughts -------------- From 41db0419c2b69cbc82c09403bcb8cb5092d20d57 Mon Sep 17 00:00:00 2001 From: Rafael Dohms Date: Sat, 22 Dec 2012 13:19:10 +0100 Subject: [PATCH 2/3] Fixing standards. --- book/forms.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/book/forms.rst b/book/forms.rst index ead392bc340..72cd67dd798 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -1603,6 +1603,7 @@ this is a great approach. ; .. tip:: + If you are using Validation Groups, you need to either reference the ``Default`` group when creating the form, or set the correct group on the constraint you are adding. From 7c7a8e7a4b1ba531527bca00004f6cca738efd49 Mon Sep 17 00:00:00 2001 From: Rafael Dohms Date: Fri, 28 Dec 2012 00:22:07 +0100 Subject: [PATCH 3/3] Fixing code block name --- book/forms.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index 72cd67dd798..6745b855724 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -1586,7 +1586,8 @@ this is a great approach. ``constraints`` option, which accepts a single constraint or an array of them. -:: +.. code-block:: php + use Symfony\Component\Validator\Constraints\MinLength; use Symfony\Component\Validator\Constraints\NotBlank; @@ -1608,7 +1609,8 @@ this is a great approach. ``Default`` group when creating the form, or set the correct group on the constraint you are adding. -:: +.. code-block:: php + new NotBlank(array('groups' => array('create', 'update'))