function NumericItemBase::validateMinAndMaxConfig

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php \Drupal\Core\Field\Plugin\Field\FieldType\NumericItemBase::validateMinAndMaxConfig()

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.

1 call to NumericItemBase::validateMinAndMaxConfig()
NumberItemTest::testFormFieldMinMaxValue in core/modules/field/tests/src/Kernel/Number/NumberItemTest.php
Tests the validation of minimum and maximum values.

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;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.