|
5 | 5 | The Validator Component
|
6 | 6 | =======================
|
7 | 7 |
|
8 |
| - The Validator component provides tools to validate values. |
| 8 | + The Validator component provides tools to validate values following the |
| 9 | + `JSR-303 Bean Validation specification`_. |
9 | 10 |
|
10 | 11 | Installation
|
11 | 12 | ------------
|
12 | 13 |
|
13 |
| -You can install the component in 2 different ways: |
| 14 | +You can install the component in two different ways: |
14 | 15 |
|
15 | 16 | * :doc:`Install it via Composer </components/using_components>` (``symfony/validator`` on `Packagist`_);
|
16 |
| -* Use the official Git repository (https://github.com/symfony/validator). |
| 17 | +* Use the official Git repository (https://github.com/symfony/Validator). |
17 | 18 |
|
18 |
| -For more information, see the code in the Git Repository. |
| 19 | +.. include:: /components/require_autoload.rst.inc |
19 | 20 |
|
20 |
| -Learn more |
| 21 | +Usage |
| 22 | +----- |
| 23 | + |
| 24 | +The Validator component behavior is based on two concepts: |
| 25 | + |
| 26 | +* Contraints, which define the rules to be validated; |
| 27 | +* Validators, which are the classes that contain the actual validation logic. |
| 28 | + |
| 29 | +The following example shows how to validate that a string is at least 10 |
| 30 | +characters long:: |
| 31 | + |
| 32 | + use Symfony\Component\Validator\Validation; |
| 33 | + use Symfony\Component\Validator\Constraints\Length; |
| 34 | + use Symfony\Component\Validator\Constraints\NotBlank; |
| 35 | + |
| 36 | + $validator = Validation::createValidator(); |
| 37 | + $violations = $validator->validate('Bernhard', array( |
| 38 | + new Length(array('min' => 10)), |
| 39 | + new NotBlank(), |
| 40 | + )); |
| 41 | + |
| 42 | + if (0 !== count($violations)) { |
| 43 | + // there are errors, now you can show them |
| 44 | + foreach ($violations as $violation) { |
| 45 | + echo $violation->getMessage().'<br>'; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | +Retrieving a Validator Instance |
| 50 | +------------------------------- |
| 51 | + |
| 52 | +The :class:`Symfony\\Component\\Validator\\Validator` class is the main access |
| 53 | +point of the Validator component. To create a new instance of this class, it's |
| 54 | +recommended to use the :class:`Symfony\\Component\\Validator\\Validation` class:: |
| 55 | + |
| 56 | + use Symfony\Component\Validator\Validation; |
| 57 | + |
| 58 | + $validator = Validation::createValidator(); |
| 59 | + |
| 60 | +This ``$validator`` object can validate simple variables such as strings, numbers |
| 61 | +and arrays, but it can't validate objects. To do so, configure the |
| 62 | +``Validator`` class as explained in the next sections. |
| 63 | + |
| 64 | +Learn More |
21 | 65 | ----------
|
22 | 66 |
|
23 | 67 | .. toctree::
|
24 |
| - :maxdepth: 1 |
25 |
| - :glob: |
| 68 | + :maxdepth: 1 |
| 69 | + :glob: |
26 | 70 |
|
27 |
| - /validation |
28 |
| - /validation/* |
29 |
| - /reference/constraints/* |
| 71 | + /components/validator/* |
| 72 | + /validator |
| 73 | + /validator/* |
30 | 74 |
|
| 75 | +.. _`JSR-303 Bean Validation specification`: http://jcp.org/en/jsr/detail?id=303 |
31 | 76 | .. _Packagist: https://packagist.org/packages/symfony/validator
|
0 commit comments