4
4
Loading Resources
5
5
=================
6
6
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.
11
11
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.
15
14
16
15
.. seealso ::
17
16
@@ -22,19 +21,19 @@ The StaticMethodLoader
22
21
23
22
The most basic loader is the
24
23
: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
27
26
:method: `Symfony\\ Component\\ Validator\\ ValidatorBuilder::addMethodMapping `
28
- method of the Validator builder::
27
+ method of the validator builder::
29
28
30
29
use Symfony\Component\Validator\Validation;
31
30
32
31
$validator = Validation::createValidatorBuilder()
33
32
->addMethodMapping('loadValidatorMetadata')
34
33
->getValidator();
35
34
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::
38
37
39
38
use Symfony\Component\Validator\Mapping\ClassMetadata;
40
39
use Symfony\Component\Validator\Constraints as Assert;
@@ -55,15 +54,14 @@ method of the class to validate to load its metadata::
55
54
56
55
.. tip ::
57
56
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 `
61
59
to set an array of supported method names.
62
60
63
61
The FileLoaders
64
62
---------------
65
63
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
67
65
load XML files. Use
68
66
:method: `Symfony\\ Component\\ Validator\\ ValidatorBuilder::addYamlMapping ` or
69
67
:method: `Symfony\\ Component\\ Validator\\ ValidatorBuilder::addXmlMapping ` to
@@ -91,10 +89,9 @@ The AnnotationLoader
91
89
--------------------
92
90
93
91
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::
98
95
99
96
use Symfony\Component\Validator\Constraints as Assert;
100
97
// ...
@@ -128,8 +125,7 @@ Using Multiple Loaders
128
125
129
126
The component provides a
130
127
: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:
133
129
134
130
The ``ValidatorBuilder `` will already take care of this when you configure
135
131
multiple mappings::
@@ -145,12 +141,10 @@ multiple mappings::
145
141
Caching
146
142
-------
147
143
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.
154
148
155
149
The Validator component comes with an
156
150
:class: `Symfony\\ Component\\ Validator\\ Mapping\\ Cache\\ ApcCache `
@@ -165,9 +159,9 @@ implements :class:`Symfony\\Component\\Validator\\Mapping\\Cache\\CacheInterface
165
159
the Validator still needs to merge all metadata of one class from every
166
160
loader when it is requested.
167
161
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::
171
165
172
166
use Symfony\Component\Validator\Validation;
173
167
use Symfony\Component\Validator\Mapping\Cache\ApcCache;
@@ -180,7 +174,7 @@ the Validator builder::
180
174
Using a Custom MetadataFactory
181
175
------------------------------
182
176
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
184
178
:class: `Symfony\\ Component\\ Validator\\ Mapping\\ ClassMetadataFactory `. This
185
179
class is responsible for creating a ``ClassMetadata `` instance from all the
186
180
configured resources.
@@ -201,7 +195,7 @@ this custom implementation using
201
195
.. caution ::
202
196
203
197
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
205
199
inject them into your custom metadata factory yourself.
206
200
207
201
.. _Packagist : https://packagist.org
0 commit comments