Problem/Motivation

For a custom field of type numeric, one cannot set both min and max field values. (I've tried this with the decimal numeric field type.) If you do set both, when saving the content, it leads to the following error message:

The website encountered an unexpected error. Try again later.
Symfony\Component\Validator\Exception\ConstraintDefinitionException: The "Drupal\Core\Validation\Plugin\Validation\Constraint\RangeConstraint" constraint can not use "minMessage" and "maxMessage" when the "min" and "max" options are both set. Use "notInRangeMessage" instead. in Symfony\Component\Validator\Constraints\Range->__construct() (line 103 of /.../vendor/symfony/validator/Constraints/Range.php).

I suppose the problem is with the public function getConstraints(array $settings): (in DecimalType.php) which provides validations for the min / max parameters only when set separately.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

marksmith created an issue. See original summary.

marksmith’s picture

The following code appears to solve the issue:

/**
 * {@inheritdoc}
 */
public function getConstraints(array $settings): array {
  $constraints['Regex']['pattern'] = '/^[+-]?((\d+(\.\d*)?)|(\.\d+))$/i';
  
  // Handle range constraints
  if ((isset($settings['min']) && $settings['min'] !== '') || 
      (isset($settings['max']) && $settings['max'] !== '')) {
    
    // If both min and max are set, use notInRangeMessage
    if ((isset($settings['min']) && $settings['min'] !== '') && 
        (isset($settings['max']) && $settings['max'] !== '')) {
      
      $min = $settings['min'];
      $max = $settings['max'];
      $constraints['Range']['min'] = $min;
      $constraints['Range']['max'] = $max;
      $constraints['Range']['notInRangeMessage'] = $this->t('%name: the value must be between %min and %max.', [
        '%name' => $settings['name'],
        '%min' => $min,
        '%max' => $max,
      ]);
    }
    // Only min is set
    elseif (isset($settings['min']) && $settings['min'] !== '') {
      $min = $settings['min'];
      $constraints['Range']['min'] = $min;
      $constraints['Range']['minMessage'] = $this->t('%name: the value may be no less than %min.', [
        '%name' => $settings['name'],
        '%min' => $min,
      ]);
    }
    // Only max is set
    elseif (isset($settings['max']) && $settings['max'] !== '') {
      $max = $settings['max'];
      $constraints['Range']['max'] = $max;
      $constraints['Range']['maxMessage'] = $this->t('%name: the value may be no greater than %max.', [
        '%name' => $settings['name'],
        '%max' => $max,
      ]);
    }
  }
  
  return $constraints;
}

I suppose, something similar should also be added to the IntegerType.php as well.

apmsooner’s picture

Okay thanks for the report. I'll put together a patch soon for this issue.

apmsooner’s picture

Version: 3.1.6 » 3.1.x-dev
Status: Active » Needs review

Try the patch please.

apmsooner’s picture

Status: Needs review » Fixed

  • apmsooner committed 277c6878 on 3.1.x
    Issue #3518147 by apmsooner, marksmith: Cannot set both min and max...
apmsooner’s picture

Status: Fixed » Closed (fixed)