Skip to content

Commit 338213f

Browse files
committed
[Validator] Documented the ExpressionLanguageSyntax constraint
1 parent f9c71c2 commit 338213f

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

reference/constraints.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Validation Constraints Reference
1414
constraints/Type
1515

1616
constraints/Email
17+
constraints/ExpressionLanguageSyntax
1718
constraints/Length
1819
constraints/Url
1920
constraints/Regex
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
ExpressionLanguageSyntax
2+
========================
3+
4+
This constraint checks that the value is valid as an `ExpressionLanguage`_
5+
expression.
6+
7+
.. versionadded:: 5.1
8+
9+
The ``ExpressionLanguageSyntax`` constraint was introduced in Symfony 5.1.
10+
11+
========== ===================================================================
12+
Applies to :ref:`property or method <validation-property-target>`
13+
Options - `groups`_
14+
- `message`_
15+
- `names`_
16+
- `payload`_
17+
- `validateNames`_
18+
Class :class:`Symfony\\Component\\Validator\\Constraints\\ExpressionLanguageSyntax`
19+
Validator :class:`Symfony\\Component\\Validator\\Constraints\\ExpressionLanguageSyntaxValidator`
20+
========== ===================================================================
21+
22+
Basic Usage
23+
-----------
24+
25+
The following constraints ensure that:
26+
27+
* the ``promotion`` propery stores a value which is valid as an
28+
ExpressionLanguage expression;
29+
* the ``shippingOptions`` property also ensures that the expression only uses
30+
certain variables.
31+
32+
.. configuration-block::
33+
34+
.. code-block:: php-annotations
35+
36+
// src/Entity/Order.php
37+
namespace App\Entity;
38+
39+
use Symfony\Component\Validator\Constraints as Assert;
40+
41+
class Order
42+
{
43+
/**
44+
* @Assert\ExpressionLanguageSyntax()
45+
*/
46+
protected $promotion;
47+
48+
/**
49+
* @Assert\ExpressionLanguageSyntax(
50+
* names = ['user', 'shipping_centers'],
51+
* validateNames = true
52+
* )
53+
*/
54+
protected $shippingOptions;
55+
}
56+
57+
.. code-block:: yaml
58+
59+
# config/validator/validation.yaml
60+
App\Entity\Order:
61+
properties:
62+
promotion:
63+
- ExpressionLanguageSyntax: ~
64+
shippingOptions:
65+
- ExpressionLanguageSyntax:
66+
names: ['user', 'shipping_centers']
67+
validateNames: true
68+
69+
.. code-block:: xml
70+
71+
<!-- config/validator/validation.xml -->
72+
<?xml version="1.0" encoding="UTF-8" ?>
73+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
74+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
75+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
76+
77+
<class name="App\Entity\Order">
78+
<property name="promotion">
79+
<constraint name="ExpressionLanguageSyntax"/>
80+
</property>
81+
<property name="shippingOptions">
82+
<constraint name="ExpressionLanguageSyntax">
83+
<option name="names">['user', 'shipping_centers']</option>
84+
<option name="validateNames">true</option>
85+
</constraint>
86+
</property>
87+
</class>
88+
</constraint-mapping>
89+
90+
.. code-block:: php
91+
92+
// src/Entity/Student.php
93+
namespace App\Entity;
94+
95+
use Symfony\Component\Validator\Constraints as Assert;
96+
use Symfony\Component\Validator\Mapping\ClassMetadata;
97+
98+
class Order
99+
{
100+
public static function loadValidatorMetadata(ClassMetadata $metadata)
101+
{
102+
$metadata->addPropertyConstraint('promotion', new Assert\ExpressionLanguageSyntax());
103+
104+
$metadata->addPropertyConstraint('shippingOptions', new Assert\ExpressionLanguageSyntax([
105+
'names' => ['user', 'shipping_centers'],
106+
'validateNames' => true,
107+
]));
108+
}
109+
}
110+
111+
Options
112+
-------
113+
114+
.. include:: /reference/constraints/_groups-option.rst.inc
115+
116+
message
117+
~~~~~~~
118+
119+
**type**: ``string`` **default**: ``This value should be a valid expression.``
120+
121+
This is the message displayed when the validation fails.
122+
123+
names
124+
~~~~~
125+
126+
**type**: ``array`` or ``null`` **default**: ``null``
127+
128+
If the ``validateNames`` is ``true``, the expression can only use the variables
129+
whose names are included in this array. Use ``null`` to allow expressions to use
130+
any variables.
131+
132+
.. include:: /reference/constraints/_payload-option.rst.inc
133+
134+
validateNames
135+
~~~~~~~~~~~~~
136+
137+
**type**: ``bool`` **default**: ``false``
138+
139+
Set this option to ``true`` to require the expression to use only the variable
140+
names defined in the ``names`` option.
141+
142+
.. _`ExpressionLanguage`: https://symfony.com/components/ExpressionLanguage

reference/constraints/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ String Constraints
1616
~~~~~~~~~~~~~~~~~~
1717

1818
* :doc:`Email </reference/constraints/Email>`
19+
* :doc:`ExpressionLanguageSyntax </reference/constraints/ExpressionLanguageSyntax>`
1920
* :doc:`Length </reference/constraints/Length>`
2021
* :doc:`Url </reference/constraints/Url>`
2122
* :doc:`Regex </reference/constraints/Regex>`

0 commit comments

Comments
 (0)