From 15bfb64271739f4f030ce6634d5c6fe95b2c77e5 Mon Sep 17 00:00:00 2001 From: "Andrew (Andrius) Marcinkevicius" Date: Wed, 7 Jan 2015 13:24:04 +0200 Subject: [PATCH 1/3] Update validation.rst | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.3 | Fixed tickets | Some fixes can be a bit "controversial". Due to PR https://github.com/symfony/symfony-docs/pull/4779/files not all changes were made. Something to keep in mind is what due to https://github.com/symfony/symfony-docs/pull/4779/files using `Assert\...` if arguments are kept on the same line the possibility of horizontal scrollbar (especially on the smaller screens) is quite high. Also for most use cases I think adding PHP constraints is better if arguments are on different lines ("easier to read" and easier to spot errors --- book/validation.rst | 117 +++++++++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 39 deletions(-) diff --git a/book/validation.rst b/book/validation.rst index 70d21eec5be..e2b9e766363 100644 --- a/book/validation.rst +++ b/book/validation.rst @@ -35,7 +35,7 @@ application. The goal of validation is to tell you if the data of an object is valid. For this to work, you'll configure a list of rules (called :ref:`constraints `) that the object must follow in order to be valid. These rules can be specified via a number of -different formats (YAML, XML, annotations, or PHP). +different formats (annotations, YAML, XML, or PHP). For example, to guarantee that the ``$name`` property is not empty, add the following: @@ -71,7 +71,8 @@ following: + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping + http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> @@ -271,8 +272,10 @@ annotations if you're using the annotation method to specify your constraints: + xsi:schemaLocation="http://symfony.com/schema/dic/services + http://symfony.com/schema/dic/services/services-1.0.xsd + http://symfony.com/schema/dic/symfony + http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> @@ -362,7 +365,8 @@ constraint, have several configuration options available. Suppose that the + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping + http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> @@ -391,10 +395,13 @@ constraint, have several configuration options available. Suppose that the public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addPropertyConstraint('gender', new Choice(array( - 'choices' => array('male', 'female'), - 'message' => 'Choose a valid gender.', - ))); + $metadata->addPropertyConstraint( + 'gender', + new Choice(array( + 'choices' => array('male', 'female'), + 'message' => 'Choose a valid gender.', + )) + ); } } @@ -436,7 +443,8 @@ options can be specified in this way. + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping + http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> @@ -476,7 +484,7 @@ 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). -Translation Constraint Messages +Translating Constraint Messages ------------------------------- For information on translating the constraint messages, see @@ -541,7 +549,8 @@ class to have at least 3 characters. + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping + http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> @@ -568,7 +577,10 @@ class to have at least 3 characters. public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addPropertyConstraint('firstName', new NotBlank()); + $metadata->addPropertyConstraint( + 'firstName', + new NotBlank() + ); $metadata->addPropertyConstraint( 'firstName', new Length(array("min" => 3))); @@ -626,7 +638,8 @@ this method must return ``true``: + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping + http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> @@ -649,9 +662,12 @@ this method must return ``true``: { public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addGetterConstraint('passwordLegal', new True(array( - 'message' => 'The password cannot match your first name', - ))); + $metadata->addGetterConstraint( + 'passwordLegal', + new True(array( + 'message' => 'The password cannot match your first name', + )) + ); } } @@ -744,7 +760,8 @@ user registers and when a user updates their contact information later: + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping + http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> @@ -769,7 +786,7 @@ user registers and when a user updates their contact information later: - + @@ -789,21 +806,33 @@ user registers and when a user updates their contact information later: { public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addPropertyConstraint('email', new Email(array( - 'groups' => array('registration'), - ))); - - $metadata->addPropertyConstraint('password', new NotBlank(array( - 'groups' => array('registration'), - ))); - $metadata->addPropertyConstraint('password', new Length(array( - 'min' => 7, - 'groups' => array('registration') - ))); + $metadata->addPropertyConstraint( + 'email', + new Email(array( + 'groups' => array('registration'), + )) + ); + + $metadata->addPropertyConstraint( + 'password', + new NotBlank(array( + 'groups' => array('registration'), + )) + ); + $metadata->addPropertyConstraint( + 'password', + new Length(array( + 'min' => 7, + 'groups' => array('registration'), + )) + ); $metadata->addPropertyConstraint( 'city', - Length(array("min" => 3))); + new Length(array( + 'min' => 2, + )) + ); } } @@ -919,7 +948,10 @@ username and the password are different only if all other validation passes private $password; /** - * @Assert\True(message="The password cannot match your username", groups={"Strict"}) + * @Assert\True( + * message="The password cannot match your username", + * groups={"Strict"} + * ) */ public function isPasswordLegal() { @@ -933,7 +965,8 @@ username and the password are different only if all other validation passes + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping + http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> @@ -962,10 +995,11 @@ username and the password are different only if all other validation passes // src/Acme/BlogBundle/Entity/User.php namespace Acme\BlogBundle\Entity; + use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints as Assert; - class User + class User implements UserInterface { public static function loadValidatorMetadata(ClassMetadata $metadata) { @@ -1062,7 +1096,8 @@ entity and a new constraint group called ``Premium``: + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping + http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> @@ -1100,10 +1135,13 @@ entity and a new constraint group called ``Premium``: public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('name', new Assert\NotBlank()); - $metadata->addPropertyConstraint('creditCard', new Assert\CardScheme( - 'schemes' => array('VISA'), - 'groups' => array('Premium'), - )); + $metadata->addPropertyConstraint( + 'creditCard', + new Assert\CardScheme(array( + 'schemes' => array('VISA'), + 'groups' => array('Premium'), + )) + ); } } @@ -1206,6 +1244,7 @@ email address. This is actually pretty easy to do. From inside a controller, it looks like this:: use Symfony\Component\Validator\Constraints\Email; + // ... public function addEmailAction($email) From 59673ae3042fa0e217d10df17ebbe2027eee21e9 Mon Sep 17 00:00:00 2001 From: "Andrew (Andrius) Marcinkevicius" Date: Wed, 7 Jan 2015 14:58:46 +0200 Subject: [PATCH 2/3] Update validation.rst --- book/validation.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/book/validation.rst b/book/validation.rst index e2b9e766363..65175b0269c 100644 --- a/book/validation.rst +++ b/book/validation.rst @@ -35,7 +35,7 @@ application. The goal of validation is to tell you if the data of an object is valid. For this to work, you'll configure a list of rules (called :ref:`constraints `) that the object must follow in order to be valid. These rules can be specified via a number of -different formats (annotations, YAML, XML, or PHP). +different formats (annotations, YAML, XML or PHP). For example, to guarantee that the ``$name`` property is not empty, add the following: @@ -341,7 +341,9 @@ constraint, have several configuration options available. Suppose that the Acme\BlogBundle\Entity\Author: properties: gender: - - Choice: { choices: [male, female], message: Choose a valid gender. } + - Choice: + choices: [male, female] + message: Choose a valid gender. .. code-block:: php-annotations From 175dbd053897bff0405465ec97e685f2daf2ee09 Mon Sep 17 00:00:00 2001 From: "Andrew (Andrius) Marcinkevicius" Date: Tue, 13 Jan 2015 11:36:29 +0200 Subject: [PATCH 3/3] Add former label for BC --- book/validation.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/book/validation.rst b/book/validation.rst index 65175b0269c..0fa52c724f9 100644 --- a/book/validation.rst +++ b/book/validation.rst @@ -486,6 +486,8 @@ 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). +.. _translation-constraint-messages: + Translating Constraint Messages -------------------------------