public static function Number::validateNumber in Physical Fields 8
Validates the number element.
Converts the number back to the standard format (e.g. "9,99" -> "9.99").
Parameters
array $element: The form element.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
File
- src/
Element/ Number.php, line 113
Class
- Number
- Provides a number form element with support for language-specific input.
Namespace
Drupal\physical\ElementCode
public static function validateNumber(array $element, FormStateInterface $form_state) {
$value = trim($element['#value']);
if ($value === '') {
return;
}
$title = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
$number_formatter = \Drupal::service('physical.number_formatter');
$value = $number_formatter
->parse($value);
if ($value === FALSE) {
$form_state
->setError($element, t('%title must be a number.', [
'%title' => $title,
]));
return;
}
if (isset($element['#min']) && $value < $element['#min']) {
$form_state
->setError($element, t('%title must be higher than or equal to %min.', [
'%title' => $title,
'%min' => $element['#min'],
]));
return;
}
if (isset($element['#max']) && $value > $element['#max']) {
$form_state
->setError($element, t('%title must be lower than or equal to %max.', [
'%title' => $title,
'%max' => $element['#max'],
]));
return;
}
$form_state
->setValueForElement($element, $value);
}