diff --git a/reference/constraints/All.rst b/reference/constraints/All.rst
index 4601d443ffa..55007203ccb 100644
--- a/reference/constraints/All.rst
+++ b/reference/constraints/All.rst
@@ -34,21 +34,21 @@ entry in that array:
.. code-block:: php-annotations
- // src/Acme/UserBundle/Entity/User.php
- namespace Acme\UserBundle\Entity;
-
- use Symfony\Component\Validator\Constraints as Assert;
-
- class User
- {
- /**
- * @Assert\All({
- * @Assert\NotBlank
- * @Assert\MinLength(5),
- * })
- */
- protected $favoriteColors = array();
- }
+ // src/Acme/UserBundle/Entity/User.php
+ namespace Acme\UserBundle\Entity;
+
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class User
+ {
+ /**
+ * @Assert\All({
+ * @Assert\NotBlank
+ * @Assert\MinLength(5),
+ * })
+ */
+ protected $favoriteColors = array();
+ }
.. code-block:: xml
@@ -58,14 +58,35 @@ entry in that array:
+ .. code-block:: php
+
+ // src/Acme/UserBundle/Enttiy/User.php
+ namespace Acme\UserBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class User
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('favoriteColors', new Assert\All(array(
+ 'constraints' => array(
+ new Assert\NotBlank(),
+ new Assert\MinLength(array('limit' => 5)),
+ ),
+ )));
+ }
+ }
+
Now, each entry in the ``favoriteColors`` array will be validated to not
be blank and to be at least 5 characters long.
diff --git a/reference/constraints/Blank.rst b/reference/constraints/Blank.rst
index 2c37e1b741a..9186aa64261 100644
--- a/reference/constraints/Blank.rst
+++ b/reference/constraints/Blank.rst
@@ -26,13 +26,17 @@ of an ``Author`` class were blank, you could do the following:
.. code-block:: yaml
- properties:
- firstName:
- - Blank: ~
+ # src/BlogBundle/Resources/config/validation.yml
+ Acme\BlogBundle\Entity\Author:
+ properties:
+ firstName:
+ - Blank: ~
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Author
@@ -52,6 +56,22 @@ of an ``Author`` class were blank, you could do the following:
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('firstName', new Assetc\Blank());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/Callback.rst b/reference/constraints/Callback.rst
index 027e534b922..987f1ea2930 100644
--- a/reference/constraints/Callback.rst
+++ b/reference/constraints/Callback.rst
@@ -43,6 +43,8 @@ Setup
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
/**
@@ -63,6 +65,24 @@ Setup
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addConstraint(new Assert\Callback(array(
+ 'methods' => array('isAuthorValid'),
+ )));
+ }
+ }
+
The Callback Method
-------------------
diff --git a/reference/constraints/Choice.rst b/reference/constraints/Choice.rst
index 83a0e24ed24..f3cb36ff329 100644
--- a/reference/constraints/Choice.rst
+++ b/reference/constraints/Choice.rst
@@ -64,6 +64,8 @@ If your valid choice list is simple, you can pass them in directly via the
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Author
@@ -77,8 +79,10 @@ If your valid choice list is simple, you can pass them in directly via the
.. code-block:: php
// src/Acme/BlogBundle/EntityAuthor.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Mapping\ClassMetadata;
- use Symfony\Component\Validator\Constraints\Choice;
+ use Symfony\Component\Validator\Constraints as Assert;
class Author
{
@@ -86,7 +90,7 @@ If your valid choice list is simple, you can pass them in directly via the
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
- $metadata->addPropertyConstraint('gender', new Choice(array(
+ $metadata->addPropertyConstraint('gender', new Assert\Choice(array(
'choices' => array('male', 'female'),
'message' => 'Choose a valid gender',
)));
@@ -280,4 +284,4 @@ strict
If true, the validator will also check the type of the input value. Specifically,
this value is passed to as the third argument to the PHP :phpfunction:`in_array` method
-when checking to see if a value is in the valid choices array.
\ No newline at end of file
+when checking to see if a value is in the valid choices array.
diff --git a/reference/constraints/Collection.rst b/reference/constraints/Collection.rst
index 8013166b7c7..07ffc0dbe73 100644
--- a/reference/constraints/Collection.rst
+++ b/reference/constraints/Collection.rst
@@ -53,21 +53,25 @@ blank but is no longer than 100 characters in length, you would do the following
.. code-block:: yaml
- properties:
- profileData:
- - Collection:
- fields:
- personal_email: Email
- short_bio:
- - NotBlank
- - MaxLength:
- limit: 100
- message: Your short bio is too long!
- allowMissingFields: true
+ # src/BlogBundle/Resources/config/validation.yml
+ Acme\BlogBundle\Entity\Author:
+ properties:
+ profileData:
+ - Collection:
+ fields:
+ personal_email: Email
+ short_bio:
+ - NotBlank
+ - MaxLength:
+ limit: 100
+ message: Your short bio is too long!
+ allowMissingFields: true
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Author
@@ -119,10 +123,10 @@ blank but is no longer than 100 characters in length, you would do the following
.. code-block:: php
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Mapping\ClassMetadata;
- use Symfony\Component\Validator\Constraints\Collection;
- use Symfony\Component\Validator\Constraints\Email;
- use Symfony\Component\Validator\Constraints\MaxLength;
+ use Symfony\Component\Validator\Constraints as Assert;
class Author
{
@@ -130,10 +134,10 @@ blank but is no longer than 100 characters in length, you would do the following
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
- $metadata->addPropertyConstraint('profileData', new Collection(array(
+ $metadata->addPropertyConstraint('profileData', new Assert\Collection(array(
'fields' => array(
- 'personal_email' => new Email(),
- 'lastName' => array(new NotBlank(), new MaxLength(100)),
+ 'personal_email' => new Assert\Email(),
+ 'lastName' => array(new Assert\NotBlank(), new Assert\MaxLength(100)),
),
'allowMissingFields' => true,
)));
diff --git a/reference/constraints/Country.rst b/reference/constraints/Country.rst
index 118c0ff3a00..cc5ec176fb0 100644
--- a/reference/constraints/Country.rst
+++ b/reference/constraints/Country.rst
@@ -28,18 +28,18 @@ Basic Usage
.. code-block:: php-annotations
- // src/Acme/UserBundle/Entity/User.php
- namespace Acme\UserBundle\Entity;
-
- use Symfony\Component\Validator\Constraints as Assert;
-
- class User
- {
- /**
- * @Assert\Country
- */
- protected $country;
- }
+ // src/Acme/UserBundle/Entity/User.php
+ namespace Acme\UserBundle\Entity;
+
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class User
+ {
+ /**
+ * @Assert\Country
+ */
+ protected $country;
+ }
.. code-block:: xml
@@ -50,6 +50,22 @@ Basic Usage
+ .. code-block:: php
+
+ // src/Acme/UserBundle/Entity/User.php
+ namespace Acme\UserBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class User
+ {
+ public static function loadValidationMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('country', new Assert\Country());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/Date.rst b/reference/constraints/Date.rst
index 259495eb652..293ed88bc35 100644
--- a/reference/constraints/Date.rst
+++ b/reference/constraints/Date.rst
@@ -31,6 +31,8 @@ Basic Usage
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Author
@@ -50,6 +52,22 @@ Basic Usage
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('birthday', new Assert\Date());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/DateTime.rst b/reference/constraints/DateTime.rst
index d4c92419073..91a590d57bb 100644
--- a/reference/constraints/DateTime.rst
+++ b/reference/constraints/DateTime.rst
@@ -52,6 +52,22 @@ Basic Usage
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('createdAt', new Assert\DataTime());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/Email.rst b/reference/constraints/Email.rst
index ed549978b99..06a97bbed22 100644
--- a/reference/constraints/Email.rst
+++ b/reference/constraints/Email.rst
@@ -65,6 +65,25 @@ Basic Usage
protected $email;
}
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('email', new Assert\Email(array(
+ 'message' => 'The email "{{ value }}" is not a valid email.',
+ 'check' => true,
+ )));
+ }
+ }
+
Options
-------
@@ -83,4 +102,4 @@ checkMX
If true, then the `checkdnsrr`_ PHP function will be used to check the validity
of the MX record of the host of the given email.
-.. _`checkdnsrr`: http://www.php.net/manual/en/function.checkdnsrr.php
\ No newline at end of file
+.. _`checkdnsrr`: http://www.php.net/manual/en/function.checkdnsrr.php
diff --git a/reference/constraints/False.rst b/reference/constraints/False.rst
index ada5c9c3173..bb11845cccd 100644
--- a/reference/constraints/False.rst
+++ b/reference/constraints/False.rst
@@ -62,6 +62,8 @@ method returns **false**:
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Author
@@ -75,6 +77,22 @@ method returns **false**:
}
}
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addGetterConstraint('stateInvalid', new Assert\False());
+ }
+ }
+
.. caution::
When using YAML, be sure to surround ``False`` with quotes (``"False"``)
diff --git a/reference/constraints/File.rst b/reference/constraints/File.rst
index 5a68bff04f2..f9d18a83cdc 100644
--- a/reference/constraints/File.rst
+++ b/reference/constraints/File.rst
@@ -83,6 +83,8 @@ below a certain file size and a valid PDF, add the following:
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Author
@@ -116,15 +118,13 @@ below a certain file size and a valid PDF, add the following:
.. code-block:: php
// src/Acme/BlogBundle/Entity/Author.php
- // ...
+ namespace Acme\BlogBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\File;
class Author
{
- // ...
-
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('bioFile', new File(array(
@@ -231,4 +231,4 @@ for some unknown reason, such as the file upload failed or it couldn't be writte
to disk.
-.. _`IANA website`: http://www.iana.org/assignments/media-types/index.html
\ No newline at end of file
+.. _`IANA website`: http://www.iana.org/assignments/media-types/index.html
diff --git a/reference/constraints/Ip.rst b/reference/constraints/Ip.rst
index 761f044d2a7..ede78b3efea 100644
--- a/reference/constraints/Ip.rst
+++ b/reference/constraints/Ip.rst
@@ -31,18 +31,18 @@ Basic Usage
.. code-block:: php-annotations
- // src/Acme/BlogBundle/Entity/Author.php
- namespace Acme\BlogBundle\Entity;
-
- use Symfony\Component\Validator\Constraints as Assert;
-
- class Author
- {
- /**
- * @Assert\Ip
- */
- protected $ipAddress;
- }
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ /**
+ * @Assert\Ip
+ */
+ protected $ipAddress;
+ }
.. code-block:: xml
@@ -53,6 +53,22 @@ Basic Usage
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('ipAddress', new Assert\Ip());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/Language.rst b/reference/constraints/Language.rst
index f74b6eab75b..7d4639802bb 100644
--- a/reference/constraints/Language.rst
+++ b/reference/constraints/Language.rst
@@ -28,18 +28,18 @@ Basic Usage
.. code-block:: php-annotations
- // src/Acme/UserBundle/Entity/User.php
- namespace Acme\UserBundle\Entity;
-
- use Symfony\Component\Validator\Constraints as Assert;
-
- class User
- {
- /**
- * @Assert\Language
- */
- protected $preferredLanguage;
- }
+ // src/Acme/UserBundle/Entity/User.php
+ namespace Acme\UserBundle\Entity;
+
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class User
+ {
+ /**
+ * @Assert\Language
+ */
+ protected $preferredLanguage;
+ }
.. code-block:: xml
@@ -50,6 +50,22 @@ Basic Usage
+ .. code-block:: php
+
+ // src/Acme/UserBundle/Entity/User.php
+ namespace Acme\UserBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class User
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('preferredLanguage', new Assert\Language());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/Locale.rst b/reference/constraints/Locale.rst
index b86acb5b346..b841651577b 100644
--- a/reference/constraints/Locale.rst
+++ b/reference/constraints/Locale.rst
@@ -32,18 +32,18 @@ Basic Usage
.. code-block:: php-annotations
- // src/Acme/UserBundle/Entity/User.php
- namespace Acme\UserBundle\Entity;
-
- use Symfony\Component\Validator\Constraints as Assert;
-
- class User
- {
- /**
- * @Assert\Locale
- */
- protected $locale;
- }
+ // src/Acme/UserBundle/Entity/User.php
+ namespace Acme\UserBundle\Entity;
+
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class User
+ {
+ /**
+ * @Assert\Locale
+ */
+ protected $locale;
+ }
.. code-block:: xml
@@ -54,6 +54,22 @@ Basic Usage
+ .. code-block:: php
+
+ // src/Acme/UserBundle/Entity/User.php
+ namespace Acme\UserBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class User
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('locale', new Assert\Locale());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/Max.rst b/reference/constraints/Max.rst
index 084483fec71..8317e972e26 100644
--- a/reference/constraints/Max.rst
+++ b/reference/constraints/Max.rst
@@ -34,6 +34,8 @@ add the following:
.. code-block:: php-annotations
// src/Acme/EventBundle/Entity/Participant.php
+ namespace Acme\EventBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Participant
@@ -56,6 +58,25 @@ add the following:
+ .. code-block:: php
+
+ // src/Acme/EventBundle/Entity/Participant.php
+ namespace Acme\EventBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Participant
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('age', new Assert\Max(array(
+ 'limit' => 50,
+ 'message' => "You must be 50 or under to enter.",
+ )));
+ }
+ }
+
Options
-------
@@ -81,4 +102,4 @@ invalidMessage
**type**: ``string`` **default**: ``This value should be a valid number``
The message that will be shown if the underlying value is not a number (per
-the :phpfunction:`is_numeric` PHP function).
\ No newline at end of file
+the :phpfunction:`is_numeric` PHP function).
diff --git a/reference/constraints/MaxLength.rst b/reference/constraints/MaxLength.rst
index 2c379b19df1..b1bcc7b5fd2 100644
--- a/reference/constraints/MaxLength.rst
+++ b/reference/constraints/MaxLength.rst
@@ -31,6 +31,8 @@ Basic Usage
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Blog.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Blog
@@ -52,6 +54,24 @@ Basic Usage
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Blog.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Blog
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('summary', new Assert\MaxLength(array(
+ 'limit' => 100,
+ )));
+ }
+ }
+
Options
-------
@@ -78,4 +98,4 @@ charset
If the PHP extension "mbstring" is installed, then the PHP function :phpfunction:`mb_strlen`
will be used to calculate the length of the string. The value of the ``charset``
-option is passed as the second argument to that function.
\ No newline at end of file
+option is passed as the second argument to that function.
diff --git a/reference/constraints/Min.rst b/reference/constraints/Min.rst
index 72935b36eaf..a51178e13be 100644
--- a/reference/constraints/Min.rst
+++ b/reference/constraints/Min.rst
@@ -34,6 +34,8 @@ the following:
.. code-block:: php-annotations
// src/Acme/EventBundle/Entity/Participant.php
+ namespace Acme\EventBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Participant
@@ -69,4 +71,4 @@ invalidMessage
**type**: ``string`` **default**: ``This value should be a valid number``
The message that will be shown if the underlying value is not a number (per
-the :phpfunction:`is_numeric` PHP function).
\ No newline at end of file
+the :phpfunction:`is_numeric` PHP function).
diff --git a/reference/constraints/MinLength.rst b/reference/constraints/MinLength.rst
index 9bcd3cc2a67..50b3350b18e 100644
--- a/reference/constraints/MinLength.rst
+++ b/reference/constraints/MinLength.rst
@@ -31,6 +31,8 @@ Basic Usage
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Blog.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Blog
@@ -56,6 +58,25 @@ Basic Usage
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Blog.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Blog
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('summary', new Assert\MinLength(array(
+ 'limit' => 3,
+ 'message' => 'Your name must have at least {{ limit }} characters.',
+ )));
+ }
+ }
+
Options
-------
@@ -82,4 +103,4 @@ charset
If the PHP extension "mbstring" is installed, then the PHP function :phpfunction:`mb_strlen`
will be used to calculate the length of the string. The value of the ``charset``
-option is passed as the second argument to that function.
\ No newline at end of file
+option is passed as the second argument to that function.
diff --git a/reference/constraints/NotBlank.rst b/reference/constraints/NotBlank.rst
index 23d34cefa49..bcb6628f0b0 100644
--- a/reference/constraints/NotBlank.rst
+++ b/reference/constraints/NotBlank.rst
@@ -25,13 +25,17 @@ were not blank, you could do the following:
.. code-block:: yaml
- properties:
- firstName:
- - NotBlank: ~
+ # src/BlogBundle/Resources/config/validation.yml
+ Acme\BlogBundle\Entity\Author:
+ properties:
+ firstName:
+ - NotBlank: ~
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Author
@@ -51,6 +55,22 @@ were not blank, you could do the following:
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('firstName', new Assert\NotBlank());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/NotNull.rst b/reference/constraints/NotNull.rst
index a525a434df0..1bd63d7cc55 100644
--- a/reference/constraints/NotNull.rst
+++ b/reference/constraints/NotNull.rst
@@ -25,13 +25,17 @@ were not strictly equal to ``null``, you would:
.. code-block:: yaml
- properties:
- firstName:
- - NotNull: ~
+ # src/BlogBundle/Resources/config/validation.yml
+ Acme\BlogBundle\Entity\Author:
+ properties:
+ firstName:
+ - NotNull: ~
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Author
@@ -51,6 +55,22 @@ were not strictly equal to ``null``, you would:
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('firstName', new Assert\NotNull());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/Null.rst b/reference/constraints/Null.rst
index 97c04e741c5..0d34cb6311d 100644
--- a/reference/constraints/Null.rst
+++ b/reference/constraints/Null.rst
@@ -55,6 +55,22 @@ of an ``Author`` class exactly equal to ``null``, you could do the following:
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('firstName', Assert\Null());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/Regex.rst b/reference/constraints/Regex.rst
index b277756122d..d3f02426685 100644
--- a/reference/constraints/Regex.rst
+++ b/reference/constraints/Regex.rst
@@ -59,6 +59,24 @@ characters at the beginning of your string:
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('description', new Assert\Regex(array(
+ 'pattern' => '/^\w+/',
+ )));
+ }
+ }
+
Alternatively, you can set the `match`_ option to ``false`` in order to assert
that a given string does *not* match. In the following example, you'll assert
that the ``firstName`` field does not contain any numbers and give it a custom
@@ -138,4 +156,4 @@ message
**type**: ``string`` **default**: ``This value is not valid``
-This is the message that will be shown if this validator fails.
\ No newline at end of file
+This is the message that will be shown if this validator fails.
diff --git a/reference/constraints/Time.rst b/reference/constraints/Time.rst
index 3c4e9d09564..82267074381 100644
--- a/reference/constraints/Time.rst
+++ b/reference/constraints/Time.rst
@@ -55,6 +55,22 @@ of the day when the event starts:
+ .. code-block:: php
+
+ // src/Acme/EventBundle/Entity/Event.php
+ namespace Acme\EventBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Event
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('startsAt', new Assert\Time());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/True.rst b/reference/constraints/True.rst
index 77813414b7c..556997c0aa0 100644
--- a/reference/constraints/True.rst
+++ b/reference/constraints/True.rst
@@ -55,6 +55,8 @@ Then you can constrain this method with ``True``.
.. code-block:: php-annotations
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Author
@@ -72,25 +74,20 @@ Then you can constrain this method with ``True``.
.. code-block:: xml
-
-
-
-
-
-
-
-
The token is invalid...
-
-
-
-
+
+
+
+
The token is invalid...
+
+
+
.. code-block:: php
// src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\True;
diff --git a/reference/constraints/Type.rst b/reference/constraints/Type.rst
index 073fe11206a..cf2024c6236 100644
--- a/reference/constraints/Type.rst
+++ b/reference/constraints/Type.rst
@@ -58,6 +58,25 @@ Basic Usage
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('age', new Assert\Type(array(
+ 'type' => 'integer',
+ 'message' => 'The value {{ value }} is not a valid {{ type }}.',
+ )));
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/UniqueEntity.rst b/reference/constraints/UniqueEntity.rst
index 4820291fb92..442e9b39b7c 100644
--- a/reference/constraints/UniqueEntity.rst
+++ b/reference/constraints/UniqueEntity.rst
@@ -30,6 +30,8 @@ table:
.. code-block:: php-annotations
// Acme/UserBundle/Entity/User.php
+ namespace Acme\UserBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
diff --git a/reference/constraints/Url.rst b/reference/constraints/Url.rst
index 8f1fce898c2..2b6614f59d9 100644
--- a/reference/constraints/Url.rst
+++ b/reference/constraints/Url.rst
@@ -29,18 +29,18 @@ Basic Usage
.. code-block:: php-annotations
- // src/Acme/BlogBundle/Entity/Author.php
- namespace Acme\BlogBundle\Entity;
-
- use Symfony\Component\Validator\Constraints as Assert;
-
- class Author
- {
- /**
- * @Assert\Url()
- */
- protected $bioUrl;
- }
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ /**
+ * @Assert\Url()
+ */
+ protected $bioUrl;
+ }
.. code-block:: xml
@@ -51,6 +51,22 @@ Basic Usage
+ .. code-block:: php
+
+ // src/Acme/BlogBundle/Entity/Author.php
+ namespace Acme\BlogBundle\Entity;
+
+ use Symfomy\Component\Validator\Mapping\ClassMetadata;
+ use Symfony\Component\Validator\Constraints as Assert;
+
+ class Author
+ {
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
+ {
+ $metadata->addPropertyConstraint('bioUrl', new Assert\Url());
+ }
+ }
+
Options
-------
diff --git a/reference/constraints/Valid.rst b/reference/constraints/Valid.rst
index a0d88b14399..f8c897311eb 100644
--- a/reference/constraints/Valid.rst
+++ b/reference/constraints/Valid.rst
@@ -85,7 +85,9 @@ an ``Address`` instance in the ``$address`` property.
.. code-block:: php-annotations
- // src/Acme/HelloBundle/Address.php
+ // src/Acme/HelloBundle/Entity/Address.php
+ namespace Acme\HelloBundle\Entity;
+
use Symfony\Component\Validator\Constraints as Assert;
class Address
@@ -102,7 +104,9 @@ an ``Address`` instance in the ``$address`` property.
protected $zipCode;
}
- // src/Acme/HelloBundle/Author.php
+ // src/Acme/HelloBundle/Entity/Author.php
+ namespace Acme\HelloBundle\Entity;
+
class Author
{
/**
@@ -121,7 +125,9 @@ an ``Address`` instance in the ``$address`` property.
.. code-block:: php
- // src/Acme/HelloBundle/Address.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;
@@ -140,7 +146,9 @@ an ``Address`` instance in the ``$address`` property.
}
}
- // 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\NotBlank;
use Symfony\Component\Validator\Constraints\MinLength;