|
1 | 1 | Validator Component
|
2 | 2 | ===================
|
3 | 3 |
|
4 |
| -This component is based on the JSR-303 Bean Validation specification and |
5 |
| -enables specifying validation rules for classes using XML, YAML, PHP or |
6 |
| -annotations, which can then be checked against instances of these classes. |
7 |
| - |
8 |
| -Usage |
9 |
| ------ |
10 |
| - |
11 |
| -The component provides "validation constraints", which are simple objects |
12 |
| -containing the rules for the validation. Let's validate a simple string |
13 |
| -as an example: |
14 |
| - |
15 |
| -```php |
16 |
| -use Symfony\Component\Validator\Validation; |
17 |
| -use Symfony\Component\Validator\Constraints\Length; |
18 |
| - |
19 |
| -$validator = Validation::createValidator(); |
20 |
| - |
21 |
| -$violations = $validator->validate('Bernhard', new Length(array('min' => 10))); |
22 |
| -``` |
23 |
| - |
24 |
| -This validation will fail because the given string is shorter than ten |
25 |
| -characters. The precise errors, here called "constraint violations", are |
26 |
| -returned by the validator. You can analyze these or return them to the user. |
27 |
| -If the violation list is empty, validation succeeded. |
28 |
| - |
29 |
| -Validation of arrays is possible using the `Collection` constraint: |
30 |
| - |
31 |
| -```php |
32 |
| -use Symfony\Component\Validator\Validation; |
33 |
| -use Symfony\Component\Validator\Constraints as Assert; |
34 |
| - |
35 |
| -$validator = Validation::createValidator(); |
36 |
| - |
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 |
| -)); |
48 |
| - |
49 |
| -$violations = $validator->validate($input, $constraint); |
50 |
| -``` |
51 |
| - |
52 |
| -Again, the validator returns the list of violations. |
53 |
| - |
54 |
| -Validation of objects is possible using "constraint mapping". With such |
55 |
| -a mapping you can put constraints onto properties and objects of classes. |
56 |
| -Whenever an object of this class is validated, its properties and |
57 |
| -method results are matched against the constraints. |
58 |
| - |
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 |
| - } |
82 |
| - |
83 |
| - /** |
84 |
| - * @Assert\IsTrue(message = "The user should have a Google Mail account") |
85 |
| - */ |
86 |
| - public function isGmailUser() |
87 |
| - { |
88 |
| - return false !== strpos($this->email, '@gmail.com'); |
89 |
| - } |
90 |
| -} |
91 |
| - |
92 |
| -$validator = Validation::createValidatorBuilder() |
93 |
| - ->enableAnnotationMapping() |
94 |
| - ->getValidator(); |
95 |
| - |
96 |
| -$user = new User('John Doe', ' [email protected]'); |
97 |
| - |
98 |
| -$violations = $validator->validate($user); |
99 |
| -``` |
100 |
| - |
101 |
| -This example uses the annotation support of Doctrine Common to |
102 |
| -map constraints to properties and methods. You can also map constraints |
103 |
| -using XML, YAML or plain PHP, if you dislike annotations or don't want |
104 |
| -to include Doctrine. Check the documentation for more information about |
105 |
| -these drivers. |
| 4 | +The Validator component provides tools to validate values following the |
| 5 | +[JSR-303 Bean Validation specification][1]. |
106 | 6 |
|
107 | 7 | Resources
|
108 | 8 | ---------
|
109 | 9 |
|
110 |
| -Silex integration: |
111 |
| - |
112 |
| -https://github.com/silexphp/Silex/blob/master/src/Silex/Provider/ValidatorServiceProvider.php |
113 |
| - |
114 |
| -Documentation: |
115 |
| - |
116 |
| -https://symfony.com/doc/2.7/book/validation.html |
117 |
| - |
118 |
| -JSR-303 Specification: |
119 |
| - |
120 |
| -http://jcp.org/en/jsr/detail?id=303 |
121 |
| - |
122 |
| -You can run the unit tests with the following command: |
| 10 | + * [Documentation](https://symfony.com/doc/current/book/validation.html) |
| 11 | + * [Contributing](https://symfony.com/doc/current/contributing/index.html) |
| 12 | + * [Report issues](https://github.com/symfony/symfony/issues) and |
| 13 | + [send Pull Requests](https://github.com/symfony/symfony/pulls) |
| 14 | + in the [main Symfony repository](https://github.com/symfony/symfony) |
123 | 15 |
|
124 |
| - $ cd path/to/Symfony/Component/Validator/ |
125 |
| - $ composer install |
126 |
| - $ phpunit |
| 16 | +[1]: http://jcp.org/en/jsr/detail?id=303 |
0 commit comments