Skip to content

Commit a3cfd2d

Browse files
authored
fix date_format rule throw ValueError (#46824)
1 parent ed75852 commit a3cfd2d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use InvalidArgumentException;
2626
use Symfony\Component\HttpFoundation\File\File;
2727
use Symfony\Component\HttpFoundation\File\UploadedFile;
28+
use ValueError;
2829

2930
trait ValidatesAttributes
3031
{
@@ -550,10 +551,14 @@ public function validateDateFormat($attribute, $value, $parameters)
550551
}
551552

552553
foreach ($parameters as $format) {
553-
$date = DateTime::createFromFormat('!'.$format, $value);
554+
try {
555+
$date = DateTime::createFromFormat('!'.$format, $value);
554556

555-
if ($date && $date->format($format) == $value) {
556-
return true;
557+
if ($date && $date->format($format) == $value) {
558+
return true;
559+
}
560+
} catch (ValueError) {
561+
return false;
557562
}
558563
}
559564

tests/Validation/ValidationValidatorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4802,6 +4802,9 @@ public function testValidateDateAndFormat()
48024802
$v = new Validator($trans, ['x' => ['Not', 'a', 'date']], ['x' => 'date_format:Y-m-d']);
48034803
$this->assertTrue($v->fails());
48044804

4805+
$v = new Validator($trans, ['x' => "Contain null bytes \0"], ['x' => 'date_format:Y-m-d']);
4806+
$this->assertTrue($v->fails());
4807+
48054808
// Set current machine date to 31/xx/xxxx
48064809
$v = new Validator($trans, ['x' => '2013-02'], ['x' => 'date_format:Y-m']);
48074810
$this->assertTrue($v->passes());

0 commit comments

Comments
 (0)