Skip to content

Commit

Permalink
Fixed Validator failing on 'before_or_equal:today' when the input is …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
pantherdd committed Aug 27, 2017
1 parent da18899 commit 1b651e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

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

Expand All @@ -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']);
Expand All @@ -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());
}

Expand Down

0 comments on commit 1b651e6

Please sign in to comment.