Skip to content

Commit 1e69e63

Browse files
javiereguiluzweaverryan
authored andcommitted
Finished the first version of the Validator documentation
1 parent da02759 commit 1e69e63

File tree

8 files changed

+67
-115
lines changed

8 files changed

+67
-115
lines changed

components/validator/builtin_validators.rst

Lines changed: 0 additions & 8 deletions
This file was deleted.

components/validator/custom_validation.rst

Lines changed: 0 additions & 5 deletions
This file was deleted.

components/validator/index.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,3 @@ Validator
77
introduction
88
resources
99
metadata
10-
builtin_validators
11-
validation_groups
12-
custom_validation
13-
internationalization

components/validator/internationalization.rst

Lines changed: 0 additions & 5 deletions
This file was deleted.

components/validator/introduction.rst

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ The Validator Component
66
=======================
77

88
The Validator component provides tools to validate values following the
9-
`JSR-303 Bean Validation specification`_. With the component, this is done in two parts:
10-
* ``Contraints``: a constraint describes a rule that need to be validated
11-
* ``Validators``: a list of classes that implement the validation logic for common usages
9+
`JSR-303 Bean Validation specification`_.
1210

1311
Installation
1412
------------
@@ -23,15 +21,18 @@ You can install the component in 2 different ways:
2321
Usage
2422
-----
2523

26-
The Validator component allows you to use very advanced validation rules, but
27-
it is also really easy to do easy validation tasks. For instance, if you want
28-
to validate that a string is at least 10 character long, the only code you need is::
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 classees 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::
2931

3032
use Symfony\Component\Validator\Validation;
3133
use Symfony\Component\Validator\Constraints\Length;
3234

3335
$validator = Validation::createValidator();
34-
3536
$violations = $validator->validateValue('Bernhard', new Length(array('min' => 10)));
3637

