From 367a901207068cfffa0170e8500e94e1a2479a5b Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Tue, 18 Aug 2020 15:03:46 +0200 Subject: [PATCH] allow empty strings as time format --- src/Validator/Constraints/TimeFormatValidator.php | 4 ++++ tests/Validator/Constraints/TimeFormatValidatorTest.php | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Validator/Constraints/TimeFormatValidator.php b/src/Validator/Constraints/TimeFormatValidator.php index b6c2cbf7..b8ed544b 100644 --- a/src/Validator/Constraints/TimeFormatValidator.php +++ b/src/Validator/Constraints/TimeFormatValidator.php @@ -26,6 +26,10 @@ class TimeFormatValidator extends ConstraintValidator throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\TimeFormat'); } + if (null === $value || '' === $value) { + return; + } + if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { throw new UnexpectedValueException($value, 'string'); } diff --git a/tests/Validator/Constraints/TimeFormatValidatorTest.php b/tests/Validator/Constraints/TimeFormatValidatorTest.php index c7e8d92f..e42d1a77 100644 --- a/tests/Validator/Constraints/TimeFormatValidatorTest.php +++ b/tests/Validator/Constraints/TimeFormatValidatorTest.php @@ -44,7 +44,7 @@ class TimeFormatValidatorTest extends ConstraintValidatorTestCase /** * @dataProvider getValidTimes */ - public function testValidationSucceeds(string $value) + public function testValidationSucceeds(?string $value) { $this->validator->validate($value, new TimeFormat()); $this->assertNoViolation(); @@ -53,6 +53,8 @@ class TimeFormatValidatorTest extends ConstraintValidatorTestCase public function getValidTimes() { return [ + [''], + [null], ['00:00'], ['00:01'], ['23:00'], @@ -65,7 +67,7 @@ class TimeFormatValidatorTest extends ConstraintValidatorTestCase /** * @dataProvider getInvalidTimes */ - public function testValidationProblem(string $value) + public function testValidationProblem(?string $value) { $this->validator->validate($value, new TimeFormat()); @@ -78,6 +80,7 @@ class TimeFormatValidatorTest extends ConstraintValidatorTestCase public function getInvalidTimes() { return [ + ['a'], ['1:00'], ['01:1'], ['00:60'],