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