From 8835393b815806659766312c7c7aabbf7d17c847 Mon Sep 17 00:00:00 2001 From: Punyapal Shah Date: Sat, 27 Jan 2024 22:22:58 +0530 Subject: [PATCH 1/3] added checks for 2nd date in compareDates --- .../Validation/Concerns/ValidatesAttributes.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index b4335fe90059..9b98db905c71 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -253,7 +253,13 @@ protected function compareDates($attribute, $value, $parameters, $operator) } if (is_null($date = $this->getDateTimestamp($parameters[0]))) { - $date = $this->getDateTimestamp($this->getValue($parameters[0])); + $secondValue = $this->getValue($parameters[0]); + + if (! is_string($secondValue) && ! is_numeric($secondValue) && ! $secondValue instanceof DateTimeInterface) { + return false; + } + + $date = $this->getDateTimestamp($secondValue); } return $this->compare($this->getDateTimestamp($value), $date, $operator); From 302a484fa492f682b472689e517b488e1b4a1d58 Mon Sep 17 00:00:00 2001 From: Punyapal Shah Date: Sat, 27 Jan 2024 22:40:11 +0530 Subject: [PATCH 2/3] Add validation tests for date comparison --- tests/Validation/ValidationValidatorTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 7c9d7b31d3a2..e351c118eb30 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -5980,6 +5980,12 @@ public function testBeforeAndAfter() $v = new Validator($trans, ['x' => '0001-01-01T00:00'], ['x' => 'after:1970-01-01']); $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['x' => ['a' => ['v' => 'c']], 'y' => 'invalid'], ['x' => 'date', 'y' => 'date|after:x']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['x' => ['a' => ['v' => 'c']], 'y' => 'invalid'], ['x' => 'date', 'y' => 'date|before:x']); + $this->assertTrue($v->fails()); } public function testBeforeAndAfterWithFormat() From 821ebfaa08fde0099de193c2ce1623346c1b55ac Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 30 Jan 2024 10:00:16 -0600 Subject: [PATCH 3/3] formatting --- src/Illuminate/Validation/Concerns/ValidatesAttributes.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 9b98db905c71..c95a9f4f9999 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -253,13 +253,13 @@ protected function compareDates($attribute, $value, $parameters, $operator) } if (is_null($date = $this->getDateTimestamp($parameters[0]))) { - $secondValue = $this->getValue($parameters[0]); + $comparedValue = $this->getValue($parameters[0]); - if (! is_string($secondValue) && ! is_numeric($secondValue) && ! $secondValue instanceof DateTimeInterface) { + if (! is_string($comparedValue) && ! is_numeric($comparedValue) && ! $comparedValue instanceof DateTimeInterface) { return false; } - $date = $this->getDateTimestamp($secondValue); + $date = $this->getDateTimestamp($comparedValue); } return $this->compare($this->getDateTimestamp($value), $date, $operator);