diff --git a/book/validation.rst b/book/validation.rst index 23df94320e7..94c04ee3eff 100644 --- a/book/validation.rst +++ b/book/validation.rst @@ -270,7 +270,7 @@ annotations if you're using the annotation method to specify your constraints: // app/config/config.php $container->loadFromExtension('framework', array('validation' => array( 'enable_annotations' => true, - )); + ))); .. index:: single: Validation; Constraints @@ -367,7 +367,7 @@ constraint, have several configuration options available. Suppose that the $metadata->addPropertyConstraint('gender', new Choice(array( 'choices' => array('male', 'female'), 'message' => 'Choose a valid gender.', - )); + ))); } } @@ -609,30 +609,106 @@ constraints. For example, suppose you have a ``User`` class, which is used both when a user registers and when a user updates his/her contact information later:: - // src/Acme/BlogBundle/Entity/User.php - namespace Acme\BlogBundle\Entity; - - use Symfony\Component\Security\Core\User\UserInterface - use Symfony\Component\Validator\Constraints as Assert; - - class User implements UserInterface - { - /** - * @Assert\Email(groups={"registration"}) - */ - private $email; - - /** - * @Assert\NotBlank(groups={"registration"}) - * @Assert\MinLength(limit=7, groups={"registration"}) - */ - private $password; - - /** - * @Assert\MinLength(2) - */ - private $city; - } +.. configuration-block:: + + .. code-block:: yaml + + # src/Acme/BlogBundle/Resources/config/validation.yml + Acme\BlogBundle\Entity\User: + properties: + email: + - Email: { groups: [registration] } + password: + - NotBlank: { groups: [registration] } + - MinLength: { limit: 7, groups: [registration] } + city: + - MinLength: 2 + + .. code-block:: xml + + + + + + + + + + + + + + + + + + + 7 + + + + .. code-block:: php-annotations + + // src/Acme/BlogBundle/Entity/User.php + namespace Acme\BlogBundle\Entity; + + use Symfony\Component\Security\Core\User\UserInterface + use Symfony\Component\Validator\Constraints as Assert; + + class User implements UserInterface + { + /** + * @Assert\Email(groups={"registration"}) + */ + private $email; + + /** + * @Assert\NotBlank(groups={"registration"}) + * @Assert\MinLength(limit=7, groups={"registration"}) + */ + private $password; + + /** + * @Assert\MinLength(2) + */ + private $city; + } + + .. code-block:: php + + // src/Acme/BlogBundle/Entity/User.php + namespace Acme\BlogBundle\Entity; + + use Symfony\Component\Validator\Mapping\ClassMetadata; + use Symfony\Component\Validator\Constraints\Email; + use Symfony\Component\Validator\Constraints\NotBlank; + use Symfony\Component\Validator\Constraints\MinLength; + + class User + { + 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 MinLength(array( + 'limit' => 7, + 'groups' => array('registration') + ))); + + $metadata->addPropertyConstraint('city', new MinLength(3)); + } + } With this configuration, there are two validation groups: