|
1 |
| -.. index:: |
2 |
| - single: Validator; Metadata |
3 |
| - |
4 |
| -Metadata |
5 |
| -======== |
6 |
| - |
7 |
| -The :class:`Symfony\\Component\\Validator\\Mapping\\ClassMetadata` class |
8 |
| -represents and manages all the configured constraints on a given class. |
9 |
| - |
10 |
| -Properties |
11 |
| ----------- |
12 |
| - |
13 |
| -The Validator component can validate public, protected or private properties. |
14 |
| -The following example shows how to validate that the ``$firstName`` property of |
15 |
| -the ``Author`` class has at least 3 characters:: |
16 |
| - |
17 |
| - // ... |
18 |
| - use Symfony\Component\Validator\Mapping\ClassMetadata; |
19 |
| - use Symfony\Component\Validator\Constraints as Assert; |
20 |
| - |
21 |
| - class Author |
22 |
| - { |
23 |
| - private $firstName; |
24 |
| - |
25 |
| - public static function loadValidatorMetadata(ClassMetadata $metadata) |
26 |
| - { |
27 |
| - $metadata->addPropertyConstraint('firstName', new Assert\NotBlank()); |
28 |
| - $metadata->addPropertyConstraint( |
29 |
| - 'firstName', |
30 |
| - new Assert\Length(array("min" => 3)) |
31 |
| - ); |
32 |
| - } |
33 |
| - } |
34 |
| - |
35 |
| -Getters |
36 |
| -------- |
37 |
| - |
38 |
| -Constraints can also be applied to the value returned by any public *getter* |
39 |
| -method, which are the methods whose names start with ``get``, ``has`` or ``is``. |
40 |
| -This feature allows to validate your objects dynamically. |
41 |
| - |
42 |
| -Suppose that, for security reasons, you want to validate that a password field |
43 |
| -doesn't match the first name of the user. First, create a public method called |
44 |
| -``isPasswordSafe()`` to define this custom validation logic:: |
45 |
| - |
46 |
| - public function isPasswordSafe() |
47 |
| - { |
48 |
| - return $this->firstName !== $this->password; |
49 |
| - } |
50 |
| - |
51 |
| -Then, add the Validator component configuration to the class:: |
52 |
| - |
53 |
| - // ... |
54 |
| - use Symfony\Component\Validator\Mapping\ClassMetadata; |
55 |
| - use Symfony\Component\Validator\Constraints as Assert; |
56 |
| - |
57 |
| - class Author |
58 |
| - { |
59 |
| - public static function loadValidatorMetadata(ClassMetadata $metadata) |
60 |
| - { |
61 |
| - $metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue(array( |
62 |
| - 'message' => 'The password cannot match your first name', |
63 |
| - ))); |
64 |
| - } |
65 |
| - } |
66 |
| - |
67 |
| -Classes |
68 |
| -------- |
69 |
| - |
70 |
| -Some constraints allow to validate the entire object. For example, the |
71 |
| -:doc:`Callback </reference/constraints/Callback>` constraint is a generic |
72 |
| -constraint that's applied to the class itself. |
| 1 | +.. index:: |
| 2 | + single: Validator; Metadata |
| 3 | + |
| 4 | +Metadata |
| 5 | +======== |
| 6 | + |
| 7 | +The :class:`Symfony\\Component\\Validator\\Mapping\\ClassMetadata` class |
| 8 | +represents and manages all the configured constraints on a given class. |
| 9 | + |
| 10 | +Properties |
| 11 | +---------- |
| 12 | + |
| 13 | +The Validator component can validate public, protected or private properties. |
| 14 | +The following example shows how to validate that the ``$firstName`` property of |
| 15 | +the ``Author`` class has at least 3 characters:: |
| 16 | + |
| 17 | + // ... |
| 18 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 19 | + use Symfony\Component\Validator\Constraints as Assert; |
| 20 | + |
| 21 | + class Author |
| 22 | + { |
| 23 | + private $firstName; |
| 24 | + |
| 25 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 26 | + { |
| 27 | + $metadata->addPropertyConstraint('firstName', new Assert\NotBlank()); |
| 28 | + $metadata->addPropertyConstraint( |
| 29 | + 'firstName', |
| 30 | + new Assert\Length(array("min" => 3)) |
| 31 | + ); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | +Getters |
| 36 | +------- |
| 37 | + |
| 38 | +Constraints can also be applied to the value returned by any public *getter* |
| 39 | +method, which are the methods whose names start with ``get``, ``has`` or ``is``. |
| 40 | +This feature allows to validate your objects dynamically. |
| 41 | + |
| 42 | +Suppose that, for security reasons, you want to validate that a password field |
| 43 | +doesn't match the first name of the user. First, create a public method called |
| 44 | +``isPasswordSafe()`` to define this custom validation logic:: |
| 45 | + |
| 46 | + public function isPasswordSafe() |
| 47 | + { |
| 48 | + return $this->firstName !== $this->password; |
| 49 | + } |
| 50 | + |
| 51 | +Then, add the Validator component configuration to the class:: |
| 52 | + |
| 53 | + // ... |
| 54 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 55 | + use Symfony\Component\Validator\Constraints as Assert; |
| 56 | + |
| 57 | + class Author |
| 58 | + { |
| 59 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 60 | + { |
| 61 | + $metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue(array( |
| 62 | + 'message' => 'The password cannot match your first name', |
| 63 | + ))); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | +Classes |
| 68 | +------- |
| 69 | + |
| 70 | +Some constraints allow to validate the entire object. For example, the |
| 71 | +:doc:`Callback </reference/constraints/Callback>` constraint is a generic |
| 72 | +constraint that's applied to the class itself. |
0 commit comments