Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57d79b2

Browse files
committed
[Validator] Add option to pass custom values to Expression validator
1 parent 6470380 commit 57d79b2

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.1.0
5+
-----
6+
7+
* added a `values` option to the `Expression` constraint
8+
49
4.0.0
510
-----
611

src/Symfony/Component/Validator/Constraints/Expression.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Expression extends Constraint
3030

3131
public $message = 'This value is not valid.';
3232
public $expression;
33+
public $values = array();
3334

3435
/**
3536
* {@inheritdoc}

src/Symfony/Component/Validator/Constraints/ExpressionValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function validate($value, Constraint $constraint)
3939
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression');
4040
}
4141

42-
$variables = array();
42+
$variables = $constraint->values;
4343
$variables['value'] = $value;
4444
$variables['this'] = $this->context->getObject();
4545

src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,18 @@ public function testExpressionLanguageUsage()
270270

271271
$this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
272272
}
273+
274+
public function testPassingCustomValues()
275+
{
276+
$constraint = new Expression(array(
277+
'expression' => 'value + custom == 2',
278+
'values' => array(
279+
'custom' => 1,
280+
),
281+
));
282+
283+
$this->validator->validate(1, $constraint);
284+
285+
$this->assertNoViolation();
286+
}
273287
}

0 commit comments

Comments
 (0)