diff --git a/reference/constraints/Callback.rst b/reference/constraints/Callback.rst index 9e5ff19d216..cc545296750 100644 --- a/reference/constraints/Callback.rst +++ b/reference/constraints/Callback.rst @@ -158,6 +158,18 @@ process. Each method can be one of the following formats: { } + .. code-block:: xml + + + + + + + + .. code-block:: php // src/Acme/BlogBundle/Entity/Author.php diff --git a/reference/constraints/Choice.rst b/reference/constraints/Choice.rst index f3cb36ff329..e0c57da530d 100644 --- a/reference/constraints/Choice.rst +++ b/reference/constraints/Choice.rst @@ -46,21 +46,6 @@ If your valid choice list is simple, you can pass them in directly via the choices: [male, female] message: Choose a valid gender. - .. code-block:: xml - - - - - - - - - - - .. code-block:: php-annotations // src/Acme/BlogBundle/Entity/Author.php @@ -76,6 +61,21 @@ If your valid choice list is simple, you can pass them in directly via the protected $gender; } + .. code-block:: xml + + + + + + + + + + + .. code-block:: php // src/Acme/BlogBundle/EntityAuthor.php @@ -108,6 +108,8 @@ form element. .. code-block:: php // src/Acme/BlogBundle/Entity/Author.php + namespace Acme\BlogBundle\Entity; + class Author { public static function getGenders() @@ -132,6 +134,8 @@ constraint. .. code-block:: php-annotations // src/Acme/BlogBundle/Entity/Author.php + namespace Acme\BlogBundle\Entity; + use Symfony\Component\Validator\Constraints as Assert; class Author @@ -153,6 +157,26 @@ constraint. + .. code-block:: php + + // src/Acme/BlogBundle/EntityAuthor.php + namespace Acme\BlogBundle\Entity; + + use Symfony\Component\Validator\Mapping\ClassMetadata; + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + protected $gender; + + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addPropertyConstraint('gender', new Assert\Choice(array( + 'callback' => 'getGenders', + ))); + } + } + If the static callback is stored in a different class, for example ``Util``, you can pass the class name and the method as an array. @@ -166,6 +190,21 @@ you can pass the class name and the method as an array. gender: - Choice: { callback: [Util, getGenders] } + .. code-block:: php-annotations + + // src/Acme/BlogBundle/Entity/Author.php + namespace Acme\BlogBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + /** + * @Assert\Choice(callback = {"Util", "getGenders"}) + */ + protected $gender; + } + .. code-block:: xml @@ -180,17 +219,24 @@ you can pass the class name and the method as an array. - .. code-block:: php-annotations + .. code-block:: php - // src/Acme/BlogBundle/Entity/Author.php - use Symfony\Component\Validator\Constraints as Assert; + // src/Acme/BlogBundle/EntityAuthor.php + namespace Acme\BlogBundle\Entity; + use Symfony\Component\Validator\Mapping\ClassMetadata; + use Symfony\Component\Validator\Constraints as Assert; + class Author { - /** - * @Assert\Choice(callback = {"Util", "getGenders"}) - */ protected $gender; + + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addPropertyConstraint('gender', new Assert\Choice(array( + 'callback' => array('Util', 'getGenders'), + ))); + } } Available Options diff --git a/reference/constraints/Collection.rst b/reference/constraints/Collection.rst index 07ffc0dbe73..ba63fb2f485 100644 --- a/reference/constraints/Collection.rst +++ b/reference/constraints/Collection.rst @@ -30,6 +30,7 @@ Basic Usage The ``Collection`` constraint allows you to validate the different keys of a collection individually. Take the following example:: + // src/Acme/BlogBundle/Entity/Author.php namespace Acme\BlogBundle\Entity; class Author @@ -53,7 +54,7 @@ blank but is no longer than 100 characters in length, you would do the following .. code-block:: yaml - # src/BlogBundle/Resources/config/validation.yml + # src/Acme/BlogBundle/Resources/config/validation.yml Acme\BlogBundle\Entity\Author: properties: profileData: diff --git a/reference/constraints/Email.rst b/reference/constraints/Email.rst index 06a97bbed22..50802f97d97 100644 --- a/reference/constraints/Email.rst +++ b/reference/constraints/Email.rst @@ -29,23 +29,6 @@ Basic Usage - Email: message: The email "{{ value }}" is not a valid email. checkMX: true - .. code-block:: xml - - - - - - - - - - - - - - .. code-block:: php-annotations @@ -65,6 +48,24 @@ Basic Usage protected $email; } + .. code-block:: xml + + + + + + + + + + + + + + + .. code-block:: php // src/Acme/BlogBundle/Entity/Author.php diff --git a/reference/constraints/False.rst b/reference/constraints/False.rst index bb11845cccd..118999cede0 100644 --- a/reference/constraints/False.rst +++ b/reference/constraints/False.rst @@ -48,17 +48,6 @@ method returns **false**: - "False": message: You've entered an invalid state. - .. code-block:: xml - - - - - - - - - - .. code-block:: php-annotations // src/Acme/BlogBundle/Entity/Author.php @@ -69,14 +58,27 @@ method returns **false**: class Author { /** - * @Assert\False() + * @Assert\False( + * message = "You've entered an invalid state." + * ) */ - public function isStateInvalid($message = "You've entered an invalid state.") + public function isStateInvalid() { // ... } } + .. code-block:: xml + + + + + + + + + + .. code-block:: php // src/Acme/BlogBundle/Entity/Author.php diff --git a/reference/constraints/File.rst b/reference/constraints/File.rst index de15ff6d4c7..d5d1605f99d 100644 --- a/reference/constraints/File.rst +++ b/reference/constraints/File.rst @@ -120,15 +120,14 @@ below a certain file size and a valid PDF, add the following: // src/Acme/BlogBundle/Entity/Author.php namespace Acme\BlogBundle\Entity; - // ... use Symfony\Component\Validator\Mapping\ClassMetadata; - use Symfony\Component\Validator\Constraints\File; + use Symfony\Component\Validator\Constraints as Assert; class Author { public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addPropertyConstraint('bioFile', new File(array( + $metadata->addPropertyConstraint('bioFile', new Assert\File(array( 'maxSize' => '1024k', 'mimeTypes' => array( 'application/pdf', diff --git a/reference/constraints/UniqueEntity.rst b/reference/constraints/UniqueEntity.rst index 442e9b39b7c..e06fe15fc6d 100644 --- a/reference/constraints/UniqueEntity.rst +++ b/reference/constraints/UniqueEntity.rst @@ -27,6 +27,16 @@ table: .. configuration-block:: + .. code-block:: yaml + + # src/Acme/UserBundle/Resources/config/validation.yml + Acme\UserBundle\Entity\Author: + constraints: + - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email + properties: + email: + - Email: ~ + .. code-block:: php-annotations // Acme/UserBundle/Entity/User.php @@ -55,16 +65,6 @@ table: // ... } - .. code-block:: yaml - - # src/Acme/UserBundle/Resources/config/validation.yml - Acme\UserBundle\Entity\Author: - constraints: - - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email - properties: - email: - - Email: ~ - .. code-block:: xml @@ -72,11 +72,35 @@ table: - + + .. code-block:: php + + + // Acme/UserBundle/Entity/User.php + namespace Acme\UserBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + // DON'T forget this use statement!!! + use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; + + class Author + { + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addConstraint(new UniqueEntity(array( + 'fields' => 'email', + 'message' => 'This email already exists.', + ))); + + $metadata->addPropertyConstraint(new Assert\Email()); + } + } + Options ------- diff --git a/reference/constraints/Valid.rst b/reference/constraints/Valid.rst index f8c897311eb..2a847f8b64e 100644 --- a/reference/constraints/Valid.rst +++ b/reference/constraints/Valid.rst @@ -22,7 +22,9 @@ an ``Address`` instance in the ``$address`` property. .. code-block:: php - // src/Acme/HelloBundle/Address.php + // src/Acme/HelloBundle/Entity/Address.php + namespace Amce\HelloBundle\Entity; + class Address { protected $street; @@ -31,7 +33,9 @@ an ``Address`` instance in the ``$address`` property. .. code-block:: php - // src/Acme/HelloBundle/Author.php + // src/Acme/HelloBundle/Entity/Author.php + namespace Amce\HelloBundle\Entity; + class Author { protected $firstName; @@ -44,7 +48,7 @@ an ``Address`` instance in the ``$address`` property. .. code-block:: yaml # src/Acme/HelloBundle/Resources/config/validation.yml - Acme\HelloBundle\Address: + Acme\HelloBundle\Entity\Address: properties: street: - NotBlank: ~ @@ -52,7 +56,7 @@ an ``Address`` instance in the ``$address`` property. - NotBlank: ~ - MaxLength: 5 - Acme\HelloBundle\Author: + Acme\HelloBundle\Entity\Author: properties: firstName: - NotBlank: ~ @@ -60,29 +64,6 @@ an ``Address`` instance in the ``$address`` property. lastName: - NotBlank: ~ - .. code-block:: xml - - - - - - - - - 5 - - - - - - - 4 - - - - - - .. code-block:: php-annotations // src/Acme/HelloBundle/Entity/Address.php @@ -123,26 +104,47 @@ an ``Address`` instance in the ``$address`` property. protected $address; } + .. code-block:: xml + + + + + + + + + 5 + + + + + + + 4 + + + + + + .. code-block:: php // src/Acme/HelloBundle/Entity/Address.php namespace Acme\HelloBundle\Entity; use Symfony\Component\Validator\Mapping\ClassMetadata; - use Symfony\Component\Validator\Constraints\NotBlank; - use Symfony\Component\Validator\Constraints\MaxLength; + use Symfony\Component\Validator\Constraints as Assert; class Address { protected $street; - protected $zipCode; public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addPropertyConstraint('street', new NotBlank()); - $metadata->addPropertyConstraint('zipCode', new NotBlank()); - $metadata->addPropertyConstraint('zipCode', new MaxLength(5)); + $metadata->addPropertyConstraint('street', new Assert\NotBlank()); + $metadata->addPropertyConstraint('zipCode', new Assert\NotBlank()); + $metadata->addPropertyConstraint('zipCode', new Assert\MaxLength(5)); } } @@ -150,22 +152,19 @@ an ``Address`` instance in the ``$address`` property. namespace Acme\HelloBundle\Entity; use Symfony\Component\Validator\Mapping\ClassMetadata; - use Symfony\Component\Validator\Constraints\NotBlank; - use Symfony\Component\Validator\Constraints\MinLength; + use Symfony\Component\Validator\Constraints as Assert; class Author { protected $firstName; - protected $lastName; - protected $address; public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addPropertyConstraint('firstName', new NotBlank()); - $metadata->addPropertyConstraint('firstName', new MinLength(4)); - $metadata->addPropertyConstraint('lastName', new NotBlank()); + $metadata->addPropertyConstraint('firstName', new Assert\NotBlank()); + $metadata->addPropertyConstraint('firstName', new Assert\MinLength(4)); + $metadata->addPropertyConstraint('lastName', new Assert\NotBlank()); } } @@ -183,35 +182,37 @@ property. address: - Valid: ~ - .. code-block:: xml - - - - - - - - .. code-block:: php-annotations - // src/Acme/HelloBundle/Author.php + // src/Acme/HelloBundle/Entity/Author.php + namespace Acme\HelloBundle\Entity; + use Symfony\Component\Validator\Constraints as Assert; class Author { - /* ... */ - /** * @Assert\Valid */ protected $address; } + .. code-block:: xml + + + + + + + + .. code-block:: php - // src/Acme/HelloBundle/Author.php + // src/Acme/HelloBundle/Entity/Author.php + namespace Acme\HelloBundle\Entity; + use Symfony\Component\Validator\Mapping\ClassMetadata; - use Symfony\Component\Validator\Constraints\Valid; + use Symfony\Component\Validator\Constraints as Assert; class Author { @@ -219,7 +220,7 @@ property. public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addPropertyConstraint('address', new Valid()); + $metadata->addPropertyConstraint('address', new Assert\Valid()); } }