Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4+] Fixed Validator failing on 'before_or_equal:today' for today's date #20868

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
132 changes: 123 additions & 9 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2305,9 +2305,6 @@ public function testValidateDateAndFormat()
$v = new Validator($trans, ['x' => '2000-01-01'], ['x' => 'date_format:Y-m-d']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '2000-01-01 17:43:59'], ['x' => 'date_format:Y-m-d H:i:s']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '01/01/2001'], ['x' => 'date_format:Y-m-d']);
$this->assertTrue($v->fails());

Expand Down Expand Up @@ -2335,6 +2332,21 @@ public function testValidateDateAndFormat()

$v = new Validator($trans, ['x' => '2000-01-01T00:00:00+00:30'], ['x' => 'date_format:Y-m-d\TH:i:sP']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '2000-01-01 17:43:59'], ['x' => 'date_format:Y-m-d H:i:s']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '2000-01-01 17:43:59'], ['x' => 'date_format:H:i:s']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:43:59'], ['x' => 'date_format:H:i:s']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '17:43:59'], ['x' => 'date_format:H:i']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:43'], ['x' => 'date_format:H:i']);
$this->assertTrue($v->passes());
}

public function testBeforeAndAfter()
Expand Down Expand Up @@ -2391,6 +2403,30 @@ public function testBeforeAndAfter()

$v = new Validator($trans, ['start' => 'today', 'ends' => 'tomorrow'], ['start' => 'Before:ends', 'ends' => 'After:start']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '2012-01-01 17:43:59'], ['x' => 'Before:2012-01-01 17:44|After:2012-01-01 17:43:58']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '2012-01-01 17:44:01'], ['x' => 'Before:2012-01-01 17:44:02|After:2012-01-01 17:44']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '2012-01-01 17:44'], ['x' => 'Before:2012-01-01 17:44:00']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '2012-01-01 17:44'], ['x' => 'After:2012-01-01 17:44:00']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:43:59'], ['x' => 'Before:17:44|After:17:43:58']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '17:44:01'], ['x' => 'Before:17:44:02|After:17:44']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '17:44'], ['x' => 'Before:17:44:00']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:44'], ['x' => 'After:17:44:00']);
$this->assertTrue($v->fails());
}

public function testBeforeAndAfterWithFormat()
Expand Down Expand Up @@ -2445,13 +2481,46 @@ 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());

$v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before:2012-01-01 17:44:01|after:2012-01-01 17:43:59']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before:2012-01-01 17:44:00']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|after:2012-01-01 17:44:00']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before:17:44:01|after:17:43:59']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before:17:44:00']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|after:17:44:00']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before:17:45|after:17:43']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before:17:44']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|after:17:44']);
$this->assertTrue($v->fails());
}

Expand All @@ -2468,10 +2537,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' => '31/12/2012'], ['x' => 'date_format:d/m/Y|before_or_equal:29/12/2012']);
$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' => 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 +2561,46 @@ 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' => '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:yesterday']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after_or_equal:tomorrow']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before_or_equal:2012-01-01 17:44:00|after_or_equal:2012-01-01 17:44:00']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before_or_equal:2012-01-01 17:43:59']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|after_or_equal:2012-01-01 17:44:01']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before_or_equal:17:44:00|after_or_equal:17:44:00']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before_or_equal:17:43:59']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|after_or_equal:17:44:01']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before_or_equal:17:44|after_or_equal:17:44']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before_or_equal:17:43']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|after_or_equal:17:45']);
$this->assertTrue($v->fails());
}

Expand Down