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());