Skip to content

Commit da02759

Browse files
mickaelandrieuweaverryan
authored andcommitted
started to migrate metadata part from book to component docs
1 parent e30d521 commit da02759

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

components/validator/metadata.rst

+80-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,83 @@
22
single: Validator; Metadata
33

44
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. Validation component
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+
// ...
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 return value of a method. Symfony
39+
allows you to add a constraint to any public method whose name starts with
40+
"get" or "is". In this guide, both of these types of methods are referred
41+
to as "getters".
42+
43+
The benefit of this technique is that it allows you to validate your object
44+
dynamically. For example, suppose you want to make sure that a password field
45+
doesn't match the first name of the user (for security reasons). You can
46+
do this by creating an ``isPasswordLegal`` method, and then asserting that
47+
this method must return ``true``::
48+
49+
// ...
50+
use Symfony\Component\Validator\Mapping\ClassMetadata;
51+
use Symfony\Component\Validator\Constraints as Assert;
52+
53+
class Author
54+
{
55+
public static function loadValidatorMetadata(ClassMetadata $metadata)
56+
{
57+
$metadata->addGetterConstraint('passwordLegal', new Assert\True(array(
58+
'message' => 'The password cannot match your first name',
59+
)));
60+
}
61+
}
62+
63+
Now, create the ``isPasswordLegal()`` method and include the logic you need::
64+
65+
public function isPasswordLegal()
66+
{
67+
return $this->firstName !== $this->password;
68+
}
69+
70+
.. note::
71+
72+
The keen-eyed among you will have noticed that the prefix of the getter
73+
("get" or "is") is omitted in the mapping. This allows you to move the
74+
constraint to a property with the same name later (or vice versa) without
75+
changing your validation logic.
76+
77+
Classes
78+
-------
79+
80+
Some constraints apply to the entire class being validated. For example,
81+
the :doc:`Callback </reference/constraints/Callback>` constraint is a generic
82+
constraint that's applied to the class itself. When that class is validated,
83+
methods specified by that constraint are simply executed so that each can
84+
provide more custom validation.

components/validator/resources.rst

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ At last, the component provides an
9595
This loader uses an annotation reader to parse the annotations of a class.
9696
Annotations are placed in doc block comments (``/** ... */``) and start with an
9797
``@``. For instance::
98+
9899
use Symfony\Component\Validator\Constraints as Assert;
99100
// ...
100101

0 commit comments

Comments
 (0)