Skip to content

Commit cc653b0

Browse files
[10.x] fix Before/After validation rules (#49871)
* added checks for 2nd date in compareDates * Add validation tests for date comparison * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent d48d28e commit cc653b0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,13 @@ protected function compareDates($attribute, $value, $parameters, $operator)
253253
}
254254

255255
if (is_null($date = $this->getDateTimestamp($parameters[0]))) {
256-
$date = $this->getDateTimestamp($this->getValue($parameters[0]));
256+
$comparedValue = $this->getValue($parameters[0]);
257+
258+
if (! is_string($comparedValue) && ! is_numeric($comparedValue) && ! $comparedValue instanceof DateTimeInterface) {
259+
return false;
260+
}
261+
262+
$date = $this->getDateTimestamp($comparedValue);
257263
}
258264

259265
return $this->compare($this->getDateTimestamp($value), $date, $operator);

tests/Validation/ValidationValidatorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5980,6 +5980,12 @@ public function testBeforeAndAfter()
59805980

59815981
$v = new Validator($trans, ['x' => '0001-01-01T00:00'], ['x' => 'after:1970-01-01']);
59825982
$this->assertTrue($v->fails());
5983+
5984+
$v = new Validator($trans, ['x' => ['a' => ['v' => 'c']], 'y' => 'invalid'], ['x' => 'date', 'y' => 'date|after:x']);
5985+
$this->assertTrue($v->fails());
5986+
5987+
$v = new Validator($trans, ['x' => ['a' => ['v' => 'c']], 'y' => 'invalid'], ['x' => 'date', 'y' => 'date|before:x']);
5988+
$this->assertTrue($v->fails());
59835989
}
59845990

59855991
public function testBeforeAndAfterWithFormat()

0 commit comments

Comments
 (0)