From 484d22a6d913bd7b0795ce0cae5c73e1f1246a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 18 Dec 2018 11:24:49 +0100 Subject: [PATCH] [Validator] NotBlank: add a new option to allow null values --- src/Symfony/Component/Validator/CHANGELOG.md | 1 + .../Validator/Constraints/NotBlank.php | 2 ++ .../Constraints/NotBlankValidator.php | 5 ++++ .../Constraints/NotBlankValidatorTest.php | 26 +++++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 45e00e00a9..43ead5b182 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * added options `iban` and `ibanPropertyPath` to Bic constraint * added UATP cards support to `CardSchemeValidator` + * added option `allowNull` to NotBlank constraint 4.2.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/NotBlank.php b/src/Symfony/Component/Validator/Constraints/NotBlank.php index 7616c426c7..c94f365500 100644 --- a/src/Symfony/Component/Validator/Constraints/NotBlank.php +++ b/src/Symfony/Component/Validator/Constraints/NotBlank.php @@ -18,6 +18,7 @@ use Symfony\Component\Validator\Constraint; * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) * * @author Bernhard Schussek + * @author Kévin Dunglas */ class NotBlank extends Constraint { @@ -28,4 +29,5 @@ class NotBlank extends Constraint ]; public $message = 'This value should not be blank.'; + public $allowNull = false; } diff --git a/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php b/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php index 1d6c5bc983..31798f28bf 100644 --- a/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php +++ b/src/Symfony/Component/Validator/Constraints/NotBlankValidator.php @@ -17,6 +17,7 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * @author Bernhard Schussek + * @author Kévin Dunglas */ class NotBlankValidator extends ConstraintValidator { @@ -29,6 +30,10 @@ class NotBlankValidator extends ConstraintValidator throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\NotBlank'); } + if ($constraint->allowNull && null === $value) { + return; + } + if (false === $value || (empty($value) && '0' != $value)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php index f634e0c11c..f46e017f9a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php @@ -98,4 +98,30 @@ class NotBlankValidatorTest extends ConstraintValidatorTestCase ->setCode(NotBlank::IS_BLANK_ERROR) ->assertRaised(); } + + public function testAllowNullTrue() + { + $constraint = new NotBlank([ + 'message' => 'myMessage', + 'allowNull' => true, + ]); + + $this->validator->validate(null, $constraint); + $this->assertNoViolation(); + } + + public function testAllowNullFalse() + { + $constraint = new NotBlank([ + 'message' => 'myMessage', + 'allowNull' => false, + ]); + + $this->validator->validate(null, $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', 'null') + ->setCode(NotBlank::IS_BLANK_ERROR) + ->assertRaised(); + } }