44Loading 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
2322The 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
6361The 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
6765load 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
9391At 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
129126The 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
134130The ``ValidatorBuilder `` will already take care of this when you configure
135131multiple mappings::
@@ -145,12 +141,10 @@ multiple mappings::
145141Caching
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
155149The 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::
180174Using 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
185179class is responsible for creating a ``ClassMetadata `` instance from all the
186180configured 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
0 commit comments