Skip to content

Commit bf2abd6

Browse files
committed
Merge pull request symfony#583 from dsyph3r/validation_chapter
Validation chapter
2 parents 87c92f1 + 35dc397 commit bf2abd6

File tree

1 file changed

+102
-26
lines changed

1 file changed

+102
-26
lines changed

book/validation.rst

Lines changed: 102 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ annotations if you're using the annotation method to specify your constraints:
270270
// app/config/config.php
271271
$container->loadFromExtension('framework', array('validation' => array(
272272
'enable_annotations' => true,
273-
));
273+
)));
274274
275275
.. index::
276276
single: Validation; Constraints
@@ -367,7 +367,7 @@ constraint, have several configuration options available. Suppose that the
367367
$metadata->addPropertyConstraint('gender', new Choice(array(
368368
'choices' => array('male', 'female'),
369369
'message' => 'Choose a valid gender.',
370-
));
370+
)));
371371
}
372372
}
373373
@@ -609,30 +609,106 @@ constraints.
609609
For example, suppose you have a ``User`` class, which is used both when a
610610
user registers and when a user updates his/her contact information later::
611611

612-
// src/Acme/BlogBundle/Entity/User.php
613-
namespace Acme\BlogBundle\Entity;
614-
615-
use Symfony\Component\Security\Core\User\UserInterface
616-
use Symfony\Component\Validator\Constraints as Assert;
617-
618-
class User implements UserInterface
619-
{
620-
/**
621-
* @Assert\Email(groups={"registration"})
622-
*/
623-
private $email;
624-
625-
/**
626-
* @Assert\NotBlank(groups={"registration"})
627-
* @Assert\MinLength(limit=7, groups={"registration"})
628-
*/
629-
private $password;
630-
631-
/**
632-
* @Assert\MinLength(2)
633-
*/
634-
private $city;
635-
}
612+
.. configuration-block::
613+
614+
.. code-block:: yaml
615+
616+
# src/Acme/BlogBundle/Resources/config/validation.yml
617+
Acme\BlogBundle\Entity\User:
618+
properties:
619+
email:
620+
- Email: { groups: [registration] }
621+
password:
622+
- NotBlank: { groups: [registration] }
623+
- MinLength: { limit: 7, groups: [registration] }
624+
city:
625+
- MinLength: 2
626+
627+
.. code-block:: xml
628+
629+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
630+
<class name="Acme\BlogBundle\Entity\User">
631+
<property name="email">
632+
<constraint name="Email">
633+
<option name="groups">
634+
<value>registration</value>
635+
</option>
636+
</constraint>
637+
</property>
638+
<property name="password">
639+
<constraint name="NotBlank">
640+
<option name="groups">
641+
<value>registration</value>
642+
</option>
643+
</constraint>
644+
<constraint name="MinLength">
645+
<option name="limit">7</option>
646+
<option name="groups">
647+
<value>registration</value>
648+
</option>
649+
</constraint>
650+
</property>
651+
<property name="city">
652+
<constraint name="MinLength">7</constraint>
653+
</property>
654+
</class>
655+
656+
.. code-block:: php-annotations
657+
658+
// src/Acme/BlogBundle/Entity/User.php
659+
namespace Acme\BlogBundle\Entity;
660+
661+
use Symfony\Component\Security\Core\User\UserInterface
662+
use Symfony\Component\Validator\Constraints as Assert;
663+
664+
class User implements UserInterface
665+
{
666+
/**
667+
* @Assert\Email(groups={"registration"})
668+
*/
669+
private $email;
670+
671+
/**
672+
* @Assert\NotBlank(groups={"registration"})
673+
* @Assert\MinLength(limit=7, groups={"registration"})
674+
*/
675+
private $password;
676+
677+
/**
678+
* @Assert\MinLength(2)
679+
*/
680+
private $city;
681+
}
682+
683+
.. code-block:: php
684+
685+
// src/Acme/BlogBundle/Entity/User.php
686+
namespace Acme\BlogBundle\Entity;
687+
688+
use Symfony\Component\Validator\Mapping\ClassMetadata;
689+
use Symfony\Component\Validator\Constraints\Email;
690+
use Symfony\Component\Validator\Constraints\NotBlank;
691+
use Symfony\Component\Validator\Constraints\MinLength;
692+
693+
class User
694+
{
695+
public static function loadValidatorMetadata(ClassMetadata $metadata)
696+
{
697+
$metadata->addPropertyConstraint('email', new Email(array(
698+
'groups' => array('registration')
699+
)));
700+
701+
$metadata->addPropertyConstraint('password', new NotBlank(array(
702+
'groups' => array('registration')
703+
)));
704+
$metadata->addPropertyConstraint('password', new MinLength(array(
705+
'limit' => 7,
706+
'groups' => array('registration')
707+
)));
708+
709+
$metadata->addPropertyConstraint('city', new MinLength(3));
710+
}
711+
}
636712
637713
With this configuration, there are two validation groups:
638714

0 commit comments

Comments
 (0)