Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
7378777c
Commit
7378777c
authored
May 03, 2011
by
Igor Wiedler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[docs] Documentation for the ValidatorExtension
parent
d4b32418
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
108 additions
and
0 deletions
+108
-0
doc/extensions.rst
doc/extensions.rst
+1
-0
doc/extensions/index.rst
doc/extensions/index.rst
+1
-0
doc/extensions/validator.rst
doc/extensions/validator.rst
+106
-0
No files found.
doc/extensions.rst
View file @
7378777c
...
...
@@ -65,6 +65,7 @@ All of these are within the ``Silex\Extension`` namespace.
* :doc:`SessionExtension <extensions/session>`
* :doc:`TwigExtension <extensions/twig>`
* :doc:`UrlGeneratorExtension <extensions/url_generator>`
* :doc:`ValidatorExtension <extensions/validator>`
Creating an extension
---------------------
...
...
doc/extensions/index.rst
View file @
7378777c
...
...
@@ -9,3 +9,4 @@ Silex
session
twig
url_generator
validator
doc/extensions/validator.rst
0 → 100644
View file @
7378777c
ValidatorExtension
=====================
The *ValidatorExtension* provides a service for validating data. It is
most useful when used with the *FormExtension*, but can also be used
standalone.
Parameters
----------
* **validator.class_path** (optional): Path to where
the Symfony2 Validator component is located.
Services
--------
* **validator**: An instance of `Validator
<http://api.symfony.com/2.0/Symfony/Component/Validator/Validator.html>`_.
* **validator.mapping.class_metadata_factory**: Factory for metadata loaders,
which can read validation constraint information from classes. Defaults to
StaticMethodLoader--ClassMetadataFactory.
This means you can define a static ``loadValidatorMetadata`` method on your
data class, which takes a ClassMetadata argument. Then you can set
constraints on this ClassMetadata instance.
* **validator.validator_factory**: Factory for ConstraintValidators. Defaults
to a standard ``ConstraintValidatorFactory``. Mostly used internally by the
Validator.
Registering
-----------
Make sure you place a copy of the Symfony2 Validator component in
``vendor/symfony/src``. You can simply clone the whole Symfony2 into vendor.
::
use Silex\Extension\ValidatorExtension;
$app->register(new ValidatorExtension(), array(
'validator.class_path' => __DIR__.'/vendor/symfony/src',
));
Usage
-----
The Validator extension provides a ``validator`` service.
Validating values
~~~~~~~~~~~~~~~~~
You can validate values directly using the ``validateValue`` validator method.
::
use Symfony\Component\Validator\Constraints;
$app->get('/validate-url', function () use ($app) {
$violations = $app['validator']->validateValue($app['request']->get('url'), new Constraints\Url());
return $violations;
});
This is relatively limited.
Validating object properties
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you want to add validations to a class, you can implement a static
``loadValidatorMetadata`` method as described under *Services*. This allows
you to define constraints for your object properties. It also works with
getters.
::
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints;
class Post
{
public $title;
public $body;
static public function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('title', new Constraints\NotNull());
$metadata->addPropertyConstraint('title', new Constraints\NotBlank());
$metadata->addPropertyConstraint('body', new Constraints\MinLength(array('limit' => 10)));
}
}
$app->post('/posts/new', function () use ($app) {
$post = new Post();
$post->title = $app['request']->get('title');
$post->body = $app['request']->get('body');
$violations = $app['validator']->validate($post);
return $violations;
});
You will have to handle the display of these violations yourself. You can
however use the *FormExtension* which can make use of the *ValidatorExtension*.
For more information, consult the `Symfony2 Validation documentation
<http://symfony.com/doc/2.0/book/validation.html>`_.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment