From 8ba78793ea18e7f35d7a633ccd4305e969c6e6eb Mon Sep 17 00:00:00 2001 From: Jonnathan Soares Date: Sat, 12 May 2018 18:11:30 -0300 Subject: [PATCH 1/2] Add more tests to Validator's before and after rules. When the rules before and/or after are used together with the date_format rules, the validator should give preference to validate a given field relative to the fixed date specified in the rule, instead of comparing to a field with the same name as the specified date. --- tests/Validation/ValidationValidatorTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index fc5f5e69faba..ce82ce240f84 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2600,6 +2600,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() From 3ce7ca5b23b49e736d58b494f6e338b3c863a608 Mon Sep 17 00:00:00 2001 From: Jonnathan Soares Date: Tue, 15 May 2018 00:29:59 -0300 Subject: [PATCH 2/2] Fix validation bypass for rules 'before' and 'after' when paired with 'date_format'. Now when when there is both a 'date_format' rule and a 'before' and/or 'after' rule, the first parameter will be converted to a timestamp, and only if that fails, it will be considered as a name for another field. This exactly the same behavior when there isn't a date_format rule. fixes #24191 --- .../Validation/Concerns/ValidatesAttributes.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index e4bcd813425f..7cdaef87e810 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)); } /**