Skip to content
Snippets Groups Projects
Verified Commit d4ae87ea authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3074645 by mikelutz: Account for changing violation message in symfony...

Issue #3074645 by mikelutz: Account for changing violation message in symfony 'range' constraint between SF3 and SF4.4
parent 1b077886
No related branches found
No related tags found
No related merge requests found
......@@ -22,11 +22,4 @@ class RangeConstraint extends Range {
public $minMessage = 'This value should be %limit or more.';
public $maxMessage = 'This value should be %limit or less.';
/**
* {@inheritdoc}
*/
public function validatedBy() {
return '\Symfony\Component\Validator\Constraints\RangeValidator';
}
}
<?php
namespace Drupal\Core\Validation\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Validator for the Drupal 'range' constraint.
*/
class RangeConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
if (!$constraint instanceof Range) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\Range');
}
if (NULL === $value) {
return;
}
if (!is_numeric($value) && !$value instanceof \DateTimeInterface) {
$this->context->buildViolation($constraint->invalidMessage)
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setCode(Range::INVALID_CHARACTERS_ERROR)
->addViolation();
return;
}
$min = $constraint->min;
$max = $constraint->max;
// Convert strings to DateTimes if comparing another DateTime.
// This allows to compare with any date/time value supported by
// the DateTime constructor.
// @see http://php.net/manual/en/datetime.formats.php
if ($value instanceof \DateTimeInterface) {
if (\is_string($min)) {
$min = new \DateTime($min);
}
if (\is_string($max)) {
$max = new \DateTime($max);
}
}
if (NULL !== $constraint->max && $value > $max) {
$this->context->buildViolation($constraint->maxMessage)
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
->setCode(Range::TOO_HIGH_ERROR)
->addViolation();
return;
}
if (NULL !== $constraint->min && $value < $min) {
$this->context->buildViolation($constraint->minMessage)
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
->setCode(Range::TOO_LOW_ERROR)
->addViolation();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment