Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 102 additions & 26 deletions book/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ annotations if you're using the annotation method to specify your constraints:
// app/config/config.php
$container->loadFromExtension('framework', array('validation' => array(
'enable_annotations' => true,
));
)));

.. index::
single: Validation; Constraints
Expand Down Expand Up @@ -367,7 +367,7 @@ constraint, have several configuration options available. Suppose that the
$metadata->addPropertyConstraint('gender', new Choice(array(
'choices' => array('male', 'female'),
'message' => 'Choose a valid gender.',
));
)));
}
}

Expand Down Expand Up @@ -609,30 +609,106 @@ constraints.
For example, suppose you have a ``User`` class, which is used both when a
user registers and when a user updates his/her contact information later::

// src/Acme/BlogBundle/Entity/User.php
namespace Acme\BlogBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface
use Symfony\Component\Validator\Constraints as Assert;

class User implements UserInterface
{
/**
* @Assert\Email(groups={"registration"})
*/
private $email;

/**
* @Assert\NotBlank(groups={"registration"})
* @Assert\MinLength(limit=7, groups={"registration"})
*/
private $password;

/**
* @Assert\MinLength(2)
*/
private $city;
}
.. configuration-block::

.. code-block:: yaml

# src/Acme/BlogBundle/Resources/config/validation.yml
Acme\BlogBundle\Entity\User:
properties:
email:
- Email: { groups: [registration] }
password:
- NotBlank: { groups: [registration] }
- MinLength: { limit: 7, groups: [registration] }
city:
- MinLength: 2

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\User">
<property name="email">
<constraint name="Email">
<option name="groups">
<value>registration</value>
</option>
</constraint>
</property>
<property name="password">
<constraint name="NotBlank">
<option name="groups">
<value>registration</value>
</option>
</constraint>
<constraint name="MinLength">
<option name="limit">7</option>
<option name="groups">
<value>registration</value>
</option>
</constraint>
</property>
<property name="city">
<constraint name="MinLength">7</constraint>
</property>
</class>

.. code-block:: php-annotations

// src/Acme/BlogBundle/Entity/User.php
namespace Acme\BlogBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface
use Symfony\Component\Validator\Constraints as Assert;

class User implements UserInterface
{
/**
* @Assert\Email(groups={"registration"})
*/
private $email;

/**
* @Assert\NotBlank(groups={"registration"})
* @Assert\MinLength(limit=7, groups={"registration"})
*/
private $password;

/**
* @Assert\MinLength(2)
*/
private $city;
}

.. code-block:: php

// src/Acme/BlogBundle/Entity/User.php
namespace Acme\BlogBundle\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\MinLength;

class User
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('email', new Email(array(
'groups' => array('registration')
)));

$metadata->addPropertyConstraint('password', new NotBlank(array(
'groups' => array('registration')
)));
$metadata->addPropertyConstraint('password', new MinLength(array(
'limit' => 7,
'groups' => array('registration')
)));

$metadata->addPropertyConstraint('city', new MinLength(3));
}
}

With this configuration, there are two validation groups:

Expand Down