Skip to content

Commit 07a344c

Browse files
mickaelandrieuweaverryan
authored andcommitted
add example on how to use the static method loader
1 parent 220f194 commit 07a344c

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

components/validator/builtin_validators.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
single: Validator; Built In validators
33

44
Built In validators
5-
===================
5+
===================
6+
7+
For each validation rule, the component ships a Constraint class and its associated Validator class.
8+
The Constraint object describes the rule to check and the Validator implementation runs the validation logic.

components/validator/resources.rst

+19-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,25 @@ method of the Validator builder::
3434
->getValidator();
3535

3636
Now, the retrieved ``Validator`` tries to find the ``loadValidatorMetadata()``
37-
method of the class to validate to load its metadata.
37+
method of the class to validate to load its metadata::
38+
39+
use Symfony\Component\Validator\Mapping\ClassMetadata;
40+
use Symfony\Component\Validator\Constraints\NotBlank;
41+
use Symfony\Component\Validator\Constraints\Length;
42+
43+
class User
44+
{
45+
protected $name;
46+
47+
public static function loadValidatorMatadata(ClassMetadata $metadata)
48+
{
49+
$metadata->addPropertyConstraint('name', new NotBlank());
50+
$metadata->addPropertyConstraint('name', new Length(array(
51+
'min' => 5,
52+
'max' => 20,
53+
)));
54+
}
55+
}
3856

3957
.. tip::
4058

0 commit comments

Comments
 (0)