Expression

Version: 2.4
Edit this page

Warning: You are browsing the documentation for Symfony 2.4, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

Expression

New in version 2.4

The Expression constraint was introduced in Symfony 2.4.

This constraint allows you to use an expression for more complex, dynamic validation. See Basic Usage for an example. See Callback for a different constraint that gives you similar flexibility.

Applies to class or property/method
Options
Class Expression
Validator ExpressionValidator

Basic Usage

Imagine you have a class BlogPost with category and isTechnicalPost properties:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace Acme\DemoBundle\Model;

use Symfony\Component\Validator\Constraints as Assert;

class BlogPost
{
    private $category;

    private $isTechnicalPost;

    // ...

    public function getCategory()
    {
        return $this->category;
    }

    public function setIsTechnicalPost($isTechnicalPost)
    {
        $this->isTechnicalPost = $isTechnicalPost;
    }

    // ...
}

To validate the object, you have some special requirements:

A) If isTechnicalPost is true, then category must be either php
or symfony;

B) If isTechnicalPost is false, then category can be anything.

One way to accomplish this is with the Expression constraint:

  • YAML
  • Annotations
  • XML
  • PHP
1
2
3
4
5
6
# src/Acme/DemoBundle/Resources/config/validation.yml
Acme\DemoBundle\Model\BlogPost:
    constraints:
        - Expression:
            expression: "this.getCategory() in ['php', 'symfony'] or !this.isTechnicalPost()"
            message: "If this is a tech post, the category should be either php or symfony!"

The expression option is the expression that must return true in order for validation to pass. To learn more about the expression language syntax, see The Expression Syntax.

You can also attach the constraint to a specific property and still validate based on the values of the entire entity. This is handy if you want to attach the error to a specific field. In this context, value represents the value of isTechnicalPost.

  • YAML
  • Annotations
  • XML
  • PHP
1
2
3
4
5
6
7
# src/Acme/DemoBundle/Resources/config/validation.yml
Acme\DemoBundle\Model\BlogPost:
    properties:
        isTechnicalPost:
            - Expression:
                expression: "this.getCategory() in ['php', 'symfony'] or value == false"
                message: "If this is a tech post, the category should be either php or symfony!"

Caution

In Symfony 2.4 and Symfony 2.5, if the property (e.g. isTechnicalPost) were null, the expression would never be called and the value would be seen as valid. To ensure that the value is not null, use the NotNull constraint.

For more information about the expression and what variables are available to you, see the expression option details below.

Available Options

expression

type: string [default option]

The expression that will be evaluated. If the expression evaluates to a false value (using ==, not ===), validation will fail.

To learn more about the expression language syntax, see The Expression Syntax.

Inside of the expression, you have access to up to 2 variables:

Depending on how you use the constraint, you have access to 1 or 2 variables in your expression:

  • this: The object being validated (e.g. an instance of BlogPost);
  • value: The value of the property being validated (only available when the constraint is applied directly to a property);

message

type: string default: This value is not valid.

The default message supplied when the expression evaluates to false.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.