diff --git a/reference/constraints/Expression.rst b/reference/constraints/Expression.rst index 7408b7bbb55..80a9f0bed4a 100644 --- a/reference/constraints/Expression.rst +++ b/reference/constraints/Expression.rst @@ -12,6 +12,7 @@ gives you similar flexibility. | Options | - :ref:`expression ` | | | - `message`_ | | | - `payload`_ | +| | - `values`_ | +----------------+-----------------------------------------------------------------------------------------------+ | Class | :class:`Symfony\\Component\\Validator\\Constraints\\Expression` | +----------------+-----------------------------------------------------------------------------------------------+ @@ -253,3 +254,90 @@ message The default message supplied when the expression evaluates to false. .. include:: /reference/constraints/_payload-option.rst.inc + +values +~~~~~~ + +**type**: ``array`` **default**: ``[]`` + +.. versionadded:: 4.1 + The ``values`` option was introduced in Symfony 4.1. + +The values of the custom variables used in the expression. Values can be of any +type (numeric, boolean, strings, null, etc.) + +.. configuration-block:: + + .. code-block:: php-annotations + + // 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; + + // ... + } + + .. code-block:: yaml + + # config/validator/validation.yaml + App\Model\Analysis: + properties: + metric: + - Expression: + expression: "value + error_margin < threshold" + values: { error_margin: 0.25, threshold: 1.5 } + + .. code-block:: xml + + + + + + + + + + + + + + + + .. code-block:: php + + // src/Model/Analysis.php + namespace App\Model; + + use Symfony\Component\Validator\Constraints as Assert; + use Symfony\Component\Validator\Mapping\ClassMetadata; + + class Analysis + { + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addPropertyConstraint('metric', new Assert\Expression(array( + 'expression' => 'value + error_margin < threshold', + 'values' => array('error_margin' => 0.25, 'threshold' => 1.5), + ))); + } + + // ... + }