-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[WIP] Documenting the Validator component #3710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,8 @@ Usage | |
----- | ||
|
||
The Validator component allows you to use very advanced validation rules, but | ||
it is also really easy to do very minor validation. For instance, if you want | ||
to validate a string against a specific length, the only code you need is:: | ||
it is also really easy to do easy validation tasks. For instance, if you want | ||
to validate a string is at least 10 character long, the only code you need is:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn´t it be validate that a string...? |
||
|
||
use Symfony\Component\Validator\Validation; | ||
use Symfony\Component\Validator\Constraints\Length; | ||
|
@@ -31,7 +31,7 @@ to validate a string against a specific length, the only code you need is:: | |
$violations = $validator->validateValue('Bernhard', new Length(array('min' => 10))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be good to put an example that fails to show some more detail on the output There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The string is 8 characters long, while the constraints expects a minimum of 10 character. So it already fails. However, I agree that we should put some "show error" logic in here (I copied this example from the README). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, it's already in there.... |
||
|
||
if (0 !== count($violations)) { | ||
// there are errors, let's show them | ||
// there are errors, now you can show them | ||
foreach ($violations as $violation) { | ||
echo $violation->getMessage().'<br>'; | ||
} | ||
|
@@ -42,19 +42,20 @@ Retrieving a Validator Instance | |
|
||
The :class:`Symfony\\Component\\Validator\\Validator` class is the main access | ||
point of the Validator component. To create a new instance of this class, it | ||
is recomment to use the :class:`Symfony\\Component\Validator\Validation` | ||
is recommend to use the :class:`Symfony\\Component\Validator\Validation` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is recommended to use |
||
class. | ||
|
||
You can get the ``Validator`` with the default configuration by calling | ||
You can get a very basic ``Validator`` by calling | ||
:method:`Validation::createValidator() <Symfony\\Component\\Validator\\Validation::createValidator>`:: | ||
|
||
use Symfony\Component\Validator\Validation; | ||
|
||
$validator = Validation::createValidator(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please provide a tip saying how it is done in symfony or maybe the service factory is shared but each time it creates a validator or how it is done, would be a good complement. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe let's also mention what the deps that configure a validator are: public function __construct(
MetadataFactoryInterface $metadataFactory,
ConstraintValidatorFactoryInterface $validatorFactory,
TranslatorInterface $translator,
$translationDomain = 'validators',
array $objectInitializers = array()
) |
||
|
||
However, a lot of things can be customized. To configure the ``Validator`` | ||
class, you can use the :class:`Symfony\\Component\\Validator\\ValidatorBuilder`. | ||
This class can be retrieved by using the | ||
The created validator can be used to validate strings, array, numbers, but it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. arrays instead of array |
||
can't validate classes. To be able to do that, you have to configure the ``Validator`` | ||
class. To do that, you can use the :class:`Symfony\\Component\\Validator\\ValidatorBuilder`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To do that is said twice... I am not native but what about something like "In order to achieve that" |
||
This class can be retrieved by using the | ||
:method:`Validation::createValidatorBuilder() <Symfony\\Component\\Validator\\Validation::createValidatorBuilder>` | ||
method:: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ Loading Resources | |
The Validator uses metadata to validate a value. This metadata defines how a | ||
class, array or any other value should be validated. When validating a class, | ||
each class contains its own specific metadata. When validating another value, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The metadata can also be passed to the validate methods There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not when validating a class |
||
the metadata to passed to the validate methods. | ||
the metadata must be passed to the validate methods. | ||
|
||
Class metadata should be defined somewhere in a configuration file, or in the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should -> can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well, it should be defined in a configuration file or the object isn't validated :) |
||
class itself. The ``Validator`` needs to be able to retrieve this metadata | ||
|
@@ -20,7 +20,7 @@ from the file or class. To do that, it uses a set of loaders. | |
The StaticMethodLoader | ||
---------------------- | ||
|
||
The easiest loader is the | ||
The most basic loader is the | ||
:class:`Symfony\\Component\\Validator\\Mapping\\Loader\\StaticMethodLoader`. | ||
This loader will call a static method of the class in order to get the | ||
metadata for that class. The name of the method is configured using the | ||
|
@@ -34,7 +34,7 @@ method of the Validator builder:: | |
->getValidator(); | ||
|
||
Now, the retrieved ``Validator`` tries to find the ``loadValidatorMetadata()`` | ||
method of the validated class to load its metadata. | ||
method of the class to validate to load its metadata. | ||
|
||
.. tip:: | ||
|
||
|
@@ -70,8 +70,9 @@ The AnnotationLoader | |
|
||
At last, the component provides an | ||
:class:`Symfony\\Component\\Validator\\Mapping\\Loader\\AnnotationLoader`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe let's add a note that it works together with the reader and the loader just loads the annotations but does not read them, for that it needs that collaborator |
||
This loader will parse the annotations of a class. Annotations are placed in | ||
PHPdoc comments (`/** ... */`) and start with an ``@``. For instance:: | ||
This loader uses an annotation reader to parse the annotations of a class. | ||
Annotations are placed in doc block comments (`/** ... */`) and start with an | ||
``@``. For instance:: | ||
|
||
// ... | ||
|
||
|
@@ -97,7 +98,7 @@ To disable the annotation loader after it was enabled, call | |
.. note:: | ||
|
||
In order to use the annotation loader, you should have installed the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence sounds weird to me, I would say you should have xxx and yyy packages installed from Packagist, but again I am not native :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I'm not a native either, but the current thing sounds better to me. What's the correct way @weaverryan ? :) |
||
``doctrine/annotations`` and ``doctrine/cache`` packages of Packagist. | ||
``doctrine/annotations`` and ``doctrine/cache`` packages from Packagist. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i wonder if the autoload.php for some projects is still needed to load the annotation reader, or if any comment should be made here, just to ensure or avoid some pitfalls for not being able to make it work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea! |
||
Using Multiple Loaders | ||
---------------------- | ||
|
@@ -121,9 +122,9 @@ multiple mappings:: | |
Caching | ||
------- | ||
|
||
Using many loaders to load metadata from different places is very easy for the | ||
developer, but it can easily slow down your application since each file needs | ||
to be parsed, validated and converted to a | ||
Using many loaders to load metadata from different places is very easy when | ||
creating the metadata, but it can easily slow down your application since each | ||
file needs to be parsed, validated and converted to a | ||
:class:`Symfony\\Component\\Validator\\Mapping\\ClassMetadata` instance. To | ||
solve this problems, you can configure a cacher which will be used to cache | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should just be cache and no cacher but i give you the reasons sometimes the classes are not named Cacher There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. problems -> problem There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's still a cacher. We don't talk about the name here, just what it does. My name is Wouter, yet I'm still a person... In the same way, something called YamlFile can be a cacher. And using "cache" here seems like we configure the cache system behind it, strictly spoken we configure the adapter. |
||
the ``ClassMetadata`` after it was loaded. | ||
|
@@ -135,10 +136,11 @@ implements :class:`Symfony\\Component\\Validator\\Mapping\\Cache\\CacheInterface | |
|
||
.. note:: | ||
|
||
The loader already use a singleton load mechanism. That means that they | ||
will only load and parse a file once and put that in a property, which | ||
will be used on the next time. However, the Validator still needs to | ||
merge all metadata of one class from every loader when it is requested. | ||
The loaders already use a singleton load mechanism. That means that the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means instead of that means? |
||
loaders will only load and parse a file once and put that in a property, | ||
which will then be used the next time it is asked for metadata. However, | ||
the Validator still needs to merge all metadata of one class from every | ||
loader when it is requested. | ||
|
||
To set a cacher, call the | ||
:method:`Symfony\\Component\\Validator\\ValidatorBuilder::setMetadataCache` of | ||
|
@@ -176,5 +178,5 @@ this custom implementation using | |
.. caution:: | ||
|
||
Since you are using a custom metadata factory, you can't configure loaders | ||
and cachers using the helper methods anymore. You now have to inject them | ||
into your custom metadata factory yourself. | ||
and cachers using the ``add*Mapping()`` methods anymore. You now have to | ||
inject them into your custom metadata factory yourself. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say "do easy validation tasks with it"... Don´t you think the sentence is a bit weird right now?