From a3cfd2d527107455ef384ecd3e1805edef84ecb3 Mon Sep 17 00:00:00 2001 From: Tsuguya Toma Date: Wed, 19 Apr 2023 22:54:02 +0900 Subject: [PATCH] fix date_format rule throw ValueError (#46824) --- .../Validation/Concerns/ValidatesAttributes.php | 11 ++++++++--- tests/Validation/ValidationValidatorTest.php | 3 +++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 82d613e293c1..efdadfb5d54a 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -25,6 +25,7 @@ use InvalidArgumentException; use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\UploadedFile; +use ValueError; trait ValidatesAttributes { @@ -550,10 +551,14 @@ public function validateDateFormat($attribute, $value, $parameters) } foreach ($parameters as $format) { - $date = DateTime::createFromFormat('!'.$format, $value); + try { + $date = DateTime::createFromFormat('!'.$format, $value); - if ($date && $date->format($format) == $value) { - return true; + if ($date && $date->format($format) == $value) { + return true; + } + } catch (ValueError) { + return false; } } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 5a3121c896a7..ecc9cd18e534 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -4802,6 +4802,9 @@ public function testValidateDateAndFormat() $v = new Validator($trans, ['x' => ['Not', 'a', 'date']], ['x' => 'date_format:Y-m-d']); $this->assertTrue($v->fails()); + $v = new Validator($trans, ['x' => "Contain null bytes \0"], ['x' => 'date_format:Y-m-d']); + $this->assertTrue($v->fails()); + // Set current machine date to 31/xx/xxxx $v = new Validator($trans, ['x' => '2013-02'], ['x' => 'date_format:Y-m']); $this->assertTrue($v->passes());