Skip to content

[Validator] added before and after options to Date constraints #8300

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

Closed
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
class Date extends Constraint
{
public $message = 'This value is not a valid date.';
public $after;
public $before;
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
class DateTime extends Constraint
{
public $message = 'This value is not a valid datetime.';
public $after;
public $before;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
*/
class DateTimeValidator extends DateValidator
{
const FORMAT = 'Y-m-d H:i:s';
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/';
}
33 changes: 27 additions & 6 deletions src/Symfony/Component/Validator/Constraints/DateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,46 @@
*/
class DateValidator extends ConstraintValidator
{
const FORMAT = 'Y-m-d';
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2})$/';

/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
{
if (null === $value || '' === $value || $value instanceof \DateTime) {
if (null === $value || '' === $value) {
return;
}

if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
if (!$value instanceof \DateTime) {
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}

$value = (string) $value;

if (!preg_match(static::PATTERN, $value, $matches) || !checkdate($matches[2], $matches[3], $matches[1])) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
}
}

if (!$constraint->before && !$constraint->after) {
return;
}

if ($value instanceof \DateTime) {
$timestamp = $value->getTimestamp();
} else {
$timestamp = strtotime($value);
}

$value = (string) $value;
if ($constraint->before && $timestamp > strtotime($constraint->before)) {
$this->context->addViolation($constraint->message, array('{{ value }}' => date(static::FORMAT, $timestamp)));
}

if (!preg_match(static::PATTERN, $value, $matches) || !checkdate($matches[2], $matches[3], $matches[1])) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
if ($constraint->after && $timestamp < strtotime($constraint->after)) {
$this->context->addViolation($constraint->message, array('{{ value }}' => date(static::FORMAT, $timestamp)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,33 @@ public function getInvalidDateTimes()
array('2010-01-01 00:00:60'),
);
}

/**
* @dataProvider getBeforeAndAfter
*/
public function testBeforeAndAfter($date, $before, $after, $valid)
{
$this->context->expects($valid ? $this->never() : $this->once())
->method('addViolation');

$this->validator->validate($date, new DateTime(array(
'before' => $before,
'after' => $after,
)));
}

public function getBeforeAndAfter()
{
return array(
array('2010-01-02 15:00:33', '2010-01-02 16:30:00', '2010-01-01', true),
array('2008-01-02 12:00:00', '2012-02-02', '2010-01-01', false),
array('2010-01-02 00:00:00', '2012-02-02', null, true),
array('2013-01-02 01:02:03', '2012-02-02', null, false),
array('2010-01-02 10:10:10', null, '2012-02-02', false),
array('2013-01-02 15:12:00', null, '2012-02-02', true),
array(date('Y-m-d H:i:s', strtotime('+1 day')), null, 'now', true),
array(new \DateTime('-1 hour'), 'now', null, true),
array(new \DateTime('-5 minutes'), 'now', '-3 minutes', false),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,33 @@ public function getInvalidDates()
array('2010-02-29'),
);
}

/**
* @dataProvider getBeforeAndAfter
*/
public function testBeforeAndAfter($date, $before, $after, $valid)
{
$this->context->expects($valid ? $this->never() : $this->once())
->method('addViolation');

$this->validator->validate($date, new Date(array(
'before' => $before,
'after' => $after,
)));
}

public function getBeforeAndAfter()
{
return array(
array('2010-01-02', '2012-02-02', '2010-01-01', true),
array('2008-01-02', '2012-02-02', '2010-01-01', false),
array('2010-01-02', '2012-02-02', null, true),
array('2013-01-02', '2012-02-02', null, false),
array('2010-01-02', null, '2012-02-02', false),
array('2013-01-02', null, '2012-02-02', true),
array(date('Y-m-d', strtotime('+1 day')), null, 'now', true),
array(new \DateTime('-1 day'), 'now', null, true),
array(new \DateTime('-5 days'), 'now', '-3 days', false),
);
}
}