Configuration of constraints
Some constraints, like NotBlank are relatively simple, while others, like Choice constraints, have many Available configuration options. Assume that the Author
class has another attribute called gender
, which can be set to "male", "female" or "other":
PHP:// src/AppBundle/Entity/Author.php // ...use Symfony\Component\Validator\Mapping\ClassMetadata;use Symfony\Component\Validator\Constraints as Assert; class Author{
public $gender; // ... public static function loadValidatorMetadata(ClassMetadata $metadata)
{
// ... $metadata->addPropertyConstraint('gender', new Assert\Choice(array(
'choices' => array('male', 'female', 'other'),
'message' => 'Choose a valid gender.',
)));
}}
XML:<!-- src/AppBundle/Resources/config/validation.xml --><?xml version="1.0" encoding="UTF-8" ?><constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> <class name="AppBundle\Entity\Author">
<property name="gender">
<constraint name="Choice">
<option name="choices">
<value>male</value>
<value>female</value>
<value>other</value>
</option>
<option name="message">Choose a valid gender.</option>
</constraint>
</property> <!-- ... -->
</class></constraint-mapping>
YAML:# src/AppBundle/Resources/config/validation.ymlAppBundle\Entity\Author:
properties:
gender:
- Choice: { choices: [male, female, other], message: Choose a valid gender. }
# ...
Annotations:// src/AppBundle/Entity/Author.php // ...use Symfony\Component\Validator\Constraints as Assert; class Author{
/**
* @Assert\Choice(
* choices = { "male", "female", "other" },
* message = "Choose a valid gender."
* )
*/
public $gender; // ...}
Constraint options can always be passed through an array. Some constraints also allow you to pass a value for the "default" option in place of this array. In Choice
constraints, choices
options can be specified in this way.
PHP:// src/AppBundle/Entity/Author.php // ...use Symfony\Component\Validator\Mapping\ClassMetadata;use Symfony\Component\Validator\Constraints as Assert; class Author{
protected $gender; public static function loadValidatorMetadata(ClassMetadata $metadata)
{
// ... $metadata->addPropertyConstraint(
'gender',
new Assert\Choice(array('male', 'female', 'other'))
);
}}
XML:<!-- src/AppBundle/Resources/config/validation.xml --><?xml version="1.0" encoding="UTF-8" ?><constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> <class name="AppBundle\Entity\Author">
<property name="gender">
<constraint name="Choice">
<value>male</value>
<value>female</value>
<value>other</value>
</constraint>
</property> <!-- ... -->
</class></constraint-mapping>
YAML:# src/AppBundle/Resources/config/validation.ymlAppBundle\Entity\Author:
properties:
gender:
- Choice: [male, female, other]
# ...
Annotations:// src/AppBundle/Entity/Author.php // ...use Symfony\Component\Validator\Constraints as Assert; class Author{
/**
* @Assert\Choice({"male", "female", "other"})
*/
protected $gender; // ...}
This is purely to make the most common configuration options easier and faster to use.
If you are not sure how to specify an option, either check the API documentation, or just to be on the safe side, pass it in through an options array (i.e. the first method above).
Goals of constraints
Constraints can be applied to class properties (such as name
) or to a public getter method (such as getFullName
) or even the entire class. Attribute constraints are the most commonly used and simplest, while Getter constraints allow you to specify more complex validation rules. Finally, if the use case for class constraints is that you want to verify the class as a whole.
Attribute constraints
The verification of class attributes is one of the most basic verification techniques. Symfony allows you to verify private, protected or public properties. The following code shows how to configure the $firstName
property of the Author
object so that it has at least 3 characters:
PHP:// src/AppBundle/Entity/Author.php // ...use Symfony\Component\Validator\Mapping\ClassMetadata;use Symfony\Component\Validator\Constraints as Assert; class Author{
private $firstName; public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('firstName', new Assert\NotBlank());
$metadata->addPropertyConstraint(
'firstName',
new Assert\Length(array("min" => 3))
);
}}
XML:<!-- src/AppBundle/Resources/config/validation.xml --><?xml version="1.0" encoding="UTF-8" ?><constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> <class name="AppBundle\Entity\Author">
<property name="firstName">
<constraint name="NotBlank" />
<constraint name="Length">
<option name="min">3</option>
</constraint>
</property>
</class></constraint-mapping>
YAML:# src/AppBundle/Resources/config/validation.ymlAppBundle\Entity\Author:
properties:
firstName:
- NotBlank: ~
- Length:
min: 3
Annotations:// src/AppBundle/Entity/Author.php // ...use Symfony\Component\Validator\Constraints as Assert; class Author{
/**
* @Assert\NotBlank()
* @Assert\Length(min=3)
*/
private $firstName;}
Getters Constraints
Constraints can also be applied to the return value of a method. Symfony allows you to add a constraint to any public method that begins with "get", "is" or "has". This type of method is called "getters".
The benefit of this technique is that it allows you to dynamically validate your objects. For example, suppose you want to ensure that the password field does not match the user's firstname (for security reasons). You can do this by creating a isPasswordLegal
method and then asserting that the method must return true
:
PHP:// src/AppBundle/Entity/Author.php // ...use Symfony\Component\Validator\Mapping\ClassMetadata;use Symfony\Component\Validator\Constraints as Assert; class Author{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addGetterConstraint('passwordLegal', new Assert\IsTrue(array(
'message' => 'The password cannot match your first name',
)));
}}
XML:<!-- src/AppBundle/Resources/config/validation.xml --><?xml version="1.0" encoding="UTF-8" ?><constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> <class name="AppBundle\Entity\Author">
<getter property="passwordLegal">
<constraint name="IsTrue">
<option name="message">The password cannot match your first name</option>
</constraint>
</getter>
</class></constraint-mapping>
YAML:# src/AppBundle/Resources/config/validation.ymlAppBundle\Entity\Author:
getters:
passwordLegal:
- 'IsTrue': { message: 'The password cannot match your first name' }
Annotations:// src/AppBundle/Entity/Author.php // ...use Symfony\Component\Validator\Constraints as Assert; class Author{
/**
* @Assert\IsTrue(message = "The password cannot match your first name")
*/
public function isPasswordLegal()
{
// ... return true or false
}}
Now, create a isPasswordLegal()
Method, containing the logic you need:
public function isPasswordLegal(){
return $this->firstName !== $this->password;}
Those with a sharp eye may notice that in the constraint configuration format of YAML, XML and PHP , the getter prefix ("get", "is" or "has") is ignored when mapping. This allows you to move a constraint to a subsequent property of the same name (or vice versa) without changing the validation logic.
Class constraints
There are some constraints that can be applied to the entire class being verified. For example, a constraint of type Callback is a general constraint that can be applied to the class itself. When a class is validated, the methods specified by the constraints will be executed directly to provide more custom validation.
Summary
Symfony's validator
(validation) is a powerful tool that can be used to ensure that the data of any object is legal. sex. Its power comes from constraints, which you can apply to object properties and getter methods. Although, in most cases, the validation framework is applied indirectly when using forms, remember, it can be used anywhere to validate any object.