From 72a6d59266859764764ef148d4acd44087b86dfa Mon Sep 17 00:00:00 2001 From: WouterJ Date: Mon, 31 Dec 2012 17:17:41 +0100 Subject: [PATCH 1/3] Added PHP config format --- reference/constraints/All.rst | 23 +++++++++++++++++++++-- reference/constraints/Blank.rst | 14 ++++++++++++++ reference/constraints/Callback.rst | 16 ++++++++++++++++ reference/constraints/Choice.rst | 4 ++-- reference/constraints/Collection.rst | 10 ++++------ reference/constraints/Country.rst | 14 ++++++++++++++ reference/constraints/Date.rst | 14 ++++++++++++++ reference/constraints/DateTime.rst | 14 ++++++++++++++ reference/constraints/Email.rst | 19 +++++++++++++++++++ reference/constraints/False.rst | 14 ++++++++++++++ reference/constraints/File.rst | 6 +----- reference/constraints/Ip.rst | 16 ++++++++++++++++ reference/constraints/Language.rst | 16 ++++++++++++++++ reference/constraints/Locale.rst | 16 ++++++++++++++++ reference/constraints/Max.rst | 19 ++++++++++++++++++- reference/constraints/MinLength.rst | 17 +++++++++++++++++ reference/constraints/NotBlank.rst | 14 ++++++++++++++ reference/constraints/NotNull.rst | 14 ++++++++++++++ reference/constraints/Null.rst | 16 ++++++++++++++++ reference/constraints/Regex.rst | 18 ++++++++++++++++++ reference/constraints/Time.rst | 16 ++++++++++++++++ reference/constraints/Type.rst | 19 +++++++++++++++++++ reference/constraints/Url.rst | 16 ++++++++++++++++ 23 files changed, 329 insertions(+), 16 deletions(-) diff --git a/reference/constraints/All.rst b/reference/constraints/All.rst index 4601d443ffa..96a59f29cbd 100644 --- a/reference/constraints/All.rst +++ b/reference/constraints/All.rst @@ -58,14 +58,33 @@ entry in that array: + .. code-block:: php + + // src/Acme/UserBundle/Enttiy/User.php + 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..56140b6df7f 100644 --- a/reference/constraints/Blank.rst +++ b/reference/constraints/Blank.rst @@ -52,6 +52,20 @@ of an ``Author`` class were blank, you could do the following: + .. code-block:: php + + // src/Acme/BlogBundle/Entity/Author.php + 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..6434a2232db 100644 --- a/reference/constraints/Callback.rst +++ b/reference/constraints/Callback.rst @@ -63,6 +63,22 @@ Setup + .. code-block:: php + + // src/Acme/BlogBundle/Entity/Author.php + 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..49d72d06e32 100644 --- a/reference/constraints/Choice.rst +++ b/reference/constraints/Choice.rst @@ -78,7 +78,7 @@ If your valid choice list is simple, you can pass them in directly via the // src/Acme/BlogBundle/EntityAuthor.php use Symfony\Component\Validator\Mapping\ClassMetadata; - use Symfony\Component\Validator\Constraints\Choice; + use Symfony\Component\Validator\Constraints as Assert; class Author { @@ -86,7 +86,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', ))); diff --git a/reference/constraints/Collection.rst b/reference/constraints/Collection.rst index 8013166b7c7..ae970df51b6 100644 --- a/reference/constraints/Collection.rst +++ b/reference/constraints/Collection.rst @@ -120,9 +120,7 @@ blank but is no longer than 100 characters in length, you would do the following // src/Acme/BlogBundle/Entity/Author.php 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 +128,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..ba643c3c7e8 100644 --- a/reference/constraints/Country.rst +++ b/reference/constraints/Country.rst @@ -50,6 +50,20 @@ Basic Usage + .. code-block:: php + + // src/Acme/UserBundle/Entity/User.php + 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..49e86eadb72 100644 --- a/reference/constraints/Date.rst +++ b/reference/constraints/Date.rst @@ -50,6 +50,20 @@ Basic Usage + .. code-block:: php + + // src/Acme/BlogBundle/Entity/Author.php + 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..1787a2bd976 100644 --- a/reference/constraints/DateTime.rst +++ b/reference/constraints/DateTime.rst @@ -52,6 +52,20 @@ Basic Usage + .. code-block:: php + + // src/Acme/BlogBundle/Entity/Author.php + 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..288f03f7983 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 ------- diff --git a/reference/constraints/False.rst b/reference/constraints/False.rst index ada5c9c3173..c62ee84984f 100644 --- a/reference/constraints/False.rst +++ b/reference/constraints/False.rst @@ -75,6 +75,20 @@ method returns **false**: } } + .. code-block:: php + + // src/Acme/BlogBundle/Entity/Author.php + 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..3ac515b5474 100644 --- a/reference/constraints/File.rst +++ b/reference/constraints/File.rst @@ -116,15 +116,11 @@ below a certain file size and a valid PDF, add the following: .. code-block:: php // src/Acme/BlogBundle/Entity/Author.php - // ... - 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 +227,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..01c05adc0e4 100644 --- a/reference/constraints/Ip.rst +++ b/reference/constraints/Ip.rst @@ -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..d56b4f64342 100644 --- a/reference/constraints/Language.rst +++ b/reference/constraints/Language.rst @@ -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..b3080ca8215 100644 --- a/reference/constraints/Locale.rst +++ b/reference/constraints/Locale.rst @@ -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..11323e33387 100644 --- a/reference/constraints/Max.rst +++ b/reference/constraints/Max.rst @@ -56,6 +56,23 @@ add the following: + .. code-block:: php + + // src/Acme/EventBundle/Entity/Participant.php + 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 +98,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..0dd11a0927c 100644 --- a/reference/constraints/MinLength.rst +++ b/reference/constraints/MinLength.rst @@ -56,6 +56,23 @@ Basic Usage + .. code-block:: php + + // src/Acme/BlogBundle/Entity/Blog.php + 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 ------- diff --git a/reference/constraints/NotBlank.rst b/reference/constraints/NotBlank.rst index 23d34cefa49..5ec6675bcc3 100644 --- a/reference/constraints/NotBlank.rst +++ b/reference/constraints/NotBlank.rst @@ -51,6 +51,20 @@ were not blank, you could do the following: + .. code-block:: php + + // src/Acme/BlogBundle/Entity/Author.php + 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..2f8cb80d31b 100644 --- a/reference/constraints/NotNull.rst +++ b/reference/constraints/NotNull.rst @@ -51,6 +51,20 @@ were not strictly equal to ``null``, you would: + .. code-block:: php + + // src/Acme/BlogBundle/Entity/Author.php + 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..825f7ebc1b1 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 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/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/Url.rst b/reference/constraints/Url.rst index 8f1fce898c2..a03a3888a2e 100644 --- a/reference/constraints/Url.rst +++ b/reference/constraints/Url.rst @@ -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 ------- From aad266861b7e44e188914b78b9de718b29e78dd7 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Mon, 31 Dec 2012 17:18:25 +0100 Subject: [PATCH 2/3] Fixed spacing issues --- reference/constraints/Choice.rst | 2 +- reference/constraints/Country.rst | 22 ++++++++++------------ reference/constraints/Email.rst | 2 +- reference/constraints/Ip.rst | 24 ++++++++++++------------ reference/constraints/Language.rst | 24 ++++++++++++------------ reference/constraints/Locale.rst | 24 ++++++++++++------------ reference/constraints/MinLength.rst | 2 +- reference/constraints/Regex.rst | 2 +- reference/constraints/Url.rst | 24 ++++++++++++------------ 9 files changed, 62 insertions(+), 64 deletions(-) diff --git a/reference/constraints/Choice.rst b/reference/constraints/Choice.rst index 49d72d06e32..24ec78d8ef1 100644 --- a/reference/constraints/Choice.rst +++ b/reference/constraints/Choice.rst @@ -280,4 +280,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/Country.rst b/reference/constraints/Country.rst index ba643c3c7e8..2642a558094 100644 --- a/reference/constraints/Country.rst +++ b/reference/constraints/Country.rst @@ -28,18 +28,16 @@ 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 + use Symfony\Component\Validator\Constraints as Assert; + + class User + { + /** + * @Assert\Country + */ + protected $country; + } .. code-block:: xml diff --git a/reference/constraints/Email.rst b/reference/constraints/Email.rst index 288f03f7983..06a97bbed22 100644 --- a/reference/constraints/Email.rst +++ b/reference/constraints/Email.rst @@ -102,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/Ip.rst b/reference/constraints/Ip.rst index 01c05adc0e4..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 diff --git a/reference/constraints/Language.rst b/reference/constraints/Language.rst index d56b4f64342..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 diff --git a/reference/constraints/Locale.rst b/reference/constraints/Locale.rst index b3080ca8215..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 diff --git a/reference/constraints/MinLength.rst b/reference/constraints/MinLength.rst index 0dd11a0927c..a0c3473ec06 100644 --- a/reference/constraints/MinLength.rst +++ b/reference/constraints/MinLength.rst @@ -99,4 +99,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/Regex.rst b/reference/constraints/Regex.rst index 825f7ebc1b1..d3f02426685 100644 --- a/reference/constraints/Regex.rst +++ b/reference/constraints/Regex.rst @@ -156,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/Url.rst b/reference/constraints/Url.rst index a03a3888a2e..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 From 9cabf0df7098a47e28515000b90f9c7446c17d6e Mon Sep 17 00:00:00 2001 From: WouterJ Date: Mon, 31 Dec 2012 17:50:25 +0100 Subject: [PATCH 3/3] Added namespaces to examples --- reference/constraints/All.rst | 32 ++++++++++++++------------ reference/constraints/Blank.rst | 12 +++++++--- reference/constraints/Callback.rst | 4 ++++ reference/constraints/Choice.rst | 4 ++++ reference/constraints/Collection.rst | 28 +++++++++++++--------- reference/constraints/Country.rst | 4 ++++ reference/constraints/Date.rst | 4 ++++ reference/constraints/DateTime.rst | 2 ++ reference/constraints/False.rst | 4 ++++ reference/constraints/File.rst | 4 ++++ reference/constraints/Max.rst | 4 ++++ reference/constraints/MaxLength.rst | 22 +++++++++++++++++- reference/constraints/Min.rst | 4 +++- reference/constraints/MinLength.rst | 4 ++++ reference/constraints/NotBlank.rst | 12 +++++++--- reference/constraints/NotNull.rst | 12 +++++++--- reference/constraints/True.rst | 25 +++++++++----------- reference/constraints/UniqueEntity.rst | 2 ++ reference/constraints/Valid.rst | 16 +++++++++---- 19 files changed, 144 insertions(+), 55 deletions(-) diff --git a/reference/constraints/All.rst b/reference/constraints/All.rst index 96a59f29cbd..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 @@ -69,6 +69,8 @@ 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; diff --git a/reference/constraints/Blank.rst b/reference/constraints/Blank.rst index 56140b6df7f..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 @@ -55,6 +59,8 @@ 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; diff --git a/reference/constraints/Callback.rst b/reference/constraints/Callback.rst index 6434a2232db..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; /** @@ -66,6 +68,8 @@ 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; diff --git a/reference/constraints/Choice.rst b/reference/constraints/Choice.rst index 24ec78d8ef1..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,6 +79,8 @@ 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 as Assert; diff --git a/reference/constraints/Collection.rst b/reference/constraints/Collection.rst index ae970df51b6..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,6 +123,8 @@ 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 as Assert; diff --git a/reference/constraints/Country.rst b/reference/constraints/Country.rst index 2642a558094..cc5ec176fb0 100644 --- a/reference/constraints/Country.rst +++ b/reference/constraints/Country.rst @@ -29,6 +29,8 @@ 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 @@ -51,6 +53,8 @@ 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; diff --git a/reference/constraints/Date.rst b/reference/constraints/Date.rst index 49e86eadb72..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 @@ -53,6 +55,8 @@ 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; diff --git a/reference/constraints/DateTime.rst b/reference/constraints/DateTime.rst index 1787a2bd976..91a590d57bb 100644 --- a/reference/constraints/DateTime.rst +++ b/reference/constraints/DateTime.rst @@ -55,6 +55,8 @@ 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; diff --git a/reference/constraints/False.rst b/reference/constraints/False.rst index c62ee84984f..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 @@ -78,6 +80,8 @@ 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; diff --git a/reference/constraints/File.rst b/reference/constraints/File.rst index 3ac515b5474..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,6 +118,8 @@ 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; diff --git a/reference/constraints/Max.rst b/reference/constraints/Max.rst index 11323e33387..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 @@ -59,6 +61,8 @@ 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; 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 a0c3473ec06..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 @@ -59,6 +61,8 @@ 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; diff --git a/reference/constraints/NotBlank.rst b/reference/constraints/NotBlank.rst index 5ec6675bcc3..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 @@ -54,6 +58,8 @@ 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; diff --git a/reference/constraints/NotNull.rst b/reference/constraints/NotNull.rst index 2f8cb80d31b..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 @@ -54,6 +58,8 @@ 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; 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 - - - - - - - - - - - - + + + + + + + .. 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/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/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;