Skip to content

Commit

Permalink
fix date_format rule throw ValueError (laravel#46824)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsuguya authored Apr 19, 2023
1 parent ed75852 commit a3cfd2d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use InvalidArgumentException;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use ValueError;

trait ValidatesAttributes
{
Expand Down Expand Up @@ -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;
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit a3cfd2d

Please sign in to comment.