Skip to content

Commit

Permalink
[5.2] Validation of dates - Dealing with DateTime instances
Browse files Browse the repository at this point in the history
  • Loading branch information
KennedyTedesco committed Jun 3, 2016
1 parent 13c981a commit 7bf4336
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1758,19 +1758,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) {
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 +1800,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 +1900,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

0 comments on commit 7bf4336

Please sign in to comment.