Verified Commit 64aebe15 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3276196 by mondrake, catch, Spokje: The...

Issue #3276196 by mondrake, catch, Spokje: The "Symfony\Component\Validator\Constraints\Range::$minMessage" property is considered final
parent 810334c7
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -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);
  }

}
+16 −2
Original line number Diff line number Diff line
@@ -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))
@@ -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))
+2 −2
Original line number Diff line number Diff line
@@ -183,7 +183,7 @@ 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.
@@ -191,7 +191,7 @@ protected function doFieldPropertyConstraintsTests() {
    $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());
  }

  /**