Expression ¶
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 |
| 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 25 |
// src/Model/BlogPost.php
namespace App\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
isTechnicalPostis true, thencategorymust be eitherphp -
or
symfony;またはsymfony;
B) If isTechnicalPost is false, then category can be anything.
One way to accomplish this is with the Expression constraint:
-
Attributes
属性
-
YAML
YAML
-
XML
XML
-
PHP
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// src/Model/BlogPost.php
namespace App\Model;
use Symfony\Component\Validator\Constraints as Assert;
#[Assert\Expression(
"this.getCategory() in ['php', 'symfony'] or !this.isTechnicalPost()",
message: 'If this is a tech post, the category should be either php or symfony!',
)]
class BlogPost
{
// ...
}
|
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.
Alternatively, you can set the negate option to false in order to
assert that the expression must return true for validation to fail.
6.2
The negate option was introduced in Symfony 6.2.
For more information about the expression and what variables are available to you, see the expression option details below.
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);this: 検証されるオブジェクト (BlogPost のインスタンスなど)。value: The value of the property being validated (only available when the constraint is applied directly to a property);value: 検証されるプロパティの値 (制約がプロパティに直接適用されている場合にのみ使用可能)。
groups
¶
type: array | string
It defines the validation group or groups of this constraint. Read more about validation groups.
message
¶
type: string default: This value is not valid.
The default message supplied when the expression evaluates to false.
You can use the following parameters in this message:
| Parameter | Description |
|---|---|
{{ value }} |
The current (invalid) value |
{{ label }} |
Corresponding form field label |
negate
¶
type: boolean default: true
If false, the validation fails when expression returns true.
6.2
The negate option was introduced in Symfony 6.2.
payload
¶
type: mixed default: null
This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to you.
For example, you may want to use several error levels to present failed constraints differently in the front-end depending on the severity of the error.
values
¶
type: array default: []
The values of the custom variables used in the expression. Values can be of any type (numeric, boolean, strings, null, etc.)
-
Attributes
属性
-
YAML
YAML
-
XML
XML
-
PHP
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// src/Model/Analysis.php
namespace App\Model;
use Symfony\Component\Validator\Constraints as Assert;
class Analysis
{
#[Assert\Expression(
'value + error_margin < threshold',
values: ['error_margin' => 0.25, 'threshold' => 1.5],
)]
private $metric;
// ...
}
|