Cannot set both min and max values for numeric fields

Created on 9 April 2025, about 1 month ago

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.

🐛 Bug report
Status

Active

Version

3.1

Component

Code

Created by

🇮🇪Ireland marksmith

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @marksmith
  • 🇮🇪Ireland marksmith

    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.

  • 🇺🇸United States apmsooner

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

  • Merge request !117Fix constraints for numeric field types. → (Merged) created by apmsooner
  • 🇺🇸United States apmsooner

    Try the patch please.

  • Pipeline finished with Skipped
    26 days ago
    #473394
    • apmsooner committed 277c6878 on 3.1.x
      Issue #3518147 by apmsooner, marksmith: Cannot set both min and max...
Production build 0.71.5 2024