From 89c87a15e98dceb6b6b1d0ad0834c4e8e64f639b Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 12 Mar 2018 16:36:02 +0100 Subject: [PATCH 1/4] Documented the "values" option of the Expression constraint --- reference/constraints/Expression.rst | 88 ++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/reference/constraints/Expression.rst b/reference/constraints/Expression.rst index 7408b7bbb55..e0d3e638932 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**: an empty array + +.. versionadded:: 4.1 + The ``values`` option was introduced in Symfony 4.1. + +The values of the custom variables used in the expression. + +.. 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), + ))); + } + + // ... + } From a87c55ffaf3105bb3966038c16a69808e0bb82ac Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 12 Mar 2018 17:37:36 +0100 Subject: [PATCH 2/4] Tried to fix the XML example --- reference/constraints/Expression.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/reference/constraints/Expression.rst b/reference/constraints/Expression.rst index e0d3e638932..3a0448dadda 100644 --- a/reference/constraints/Expression.rst +++ b/reference/constraints/Expression.rst @@ -263,7 +263,8 @@ values .. versionadded:: 4.1 The ``values`` option was introduced in Symfony 4.1. -The values of the custom variables used in the expression. +The values of the custom variables used in the expression. Values can be of any +type (numeric, boolean, strings, null, etc.) .. configuration-block:: @@ -311,10 +312,11 @@ The values of the custom variables used in the expression. From 1e9041693aa81c35bd55b388c67d4300028b1ae3 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 14 Mar 2018 13:18:48 +0100 Subject: [PATCH 3/4] Tweaks after review --- reference/constraints/Expression.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/reference/constraints/Expression.rst b/reference/constraints/Expression.rst index 3a0448dadda..b9a352259ef 100644 --- a/reference/constraints/Expression.rst +++ b/reference/constraints/Expression.rst @@ -258,7 +258,7 @@ The default message supplied when the expression evaluates to false. values ~~~~~~ -**type**: ``array`` **default**: an empty array +**type**: ``array`` **default**: ``[]`` .. versionadded:: 4.1 The ``values`` option was introduced in Symfony 4.1. @@ -277,8 +277,6 @@ type (numeric, boolean, strings, null, etc.) class Analysis { - // ... - /** * @Assert\Expression( * "value + error_margin < threshold", From 0aa0ce304554642fe93f11f048cfbb5d8938c065 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sun, 18 Mar 2018 19:42:26 +0100 Subject: [PATCH 4/4] Minor tweak --- reference/constraints/Expression.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reference/constraints/Expression.rst b/reference/constraints/Expression.rst index b9a352259ef..80a9f0bed4a 100644 --- a/reference/constraints/Expression.rst +++ b/reference/constraints/Expression.rst @@ -280,7 +280,7 @@ type (numeric, boolean, strings, null, etc.) /** * @Assert\Expression( * "value + error_margin < threshold", - * values = { "error_margin": 0.25, threshold: 1.5 } + * values = { "error_margin": 0.25, "threshold": 1.5 } * ) */ private $metric;