diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 453c81e6629d..9d8199dd2f26 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -147,9 +147,7 @@ protected function compareDates($attribute, $value, $parameters, $operator) } if ($format = $this->getDateFormat($attribute)) { - return $this->checkDateTimeOrder( - $format, $value, $this->getValue($parameters[0]) ?: $parameters[0], $operator - ); + return $this->checkDateTimeOrder($format, $value, $parameters[0], $operator); } if (! $date = $this->getDateTimestamp($parameters[0])) { @@ -194,11 +192,13 @@ protected function getDateTimestamp($value) */ protected function checkDateTimeOrder($format, $first, $second, $operator) { - $first = $this->getDateTimeWithOptionalFormat($format, $first); + $firstDate = $this->getDateTimeWithOptionalFormat($format, $first); - $second = $this->getDateTimeWithOptionalFormat($format, $second); + if (! $secondDate = $this->getDateTimeWithOptionalFormat($format, $second)) { + $secondDate = $this->getDateTimeWithOptionalFormat($format, $this->getValue($second)); + } - return ($first && $second) && ($this->compare($first, $second, $operator)); + return ($firstDate && $secondDate) && ($this->compare($firstDate, $secondDate, $operator)); } /** diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 29221b2a893d..01e5cdc5c25f 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2645,6 +2645,12 @@ public function testBeforeAndAfterWithFormat() $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|after:17:44']); $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['x' => '2038-01-18', '2018-05-12' => '2038-01-19'], ['x' => 'date_format:Y-m-d|before:2018-05-12']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['x' => '1970-01-02', '2018-05-12' => '1970-01-01'], ['x' => 'date_format:Y-m-d|after:2018-05-12']); + $this->assertTrue($v->fails()); } public function testWeakBeforeAndAfter()