Skip to content

[Validator] DateRange constraint added #7766

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
46 changes: 46 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraints\Range;

/**
* @Annotation
*
* @api
*
* @author Alexander Volochnev <admin@toplimit.ru>
*/
class DateRange extends Range
{
public $format = "d/m/Y";
public $minMessage = 'This date should be {{ limit }} or later.';
public $maxMessage = 'This date should be {{ limit }} or earlier.';
public $invalidMessage = 'This value should be a valid date.';

public function __construct($options = null)
{
parent::__construct($options);

if (isset($options['format']) && null !== $options['format']) {
$this->format = $options['format'];
}

if (null !== $this->min) {
$this->min = \DateTime::createFromFormat($this->format, $this->min);
}

if (null !== $this->max) {
$this->max = \DateTime::createFromFormat($this->format, $this->max);
}
}
}
53 changes: 53 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateRangeValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Constraint;

/**
* @author Alexander Volochnev <admin@toplimit.ru>
*/
class DateRangeValidator extends ConstraintValidator
{
/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
{
if (null === $value) {
return;
}

if (!$value instanceof \DateTime) {
$this->context->addViolation($constraint->invalidMessage);

return;
}

if (null !== $constraint->max && $value > $constraint->max) {
$this->context->addViolation($constraint->maxMessage, array(
'{{ value }}' => $value->format($constraint->format),
'{{ limit }}' => $constraint->max->format($constraint->format),
));

return;
}

if (null !== $constraint->min && $value < $constraint->min) {
$this->context->addViolation($constraint->minMessage, array(
'{{ value }}' => $value->format($constraint->format),
'{{ limit }}' => $constraint->min->format($constraint->format),
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@
<source>This value is not a valid ISSN.</source>
<target>This value is not a valid ISSN.</target>
</trans-unit>
<trans-unit id="64">
<source>This value should be a valid date.</source>
<target>This value should be a valid date.</target>
</trans-unit>
<trans-unit id="65">
<source>This date should be {{ limit }} or earlier.</source>
<target>This date should be {{ limit }} or earlier.</target>
</trans-unit>
<trans-unit id="66">
<source>This value should be {{ limit }} or later.</source>
Copy link
Contributor

Choose a reason for hiding this comment

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

value -> date

Copy link
Member

Choose a reason for hiding this comment

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

The other way round: date should be value above.

<target>This value should be {{ limit }} or later.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@
<source>This value is neither a valid ISBN-10 nor a valid ISBN-13.</source>
<target>Значение не соответствует форматам ISBN-10 и ISBN-13</target>
</trans-unit>
<trans-unit id="63">
<source>This value should be a valid date.</source>
<target>Значение должно быть датой.</target>
</trans-unit>
<trans-unit id="64">
<source>This date should be {{ limit }} or earlier.</source>
<target>Дата должна быть {{ limit }} или ранее.</target>
</trans-unit>
<trans-unit id="65">
<source>This value should be {{ limit }} or later.</source>
<target>Дата должна быть {{ limit }} или позднее.</target>
</trans-unit>
</body>
</file>
</xliff>