Skip to content

Commit

Permalink
[5.5] Validation bypass for 'before' and 'after' rules when paired wi…
Browse files Browse the repository at this point in the history
…th 'date_format' rule. (#24191)

* 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.

* 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
  • Loading branch information
jonnsl authored and taylorotwell committed May 21, 2018
1 parent 2d40c7a commit f1f2276
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down Expand Up @@ -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));
}

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

0 comments on commit f1f2276

Please sign in to comment.