From 8df8eb251331965c83170af6681f4a904606a594 Mon Sep 17 00:00:00 2001 From: alexmart Date: Sat, 21 May 2016 16:46:04 +0200 Subject: [PATCH 1/2] Short version example for Type constraint. --- reference/constraints/Type.rst | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/reference/constraints/Type.rst b/reference/constraints/Type.rst index abf93fe5b75..fe0fa7b54b9 100644 --- a/reference/constraints/Type.rst +++ b/reference/constraints/Type.rst @@ -19,6 +19,67 @@ option to validate this. Basic Usage ----------- +.. configuration-block:: + + .. code-block:: php-annotations + + // src/AppBundle/Entity/Author.php + namespace AppBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + /** + * @Assert\Type("integer") + */ + protected $age; + } + + .. code-block:: yaml + + # src/AppBundle/Resources/config/validation.yml + AppBundle\Entity\Author: + properties: + age: + - Type: integer + + .. code-block:: xml + + + + + + + + + integer + + + + + + .. code-block:: php + + // src/AppBundle/Entity/Author.php + namespace AppBundle\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('integer'); + } + } + +Configure more options +~~~~~~~~~~~~~~~~~~~~~~ + .. configuration-block:: .. code-block:: php-annotations From 101a129af460398784a145e4086c48fa2b2f49bc Mon Sep 17 00:00:00 2001 From: alexmart Date: Tue, 24 May 2016 12:12:16 +0200 Subject: [PATCH 2/2] Constraint examples using the default option. --- reference/constraints/Choice.rst | 18 +++++ reference/constraints/EqualTo.rst | 18 ++++- reference/constraints/GreaterThan.rst | 21 +++++- reference/constraints/GreaterThanOrEqual.rst | 19 ++++- reference/constraints/IdenticalTo.rst | 10 ++- reference/constraints/LessThan.rst | 20 +++++- reference/constraints/LessThanOrEqual.rst | 19 ++++- reference/constraints/NotEqualTo.rst | 19 ++++- reference/constraints/NotIdenticalTo.rst | 19 ++++- reference/constraints/Type.rst | 73 ++++---------------- 10 files changed, 162 insertions(+), 74 deletions(-) diff --git a/reference/constraints/Choice.rst b/reference/constraints/Choice.rst index aeeadee7a64..b4d5a9a2f40 100644 --- a/reference/constraints/Choice.rst +++ b/reference/constraints/Choice.rst @@ -45,6 +45,11 @@ If your valid choice list is simple, you can pass them in directly via the class Author { + /** + * @Assert\Choice({"New York", "Berlin", "Tokyo"}) + */ + protected $city; + /** * @Assert\Choice(choices = {"male", "female"}, message = "Choose a valid gender.") */ @@ -56,6 +61,7 @@ If your valid choice list is simple, you can pass them in directly via the # src/AppBundle/Resources/config/validation.yml AppBundle\Entity\Author: properties: + city: [New York, Berlin, Tokyo] gender: - Choice: choices: [male, female] @@ -70,6 +76,13 @@ If your valid choice list is simple, you can pass them in directly via the xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> + + + New York + Berlin + Tokyo + +