@@ -12,6 +12,7 @@ gives you similar flexibility.
12
12
| Options | - :ref: `expression <reference-constraint-expression-option >` |
13
13
| | - `message `_ |
14
14
| | - `payload `_ |
15
+ | | - `values `_ |
15
16
+----------------+-----------------------------------------------------------------------------------------------+
16
17
| Class | :class: `Symfony\\ Component\\ Validator\\ Constraints\\ Expression ` |
17
18
+----------------+-----------------------------------------------------------------------------------------------+
@@ -253,3 +254,90 @@ message
253
254
The default message supplied when the expression evaluates to false.
254
255
255
256
.. include :: /reference/constraints/_payload-option.rst.inc
257
+
258
+ values
259
+ ~~~~~~
260
+
261
+ **type **: ``array `` **default **: ``[] ``
262
+
263
+ .. versionadded :: 4.1
264
+ The ``values `` option was introduced in Symfony 4.1.
265
+
266
+ The values of the custom variables used in the expression. Values can be of any
267
+ type (numeric, boolean, strings, null, etc.)
268
+
269
+ .. configuration-block ::
270
+
271
+ .. code-block :: php-annotations
272
+
273
+ // src/Model/Analysis.php
274
+ namespace App\Model;
275
+
276
+ use Symfony\Component\Validator\Constraints as Assert;
277
+
278
+ class Analysis
279
+ {
280
+ /**
281
+ * @Assert\Expression(
282
+ * "value + error_margin < threshold",
283
+ * values = { "error_margin": 0.25, "threshold": 1.5 }
284
+ * )
285
+ */
286
+ private $metric;
287
+
288
+ // ...
289
+ }
290
+
291
+ .. code-block :: yaml
292
+
293
+ # config/validator/validation.yaml
294
+ App\Model\Analysis :
295
+ properties :
296
+ metric :
297
+ - Expression :
298
+ expression : " value + error_margin < threshold"
299
+ values : { error_margin: 0.25, threshold: 1.5 }
300
+
301
+ .. code-block :: xml
302
+
303
+ <!-- config/validator/validation.xml -->
304
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
305
+ <constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
306
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
307
+ xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
308
+
309
+ <class name =" App\Model\Analysis" >
310
+ <property name =" metric" >
311
+ <constraint name =" Expression" >
312
+ <option name =" expression" >
313
+ value + error_margin < threshold
314
+ </option >
315
+ <option name =" values" >
316
+ <value key =" error_margin" >0.25</value >
317
+ <value key =" threshold" >1.5</value >
318
+ </option >
319
+ </constraint >
320
+ </property >
321
+ </class >
322
+ </constraint-mapping >
323
+
324
+ .. code-block :: php
325
+
326
+ // src/Model/Analysis.php
327
+ namespace App\Model;
328
+
329
+ use Symfony\Component\Validator\Constraints as Assert;
330
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
331
+
332
+ class Analysis
333
+ {
334
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
335
+ {
336
+ $metadata->addPropertyConstraint('metric', new Assert\Expression(array(
337
+ 'expression' => 'value + error_margin < threshold',
338
+ 'values' => array('error_margin' => 0.25, 'threshold' => 1.5),
339
+ )));
340
+ }
341
+
342
+ // ...
343
+ }
0 commit comments