@@ -12,12 +12,14 @@ The component provides "validation constraints", which are simple objects
12
12
containing the rules for the validation. Let's validate a simple string
13
13
as an example:
14
14
15
- use Symfony\Component\Validator\Validation;
16
- use Symfony\Component\Validator\Constraints\Length;
15
+ ``` php
16
+ use Symfony\Component\Validator\Validation;
17
+ use Symfony\Component\Validator\Constraints\Length;
17
18
18
- $validator = Validation::createValidator();
19
+ $validator = Validation::createValidator();
19
20
20
- $violations = $validator->validateValue('Bernhard', new Length(array('min' => 10)));
21
+ $violations = $validator->validateValue('Bernhard', new Length(array('min' => 10)));
22
+ ```
21
23
22
24
This validation will fail because the given string is shorter than ten
23
25
characters. The precise errors, here called "constraint violations", are
@@ -26,24 +28,26 @@ If the violation list is empty, validation succeeded.
26
28
27
29
Validation of arrays is possible using the ` Collection ` constraint:
28
30
29
- use Symfony\Component\Validator\Validation;
30
- use Symfony\Component\Validator\Constraints as Assert;
31
+ ``` php
32
+ use Symfony\Component\Validator\Validation;
33
+ use Symfony\Component\Validator\Constraints as Assert;
31
34
32
- $validator = Validation::createValidator();
35
+ $validator = Validation::createValidator();
33
36
34
- $constraint = new Assert\Collection(array(
35
- 'name' => new Assert\Collection(array(
36
- 'first_name' => new Assert\Length(array('min' => 101)),
37
- 'last_name' => new Assert\Length(array('min' => 1)),
38
- )),
39
- 'email' => new Assert\Email(),
40
- 'simple' => new Assert\Length(array('min' => 102)),
41
- 'gender' => new Assert\Choice(array(3, 4)),
42
- 'file' => new Assert\File(),
43
- 'password' => new Assert\Length(array('min' => 60)),
44
- ));
37
+ $constraint = new Assert\Collection(array(
38
+ 'name' => new Assert\Collection(array(
39
+ 'first_name' => new Assert\Length(array('min' => 101)),
40
+ 'last_name' => new Assert\Length(array('min' => 1)),
41
+ )),
42
+ 'email' => new Assert\Email(),
43
+ 'simple' => new Assert\Length(array('min' => 102)),
44
+ 'gender' => new Assert\Choice(array(3, 4)),
45
+ 'file' => new Assert\File(),
46
+ 'password' => new Assert\Length(array('min' => 60)),
47
+ ));
45
48
46
- $violations = $validator->validateValue($input, $constraint);
49
+ $violations = $validator->validateValue($input, $constraint);
50
+ ```
47
51
48
52
Again, the validator returns the list of violations.
49
53
@@ -52,45 +56,47 @@ a mapping you can put constraints onto properties and objects of classes.
52
56
Whenever an object of this class is validated, its properties and
53
57
method results are matched against the constraints.
54
58
55
- use Symfony\Component\Validator\Validation;
56
- use Symfony\Component\Validator\Constraints as Assert;
59
+ ``` php
60
+ use Symfony\Component\Validator\Validation;
61
+ use Symfony\Component\Validator\Constraints as Assert;
62
+
63
+ class User
64
+ {
65
+ /**
66
+ * @Assert\Length(min = 3)
67
+ * @Assert\NotBlank
68
+ */
69
+ private $name;
70
+
71
+ /**
72
+ * @Assert\Email
73
+ * @Assert\NotBlank
74
+ */
75
+ private $email;
76
+
77
+ public function __construct($name, $email)
78
+ {
79
+ $this->name = $name;
80
+ $this->email = $email;
81
+ }
57
82
58
- class User
83
+ /**
84
+ * @Assert\True(message = "The user should have a Google Mail account")
85
+ */
86
+ public function isGmailUser()
59
87
{
60
- /**
61
- * @Assert\Length(min = 3)
62
- * @Assert\NotBlank
63
- */
64
- private $name;
65
-
66
- /**
67
- * @Assert\Email
68
- * @Assert\NotBlank
69
- */
70
- private $email;
71
-
72
- public function __construct($name, $email)
73
- {
74
- $this->name = $name;
75
- $this->email = $email;
76
- }
77
-
78
- /**
79
- * @Assert\True(message = "The user should have a Google Mail account")
80
- */
81
- public function isGmailUser()
82
- {
83
- return false !== strpos($this->email, '@gmail.com');
84
- }
88
+ return false !== strpos($this->email, '@gmail.com');
85
89
}
90
+ }
86
91
87
- $validator = Validation::createValidatorBuilder()
88
- ->enableAnnotationMapping()
89
- ->getValidator();
92
+ $validator = Validation::createValidatorBuilder()
93
+ ->enableAnnotationMapping()
94
+ ->getValidator();
90
95
91
- $user = new User('John Doe', '[email protected] ');
96
+ $user = new User('John Doe', '
[email protected] ');
92
97
93
- $violations = $validator->validate($user);
98
+ $violations = $validator->validate($user);
99
+ ```
94
100
95
101
This example uses the annotation support of Doctrine Common to
96
102
map constraints to properties and methods. You can also map constraints
0 commit comments