Validates that the minimum value is less than the maximum.

Parameters

array[] $element: The numeric element to be validated.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array[] $complete_form: The complete form structure.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php, line 138

Class

NumericItemBase
Base class for numeric configurable field types.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public static function validateMinAndMaxConfig(array &$element, FormStateInterface &$form_state, array &$complete_form) : void {
  $settingsValue = $form_state
    ->getValue('settings');

  // Ensure that the minimum and maximum are numeric.
  $minValue = is_numeric($settingsValue['min']) ? (double) $settingsValue['min'] : NULL;
  $maxValue = is_numeric($settingsValue['max']) ? (double) $settingsValue['max'] : NULL;

  // Only proceed with validation if both values are numeric.
  if ($minValue === NULL || $maxValue === NULL) {
    return;
  }
  if ($minValue > $maxValue) {
    $form_state
      ->setError($element, t('The minimum value must be less than or equal to %max.', [
      '%max' => $maxValue,
    ]));
    return;
  }
}