From 1b651e67349823b2e820da6f6b45c1399a4a6d25 Mon Sep 17 00:00:00 2001 From: Denes Daniel Date: Mon, 28 Aug 2017 00:16:30 +0200 Subject: [PATCH] Fixed Validator failing on 'before_or_equal:today' when the input is today's date and a format is specified The issue was that the time part was bleeding through from the current timestamp in `DateTime::createFromFormat`. Test case: * Validation rule: 'birth_date' => ['required', 'date_format:Y-m-d', 'before_or_equal:today'] * '2017-08-27' was parsed as '2017-08-27 22:41:37'. * 'today' was parsed as '2017-08-27 00:00:00'. * Validation failed. --- .../Concerns/ValidatesAttributes.php | 8 ++--- tests/Validation/ValidationValidatorTest.php | 36 +++++++++++++++---- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index ff0429a872a5..05831c865511 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -210,7 +210,7 @@ protected function checkDateTimeOrder($format, $first, $second, $operator) */ protected function getDateTimeWithOptionalFormat($format, $value) { - if ($date = DateTime::createFromFormat($format, $value)) { + if ($date = DateTime::createFromFormat('!'.$format, $value)) { return $date; } @@ -358,11 +358,11 @@ protected function validateDateFormat($attribute, $value, $parameters) return false; } - $format = $parameters[0] == 'Y-m' ? '!Y-m' : $parameters[0]; + $format = $parameters[0]; - $date = DateTime::createFromFormat($format, $value); + $date = DateTime::createFromFormat('!'.$format, $value); - return $date && $date->format($parameters[0]) == $value; + return $date && $date->format($format) == $value; } /** diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 02e652cc0c38..14e80941d468 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2445,13 +2445,19 @@ public function testBeforeAndAfterWithFormat() $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after:yesterday|before:tomorrow']); $this->assertTrue($v->passes()); - $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after:tomorrow|before:yesterday']); + $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after:today']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before:today']); $this->assertTrue($v->fails()); $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'after:yesterday|before:tomorrow']); $this->assertTrue($v->passes()); - $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'after:tomorrow|before:yesterday']); + $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'after:today']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'before:today']); $this->assertTrue($v->fails()); } @@ -2468,10 +2474,19 @@ public function testWeakBeforeAndAfter() $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'before_or_equal:2012-01-14']); $this->assertTrue($v->fails()); - $v = new Validator($trans, ['x' => '31/12/2012'], ['x' => 'date_format:d/m/Y|before_or_equal:31/12/2012']); + $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|before_or_equal:15/01/2012']); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|before_or_equal:14/01/2012']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before_or_equal:today']); $this->assertTrue($v->passes()); - $v = new Validator($trans, ['x' => '31/12/2012'], ['x' => 'date_format:d/m/Y|before_or_equal:29/12/2012']); + $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before_or_equal:tomorrow']); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before_or_equal:yesterday']); $this->assertTrue($v->fails()); $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'after_or_equal:2012-01-15']); @@ -2483,10 +2498,19 @@ public function testWeakBeforeAndAfter() $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'after_or_equal:2012-01-16']); $this->assertTrue($v->fails()); - $v = new Validator($trans, ['x' => '31/12/2012'], ['x' => 'date_format:d/m/Y|after_or_equal:31/12/2012']); + $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|after_or_equal:15/01/2012']); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|after_or_equal:16/01/2012']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after_or_equal:today']); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after_or_equal:yesterday']); $this->assertTrue($v->passes()); - $v = new Validator($trans, ['x' => '31/12/2012'], ['x' => 'date_format:d/m/Y|after_or_equal:02/01/2013']); + $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after_or_equal:tomorrow']); $this->assertTrue($v->fails()); }