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.2] Validation of dates - Dealing with DateTime instances #13844

Merged
merged 2 commits into from
Jun 3, 2016
Merged
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
28 changes: 19 additions & 9 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Countable;
use Exception;
use DateTimeZone;
use Carbon\Carbon;
use RuntimeException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -1758,19 +1757,19 @@ protected function validateBefore($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'before');

if (! is_string($value) && ! is_numeric($value) && ! $value instanceof Carbon) {
if (! is_string($value) && ! is_numeric($value) && ! $value instanceof DateTime) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to check for DateTimeInterface perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good! Taylor already did, btw.

return false;
}

if ($format = $this->getDateFormat($attribute)) {
return $this->validateBeforeWithFormat($format, $value, $parameters);
}

if (! ($date = strtotime($parameters[0]))) {
return strtotime($value) < strtotime($this->getValue($parameters[0]));
if (! $date = $this->getDateTimestamp($parameters[0])) {
$date = $this->getDateTimestamp($this->getValue($parameters[0]));
}

return strtotime($value) < $date;
return $this->getDateTimestamp($value) < $date;
}

/**
Expand Down Expand Up @@ -1800,19 +1799,19 @@ protected function validateAfter($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'after');

if (! is_string($value) && ! is_numeric($value) && ! $value instanceof Carbon) {
if (! is_string($value) && ! is_numeric($value) && ! $value instanceof DateTime) {
return false;
}

if ($format = $this->getDateFormat($attribute)) {
return $this->validateAfterWithFormat($format, $value, $parameters);
}

if (! ($date = strtotime($parameters[0]))) {
return strtotime($value) > strtotime($this->getValue($parameters[0]));
if (! $date = $this->getDateTimestamp($parameters[0])) {
$date = $this->getDateTimestamp($this->getValue($parameters[0]));
}

return strtotime($value) > $date;
return $this->getDateTimestamp($value) > $date;
}

/**
Expand Down Expand Up @@ -1900,6 +1899,17 @@ protected function getDateFormat($attribute)
}
}

/**
* Get the date timestamp.
*
* @param $value
* @return mixed
*/
protected function getDateTimestamp($value)
{
return $value instanceof DateTime ? $value->getTimestamp() : strtotime($value);
}

/**
* Get the validation message for an attribute and rule.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1916,12 +1916,18 @@ public function testValidateDateAndFormat()
$v = new Validator($trans, ['x' => '01/01/2000'], ['x' => 'date']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['x' => '1325376000'], ['x' => 'date']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => 'Not a date'], ['x' => 'date']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => ['Not', 'a', 'date']], ['x' => 'date']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => new DateTime()], ['x' => 'date']);
$this->assertTrue($v->passes());

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

Expand Down Expand Up @@ -1977,6 +1983,21 @@ public function testBeforeAndAfter()

$v = new Validator($trans, ['start' => '2012-01-01', 'ends' => '2000-01-01'], ['start' => 'Before:ends', 'ends' => 'After:start']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['x' => new DateTime('2000-01-01')], ['x' => 'Before:2012-01-01']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['start' => new DateTime('2012-01-01'), 'ends' => new Carbon('2013-01-01')], ['start' => 'Before:ends', 'ends' => 'After:start']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['start' => '2012-01-01', 'ends' => new DateTime('2013-01-01')], ['start' => 'Before:ends', 'ends' => 'After:start']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['start' => new DateTime('2012-01-01'), 'ends' => new DateTime('2000-01-01')], ['start' => 'After:2000-01-01', 'ends' => 'After:start']);
$this->assertTrue($v->fails());

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

public function testBeforeAndAfterWithFormat()
Expand Down