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

Skip to content

Commit 445ce1d

Browse files
committed
feature #25504 [Validator] Add option to pass custom values to Expression validator (ostrolucky)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Validator] Add option to pass custom values to Expression validator | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - I needed this in a Form. I had no way to pass things from `$options` into Expression validator. Maybe can aid in #23134 Commits ------- ba0565e [Validator] Add option to pass custom values to Expression validator
2 parents a15894e + ba0565e commit 445ce1d

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* Deprecated the `checkDNS` and `dnsMessage` options of the `Url` constraint. They will be removed in 5.0.
8+
* added a `values` option to the `Expression` constraint
89

910
4.0.0
1011
-----

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)