From 580e1a7b4696eccfc60b5783ca0f25e6c6dcb4c3 Mon Sep 17 00:00:00 2001 From: Tom Corrigan Date: Thu, 7 Aug 2014 02:24:06 +1000 Subject: [PATCH] [Validator] fixed: Expressions always valid for null values The ExpressionValidator was incorrectly skipping validation of null or empty string values. --- UPGRADE-2.6.md | 11 ++++++++++ src/Symfony/Component/Validator/CHANGELOG.md | 1 + .../Constraints/ExpressionValidator.php | 4 ---- .../Constraints/ExpressionValidatorTest.php | 22 ++++++++++++++----- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/UPGRADE-2.6.md b/UPGRADE-2.6.md index c03c3730084e..666ada13b030 100644 --- a/UPGRADE-2.6.md +++ b/UPGRADE-2.6.md @@ -17,3 +17,14 @@ Validator { } ``` + + * Prior to 2.6 `Symfony\Component\Validator\Constraints\ExpressionValidator` + would not execute the Expression if it was attached to a property on an + object and that property was set to `null` or an empty string. + + To emulate the old behaviour change your expression to something like + this: + + ``` + value == null or (YOUR_EXPRESSION) + ``` diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index ad92e56230b5..f0a04fc70d97 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -8,6 +8,7 @@ CHANGELOG * [BC BREAK] `UserPasswordValidator` source message change * [BC BREAK] added internal `ExecutionContextInterface::setConstraint()` * added `ConstraintViolation::getConstraint()` + * [BC BREAK] The `ExpressionValidator` will now evaluate the Expression even when the property value is null or an empty string 2.5.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php b/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php index 3df23d434279..2ca72fa764bd 100644 --- a/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php @@ -60,10 +60,6 @@ public function validate($value, Constraint $constraint) throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression'); } - if (null === $value || '' === $value) { - return; - } - $variables = array(); // Symfony 2.5+ diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php index 1e90c09e31c3..bf8d0e50d514 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php @@ -29,18 +29,28 @@ protected function createValidator() return new ExpressionValidator(PropertyAccess::createPropertyAccessor()); } - public function testNullIsValid() + public function testExpressionIsEvaluatedWithNullValue() { - $this->validator->validate(null, new Expression('value == 1')); + $constraint = new Expression(array( + 'expression' => 'false', + 'message' => 'myMessage', + )); - $this->assertNoViolation(); + $this->validator->validate('', $constraint); + + $this->assertViolation('myMessage'); } - public function testEmptyStringIsValid() + public function testExpressionIsEvaluatedWithEmptyStringValue() { - $this->validator->validate('', new Expression('value == 1')); + $constraint = new Expression(array( + 'expression' => 'false', + 'message' => 'myMessage', + )); - $this->assertNoViolation(); + $this->validator->validate('', $constraint); + + $this->assertViolation('myMessage'); } public function testSucceedingExpressionAtObjectLevel()