Skip to content

Commit

Permalink
Issue #3276196 by mondrake, catch, Spokje: The "Symfony\Component\Val…
Browse files Browse the repository at this point in the history
…idator\Constraints\Range::$minMessage" property is considered final
  • Loading branch information
alexpott committed May 16, 2022
1 parent 5114668 commit a2ce368
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@
*/
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 __construct(array $options = NULL) {
if (isset($options['min']) && isset($options['max'])) {
$options['notInRangeMessage'] = $options['notInRangeMessage'] ?? 'This value should be between %min and %max.';
}
else {
$options['minMessage'] = $options['minMessage'] ?? 'This value should be %limit or more.';
$options['maxMessage'] = $options['maxMessage'] ?? 'This value should be %limit or less.';
}
parent::__construct($options);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@ public function validate($value, Constraint $constraint) {
}
}

if (NULL !== $constraint->max && $value > $max) {
$hasLowerLimit = NULL !== $constraint->min;
$hasUpperLimit = NULL !== $constraint->max;

if ($hasLowerLimit && $hasUpperLimit && ($value < $min || $value > $max)) {
$this->context->buildViolation($constraint->notInRangeMessage)
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setParameter('{{ min }}', $this->formatValue($min, self::PRETTY_DATE))
->setParameter('{{ max }}', $this->formatValue($max, self::PRETTY_DATE))
->setCode(Range::NOT_IN_RANGE_ERROR)
->addViolation();

return;
}

if ($hasUpperLimit && $value > $max) {
$this->context->buildViolation($constraint->maxMessage)
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
Expand All @@ -60,7 +74,7 @@ public function validate($value, Constraint $constraint) {
return;
}

if (NULL !== $constraint->min && $value < $min) {
if ($hasLowerLimit && $value < $min) {
$this->context->buildViolation($constraint->minMessage)
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
Expand Down
4 changes: 2 additions & 2 deletions modules/field/tests/src/Kernel/FieldCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ protected function doFieldPropertyConstraintsTests() {
$this->assertEquals(t('%name does not accept the value @value.', ['%name' => $field_name, '@value' => -2]), $violations[0]->getMessage());

$this->assertEquals($field_name . '.0.value', $violations[1]->getPropertyPath());
$this->assertEquals(t('This value should be %limit or more.', ['%limit' => 0]), $violations[1]->getMessage());
$this->assertEquals(t('This value should be between %min and %max.', ['%min' => 0, '%max' => 32]), $violations[1]->getMessage());

// Check that a value that is not specifically restricted but outside the
// range triggers the expected violation.
$entity->set($field_name, 33);
$violations = $entity->validate();
$this->assertCount(1, $violations, 'Violations found when using value outside the range.');
$this->assertEquals($field_name . '.0.value', $violations[0]->getPropertyPath());
$this->assertEquals(t('This value should be %limit or less.', ['%limit' => 32]), $violations[0]->getMessage());
$this->assertEquals(t('This value should be between %min and %max.', ['%min' => 0, '%max' => 32]), $violations[0]->getMessage());
}

/**
Expand Down

0 comments on commit a2ce368

Please sign in to comment.