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
198c74fc
Commit
198c74fc
authored
Jun 15, 2012
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enhanced documentation for validators
parent
31bbe3e5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
122 additions
and
24 deletions
+122
-24
doc/providers/validator.rst
doc/providers/validator.rst
+122
-24
No files found.
doc/providers/validator.rst
View file @
198c74fc
...
@@ -52,7 +52,7 @@ Usage
...
@@ -52,7 +52,7 @@ Usage
The Validator provider provides a ``validator`` service.
The Validator provider provides a ``validator`` service.
Validating
v
alues
Validating
V
alues
~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~
You can validate values directly using the ``validateValue`` validator
You can validate values directly using the ``validateValue`` validator
...
@@ -60,50 +60,148 @@ method::
...
@@ -60,50 +60,148 @@ method::
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints as Assert;
$app->get('/validate
-url', function (
) use ($app) {
$app->get('/validate
/{email}', function ($email
) use ($app) {
$
violations = $app['validator']->validateValue($app['request']->get('url'), new Assert\Ur
l());
$
errors = $app['validator']->validateValue($email, new Assert\Emai
l());
return $violations;
if (count($errors) > 0) {
return (string) $errors;
} else {
return 'The email is valid';
}
});
});
This is relatively limited.
Validating Arrays
~~~~~~~~~~~~~~~~~
use Symfony\Component\Validator\Constraints as Assert;
class Book
{
public $title;
public $author;
}
class Author
{
public $first_name;
public $last_name;
}
$book = array(
'title' => 'My Book',
'author' => array(
'first_name' => 'Fabien',
'last_name' => 'Potencier',
),
);
$constraint = new Assert\Collection(array(
'title' => new Assert\MinLength(10),
'author' => new Assert\Collection(array(
'first_name' => array(new Assert\NotBlank(), new Assert\MinLength(10)),
'last_name' => new Assert\MinLength(10),
)),
));
$errors = $app['validator']->validateValue($book, $constraint);
if (count($errors) > 0) {
foreach ($errors as $error) {
echo $error->getPropertyPath().' '.$error->getMessage()."\n";
}
} else {
echo 'The book is valid';
}
Validating Objects
~~~~~~~~~~~~~~~~~~
Validating object properties
If you want to add validations to a class, you can define the constraint for
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the class properties and getters, and then call the ``validate`` method::
use Symfony\Component\Validator\Constraints as Assert;
If you want to add validations to a class, you can implement a static
$author = new Author();
``loadValidatorMetadata`` method as described under *Services*. This allows
$author->first_name = 'Fabien';
you to define constraints for your object properties. It also works with
$author->last_name = 'Potencier';
getters::
$book = new Book();
$book->title = 'My Book';
$book->author = $author;
$metadata = $app['validator.mapping.class_metadata_factory']->getClassMetadata('Author');
$metadata->addPropertyConstraint('first_name', new Assert\NotBlank());
$metadata->addPropertyConstraint('first_name', new Assert\MinLength(10));
$metadata->addPropertyConstraint('last_name', new Assert\MinLength(10));
$metadata = $app['validator.mapping.class_metadata_factory']->getClassMetadata('Book');
$metadata->addPropertyConstraint('title', new Assert\MinLength(10));
$metadata->addPropertyConstraint('author', new Assert\Valid());
$errors = $app['validator']->validate($book);
if (count($errors) > 0) {
foreach ($errors as $error) {
echo $error->getPropertyPath().' '.$error->getMessage()."\n";
}
} else {
echo 'The author is valid';
}
You can also declare the class constraint by adding a static
``loadValidatorMetadata`` method to your classes::
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints as Assert;
class
Post
class
Book
{
{
public $title;
public $title;
public $
body
;
public $
author
;
static public function loadValidatorMetadata(ClassMetadata $metadata)
static public function loadValidatorMetadata(ClassMetadata $metadata)
{
{
$metadata->addPropertyConstraint('title', new Assert\NotNull());
$metadata->addPropertyConstraint('title', new Assert\MinLength(10));
$metadata->addPropertyConstraint('title', new Assert\NotBlank());
$metadata->addPropertyConstraint('author', new Assert\Valid());
$metadata->addPropertyConstraint('body', new Assert\MinLength(array('limit' => 10)));
}
}
}
}
$app->post('/posts/new', function () use ($app) {
class Author
$post = new Post();
{
$post->title = $app['request']->get('title')
;
public $first_name
;
$post->body = $app['request']->get('body')
;
public $last_name
;
$violations = $app['validator']->validate($post);
static public function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('first_name', new Assert\NotBlank());
$metadata->addPropertyConstraint('first_name', new Assert\MinLength(10));
$metadata->addPropertyConstraint('last_name', new Assert\MinLength(10));
}
}
$app->get('/validate/{email}', function ($email) use ($app) {
$author = new Author();
$author->first_name = 'Fabien';
$author->last_name = 'Potencier';
$book = new Book();
$book->title = 'My Book';
$book->author = $author;
return $violations;
$errors = $app['validator']->validate($book);
if (count($errors) > 0) {
foreach ($errors as $error) {
echo $error->getPropertyPath().' '.$error->getMessage()."\n";
}
} else {
echo 'The author is valid';
}
});
});
You will have to handle the display of these violations yourself. You can
.. note::
however use the *FormServiceProvider* which can make use of the *ValidatorServiceProvider*.
Use ``addGetterConstraint()`` to add constraints on getter methods and
``addConstraint()`` to add constraints on the class itself.
Translation
Translation
~~~~~~~~~~~
~~~~~~~~~~~
...
...
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