diff --git a/Constraints/NotBlankValidator.php b/Constraints/NotBlankValidator.php index acc81040..3b749a9d 100644 --- a/Constraints/NotBlankValidator.php +++ b/Constraints/NotBlankValidator.php @@ -18,6 +18,7 @@ /** * @author Bernhard Schussek * @author Kévin Dunglas + * @author Lionel Kimb's */ class NotBlankValidator extends ConstraintValidator { @@ -35,6 +36,8 @@ public function validate(mixed $value, Constraint $constraint): void $value = ($constraint->normalizer)($value); } + $value = is_string($value) ? trim($value) : $value; + if (false === $value || (empty($value) && '0' != $value)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) diff --git a/Tests/Constraints/NotBlankValidatorTest.php b/Tests/Constraints/NotBlankValidatorTest.php index 8d1ba3d0..b55eb199 100644 --- a/Tests/Constraints/NotBlankValidatorTest.php +++ b/Tests/Constraints/NotBlankValidatorTest.php @@ -71,6 +71,20 @@ public function testBlankIsInvalid() ->assertRaised(); } + public function testBlankTextIsInvalid() + { + $constraint = new NotBlank([ + 'message' => 'myMessage', + ]); + + $this->validator->validate(' ', $constraint); + + $this->buildViolation('myMessage') + ->setParameter('{{ value }}', '""') + ->setCode(NotBlank::IS_BLANK_ERROR) + ->assertRaised(); + } + public function testFalseIsInvalid() { $constraint = new NotBlank([