|
2 | 2 | single: Validator; Metadata
|
3 | 3 |
|
4 | 4 | Metadata
|
5 |
| -======== |
| 5 | +======== |
| 6 | + |
| 7 | +The :class:`Symfony\\Component\\Validator\\Mapping\\ClassMetadata` class represents and manages all the configured constraints on a given class. |
| 8 | + |
| 9 | +Properties |
| 10 | +~~~~~~~~~~ |
| 11 | + |
| 12 | +Validating class properties is the most basic validation technique. Symfony |
| 13 | +allows you to validate private, protected or public properties. The next |
| 14 | +listing shows you how to configure the ``$firstName`` property of an ``Author`` |
| 15 | +class to have at least 3 characters. |
| 16 | + |
| 17 | +.. configuration-block:: |
| 18 | + |
| 19 | + .. code-block:: php-annotations |
| 20 | +
|
| 21 | + // AppBundle/Entity/Author.php |
| 22 | +
|
| 23 | + // ... |
| 24 | + use Symfony\Component\Validator\Constraints as Assert; |
| 25 | +
|
| 26 | + class Author |
| 27 | + { |
| 28 | + /** |
| 29 | + * @Assert\NotBlank() |
| 30 | + * @Assert\Length(min=3) |
| 31 | + */ |
| 32 | + private $firstName; |
| 33 | + } |
| 34 | +
|
| 35 | + .. code-block:: yaml |
| 36 | +
|
| 37 | + # src/AppBundle/Resources/config/validation.yml |
| 38 | + AppBundle\Entity\Author: |
| 39 | + properties: |
| 40 | + firstName: |
| 41 | + - NotBlank: ~ |
| 42 | + - Length: |
| 43 | + min: 3 |
| 44 | +
|
| 45 | + .. code-block:: xml |
| 46 | +
|
| 47 | + <!-- src/AppBundle/Resources/config/validation.xml --> |
| 48 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 49 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 50 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 51 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 52 | +
|
| 53 | + <class name="AppBundle\Entity\Author"> |
| 54 | + <property name="firstName"> |
| 55 | + <constraint name="NotBlank" /> |
| 56 | + <constraint name="Length"> |
| 57 | + <option name="min">3</option> |
| 58 | + </constraint> |
| 59 | + </property> |
| 60 | + </class> |
| 61 | + </constraint-mapping> |
| 62 | +
|
| 63 | + .. code-block:: php |
| 64 | +
|
| 65 | + // src/AppBundle/Entity/Author.php |
| 66 | +
|
| 67 | + // ... |
| 68 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 69 | + use Symfony\Component\Validator\Constraints as Assert; |
| 70 | +
|
| 71 | + class Author |
| 72 | + { |
| 73 | + private $firstName; |
| 74 | +
|
| 75 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 76 | + { |
| 77 | + $metadata->addPropertyConstraint('firstName', new Assert\NotBlank()); |
| 78 | + $metadata->addPropertyConstraint( |
| 79 | + 'firstName', |
| 80 | + new Assert\Length(array("min" => 3)) |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | +
|
| 85 | +.. index:: |
| 86 | + single: Validation; Getter constraints |
| 87 | + |
| 88 | +Getters |
| 89 | +~~~~~~~ |
| 90 | + |
| 91 | +Constraints can also be applied to the return value of a method. Symfony |
| 92 | +allows you to add a constraint to any public method whose name starts with |
| 93 | +"get" or "is". In this guide, both of these types of methods are referred |
| 94 | +to as "getters". |
| 95 | + |
| 96 | +The benefit of this technique is that it allows you to validate your object |
| 97 | +dynamically. For example, suppose you want to make sure that a password field |
| 98 | +doesn't match the first name of the user (for security reasons). You can |
| 99 | +do this by creating an ``isPasswordLegal`` method, and then asserting that |
| 100 | +this method must return ``true``: |
| 101 | + |
| 102 | +.. configuration-block:: |
| 103 | + |
| 104 | + .. code-block:: php-annotations |
| 105 | +
|
| 106 | + // src/AppBundle/Entity/Author.php |
| 107 | +
|
| 108 | + // ... |
| 109 | + use Symfony\Component\Validator\Constraints as Assert; |
| 110 | +
|
| 111 | + class Author |
| 112 | + { |
| 113 | + /** |
| 114 | + * @Assert\True(message = "The password cannot match your first name") |
| 115 | + */ |
| 116 | + public function isPasswordLegal() |
| 117 | + { |
| 118 | + // ... return true or false |
| 119 | + } |
| 120 | + } |
| 121 | +
|
| 122 | + .. code-block:: yaml |
| 123 | +
|
| 124 | + # src/AppBundle/Resources/config/validation.yml |
| 125 | + AppBundle\Entity\Author: |
| 126 | + getters: |
| 127 | + passwordLegal: |
| 128 | + - "True": { message: "The password cannot match your first name" } |
| 129 | +
|
| 130 | + .. code-block:: xml |
| 131 | +
|
| 132 | + <!-- src/AppBundle/Resources/config/validation.xml --> |
| 133 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 134 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 135 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 136 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 137 | +
|
| 138 | + <class name="AppBundle\Entity\Author"> |
| 139 | + <getter property="passwordLegal"> |
| 140 | + <constraint name="True"> |
| 141 | + <option name="message">The password cannot match your first name</option> |
| 142 | + </constraint> |
| 143 | + </getter> |
| 144 | + </class> |
| 145 | + </constraint-mapping> |
| 146 | +
|
| 147 | + .. code-block:: php |
| 148 | +
|
| 149 | + // src/AppBundle/Entity/Author.php |
| 150 | +
|
| 151 | + // ... |
| 152 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 153 | + use Symfony\Component\Validator\Constraints as Assert; |
| 154 | +
|
| 155 | + class Author |
| 156 | + { |
| 157 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 158 | + { |
| 159 | + $metadata->addGetterConstraint('passwordLegal', new Assert\True(array( |
| 160 | + 'message' => 'The password cannot match your first name', |
| 161 | + ))); |
| 162 | + } |
| 163 | + } |
| 164 | +
|
| 165 | +Now, create the ``isPasswordLegal()`` method and include the logic you need:: |
| 166 | + |
| 167 | + public function isPasswordLegal() |
| 168 | + { |
| 169 | + return $this->firstName !== $this->password; |
| 170 | + } |
| 171 | + |
| 172 | +.. note:: |
| 173 | + |
| 174 | + The keen-eyed among you will have noticed that the prefix of the getter |
| 175 | + ("get" or "is") is omitted in the mapping. This allows you to move the |
| 176 | + constraint to a property with the same name later (or vice versa) without |
| 177 | + changing your validation logic. |
| 178 | + |
| 179 | +.. _validation-class-target: |
| 180 | + |
| 181 | +Classes |
| 182 | +~~~~~~~ |
| 183 | + |
| 184 | +Some constraints apply to the entire class being validated. For example, |
| 185 | +the :doc:`Callback </reference/constraints/Callback>` constraint is a generic |
| 186 | +constraint that's applied to the class itself. When that class is validated, |
| 187 | +methods specified by that constraint are simply executed so that each can |
| 188 | +provide more custom validation. |
0 commit comments