[Validator] Add option to pass custom values to Expression validator
This commit is contained in:
parent
96e53f8837
commit
ba0565e3e8
@ -5,6 +5,7 @@ CHANGELOG
|
||||
-----
|
||||
|
||||
* Deprecated the `checkDNS` and `dnsMessage` options of the `Url` constraint. They will be removed in 5.0.
|
||||
* added a `values` option to the `Expression` constraint
|
||||
|
||||
4.0.0
|
||||
-----
|
||||
|
@ -30,6 +30,7 @@ class Expression extends Constraint
|
||||
|
||||
public $message = 'This value is not valid.';
|
||||
public $expression;
|
||||
public $values = array();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -39,7 +39,7 @@ class ExpressionValidator extends ConstraintValidator
|
||||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression');
|
||||
}
|
||||
|
||||
$variables = array();
|
||||
$variables = $constraint->values;
|
||||
$variables['value'] = $value;
|
||||
$variables['this'] = $this->context->getObject();
|
||||
|
||||
|
@ -270,4 +270,18 @@ class ExpressionValidatorTest extends ConstraintValidatorTestCase
|
||||
|
||||
$this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
|
||||
}
|
||||
|
||||
public function testPassingCustomValues()
|
||||
{
|
||||
$constraint = new Expression(array(
|
||||
'expression' => 'value + custom == 2',
|
||||
'values' => array(
|
||||
'custom' => 1,
|
||||
),
|
||||
));
|
||||
|
||||
$this->validator->validate(1, $constraint);
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user