3738
if (0 !== count($violations)) {
@@ -45,40 +46,29 @@ Retrieving a Validator Instance
4546
-------------------------------
4647

4748
The :class:`Symfony\\Component\\Validator\\Validator` class is the main access
48-
point of the Validator component. To create a new instance of this class, it
49-
is recommended to use the :class:`Symfony\\Component\\Validator\\Validation`
50-
class.
51-
52-
You can get a very basic ``Validator`` by calling
53-
:method:`Validation::createValidator() <Symfony\\Component\\Validator\\Validation::createValidator>`::
49+
point of the Validator component. To create a new instance of this class, it's
50+
recommended to use the :class:`Symfony\\Component\\Validator\\Validation` class::
5451

5552
use Symfony\Component\Validator\Validation;
5653

5754
$validator = Validation::createValidator();
5855

59-
The created validator can be used to validate strings, arrays, numbers, but it
60-
can't validate classes. In order to achieve that, you have to configure the ``Validator``
61-
class. To do that, you can use the :class:`Symfony\\Component\\Validator\\ValidatorBuilder`.
62-
This class can be retrieved by using the
63-
:method:`Validation::createValidatorBuilder() <Symfony\\Component\\Validator\\Validation::createValidatorBuilder>`
64-
method::
56+
This ``$validator`` object can validate simple variables such as strings, numbers
57+
and arrays, but it can't validate objects. To do so, use the
58+
:class:`Symfony\\Component\\Validator\\ValidatorBuilder` class to configure the
59+
``Validator`` class::
6560

6661
use Symfony\Component\Validator\Validation;
6762

6863
$validator = Validation::createValidatorBuilder()
6964
// ... build a custom instance of the Validator
7065
->getValidator();
7166

72-
In the next sections, you'll learn about all things you can configure in the Validator.
73-
74-
Sections
75-
--------
67+
In the next sections, you'll learn about all the validator features that you
68+
can configure:
7669

7770
* :doc:`/components/validator/resources`
78-
* :doc:`/components/validator/builtin_validators`
79-
* :doc:`/components/validator/validation_groups`
80-
* :doc:`/components/validator/internationalization`
81-
* :doc:`/components/validator/custom_validation`
71+
* :doc:`/components/validator/metadata`
8272

8373
.. _`JSR-303 Bean Validation specification`: http://jcp.org/en/jsr/detail?id=303
8474
.. _Packagist: https://packagist.org/packages/symfony/validator

components/validator/metadata.rst

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
Metadata
55
========
66

7-
The :class:`Symfony\\Component\\Validator\\Mapping\\ClassMetadata` class represents and manages all the configured constraints on a given class.
7+
The :class:`Symfony\\Component\\Validator\\Mapping\\ClassMetadata` class
8+
represents and manages all the configured constraints on a given class.
89

910
Properties
1011
----------
1112

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::
13+
The Validator component can validate public, protected or private properties.
14+
The following example shows how to validate that the ``$firstName`` property of
15+
the ``Author`` class has at least 3 characters::
1616

1717
// ...
1818
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -35,16 +35,20 @@ class to have at least 3 characters::
3535
Getters
3636
-------
3737

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".
38+
Constraints can also be applied to the value returned by any public *getter*
39+
method, which are the methods whose names start with ``get`` or ``is``. This
40+
feature allows to validate your objects dynamically.
4241

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``::
42+
Suppose that, for security reasons, you want to validate that a password field
43+
doesn't match the first name of the user. First, create a public method called
44+
``isPasswordSafe`` to define this custom validation logic::
45+
46+
public function isPasswordSafe()
47+
{
48+
return $this->firstName !== $this->password;
49+
}
50+
51+
Then, add the Validator component configuration to the class::
4852

4953
// ...
5054
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -54,31 +58,22 @@ this method must return ``true``::
5458
{
5559
public static function loadValidatorMetadata(ClassMetadata $metadata)
5660
{
57-
$metadata->addGetterConstraint('passwordLegal', new Assert\True(array(
61+
$metadata->addGetterConstraint('passwordSafe', new Assert\True(array(
5862
'message' => 'The password cannot match your first name',
5963
)));
6064
}
6165
}
6266

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-
7067
.. note::
7168

7269
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
70+
(``get`` or ``is``) is omitted in the mapping. This allows you to move the
7471
constraint to a property with the same name later (or vice versa) without
7572
changing your validation logic.
7673

7774
Classes
7875
-------
7976

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.
77+
Some constraints allow to validate the entire object. For example, the
78+
:doc:`Callback </reference/constraints/Callback>` constraint is a generic
79+
constraint that's applied to the class itself.

components/validator/resources.rst

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
Loading Resources
55
=================
66

7-
The Validator uses metadata to validate a value. This metadata defines how a
8-
class, array or any other value should be validated. When validating a class,
9-
each class contains its own specific metadata. When validating another value,
10-
the metadata must be passed to the validate methods.
7+
The Validator component uses metadata to validate a value. This metadata defines
8+
how a class, array or any other value should be validated. When validating a
9+
class, the metadata is defined by the class itself. When validating simple values,
10+
the metadata must be passed to the validation methods.
1111

12-
Class metadata should be defined somewhere in a configuration file, or in the
13-
class itself. The ``Validator`` needs to be able to retrieve this metadata
14-
from the file or class. To do that, it uses a set of loaders.
12+
Class metadata can be defined in a configuration file or in the class itself.
13+
The Validator component retrieves that metadata using a set of loaders.
1514

1615
.. seealso::
1716

@@ -22,19 +21,19 @@ The StaticMethodLoader
2221

2322
The most basic loader is the
2423
:class:`Symfony\\Component\\Validator\\Mapping\\Loader\\StaticMethodLoader`.
25-
This loader will call a static method of the class in order to get the
26-
metadata for that class. The name of the method is configured using the
24+
This loader gets the metadata by calling a static method of the class. The name
25+
of the method is configured using the
2726
:method:`Symfony\\Component\\Validator\\ValidatorBuilder::addMethodMapping`
28-
method of the Validator builder::
27+
method of the validator builder::
2928

3029
use Symfony\Component\Validator\Validation;
3130

3231
$validator = Validation::createValidatorBuilder()
3332
->addMethodMapping('loadValidatorMetadata')
3433
->getValidator();
3534

36-
Now, the retrieved ``Validator`` tries to find the ``loadValidatorMetadata()``
37-
method of the class to validate to load its metadata::
35+
In this example, the validation metadata is retrieved executing the
36+
``loadValidatorMetadata()`` method of the class::
3837

3938
use Symfony\Component\Validator\Mapping\ClassMetadata;
4039
use Symfony\Component\Validator\Constraints as Assert;
@@ -55,15 +54,14 @@ method of the class to validate to load its metadata::
5554

5655
.. tip::
5756

58-
You can call this method multiple times to add multiple supported method
59-
names. You can also use
60-
:method:`Symfony\\Component\\Validator\\ValidatorBuilder::addMethodMappings`
57+
You can call this method multiple times to add several method names. You can
58+
also use :method:`Symfony\\Component\\Validator\\ValidatorBuilder::addMethodMappings`
6159
to set an array of supported method names.
6260

6361
The FileLoaders
6462
---------------
6563

66-
The component also provides 2 file loaders, one to load Yaml files and one to
64+
The component also provides two file loaders, one to load YAML files and one to
6765
load XML files. Use
6866
:method:`Symfony\\Component\\Validator\\ValidatorBuilder::addYamlMapping` or
6967
:method:`Symfony\\Component\\Validator\\ValidatorBuilder::addXmlMapping` to
@@ -91,10 +89,9 @@ The AnnotationLoader
9189
--------------------
9290

9391
At last, the component provides an
94-
:class:`Symfony\\Component\\Validator\\Mapping\\Loader\\AnnotationLoader`.
95-
This loader uses an annotation reader to parse the annotations of a class.
96-
Annotations are placed in doc block comments (``/** ... */``) and start with an
97-
``@``. For instance::
92+
:class:`Symfony\\Component\\Validator\\Mapping\\Loader\\AnnotationLoader` to get
93+
the metadata from the annotations of the class. Annotations are defined as ``@``
94+
prefixed classes included in doc block comments (``/** ... */``). For example::
9895

9996
use Symfony\Component\Validator\Constraints as Assert;
10097
// ...
@@ -128,8 +125,7 @@ Using Multiple Loaders
128125

129126
The component provides a
130127
:class:`Symfony\\Component\\Validator\\Mapping\\Loader\\LoaderChain` class to
131-
chain multiple loaders. This means you can configure as many loaders as you
132-
want at the same time.
128+
execute several loaders sequentially in the same order they were defined:
133129

134130
The ``ValidatorBuilder`` will already take care of this when you configure
135131
multiple mappings::
@@ -145,12 +141,10 @@ multiple mappings::
145141
Caching
146142
-------
147143

148-
Using many loaders to load metadata from different places is very easy when
149-
creating the metadata, but it can easily slow down your application since each
150-
file needs to be parsed, validated and converted to a
151-
:class:`Symfony\\Component\\Validator\\Mapping\\ClassMetadata` instance. To
152-
solve this problem, you can configure a cacher which will be used to cache
153-
the ``ClassMetadata`` after it was loaded.
144+
Using many loaders to load metadata from different places is convenient, but it
145+
can slow down your application because each file needs to be parsed, validated
146+
and converted to a :class:`Symfony\\Component\\Validator\\Mapping\\ClassMetadata`
147+
instance. To solve this problem, you can cache the ``ClassMetadata`` information.
154148

155149
The Validator component comes with an
156150
:class:`Symfony\\Component\\Validator\\Mapping\\Cache\\ApcCache`
@@ -165,9 +159,9 @@ implements :class:`Symfony\\Component\\Validator\\Mapping\\Cache\\CacheInterface
165159
the Validator still needs to merge all metadata of one class from every
166160
loader when it is requested.
167161

168-
To set a cacher, call the
169-
:method:`Symfony\\Component\\Validator\\ValidatorBuilder::setMetadataCache` of
170-
the Validator builder::
162+
Enable the cache calling the
163+
:method:`Symfony\\Component\\Validator\\ValidatorBuilder::setMetadataCache`
164+
method of the Validator builder::
171165

172166
use Symfony\Component\Validator\Validation;
173167
use Symfony\Component\Validator\Mapping\Cache\ApcCache;
@@ -180,7 +174,7 @@ the Validator builder::
180174
Using a Custom MetadataFactory
181175
------------------------------
182176

183-
All loaders and the cacher are passed to an instance of
177+
All the loaders and the cache are passed to an instance of
184178
:class:`Symfony\\Component\\Validator\\Mapping\\ClassMetadataFactory`. This
185179
class is responsible for creating a ``ClassMetadata`` instance from all the
186180
configured resources.
@@ -201,7 +195,7 @@ this custom implementation using
201195
.. caution::
202196

203197
Since you are using a custom metadata factory, you can't configure loaders
204-
and cachers using the ``add*Mapping()`` methods anymore. You now have to
198+
and caches using the ``add*Mapping()`` methods anymore. You now have to
205199
inject them into your custom metadata factory yourself.
206200

207201
.. _Packagist: https://packagist.org

components/validator/validation_groups.rst

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)