public static function Number::validateNumber in Price 3.0.x
Same name and namespace in other branches
- 8 src/Element/Number.php \Drupal\price\Element\Number::validateNumber()
- 3.x src/Element/Number.php \Drupal\price\Element\Number::validateNumber()
- 2.0.x src/Element/Number.php \Drupal\price\Element\Number::validateNumber()
- 2.x src/Element/Number.php \Drupal\price\Element\Number::validateNumber()
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 126
Class
- Number
- Provides a number form element with support for language-specific input.
Namespace
Drupal\price\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('price.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);
}