3 namespace Drupal\Core\Render\Element;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element;
7 use Drupal\Component\Utility\Number as NumberUtility;
10 * Provides a form element for numeric input, with special numeric validation.
13 * - #default_value: A valid floating point number.
14 * - #min: Minimum value.
15 * - #max: Maximum value.
16 * - #step: Ensures that the number is an even multiple of step, offset by #min
17 * if specified. A #min of 1 and a #step of 2 would allow values of 1, 3, 5,
19 * - #size: The size of the input element in characters.
23 * $form['quantity'] = array(
24 * '#type' => 'number',
25 * '#title' => $this->t('Quantity'),
29 * @see \Drupal\Core\Render\Element\Range
30 * @see \Drupal\Core\Render\Element\Textfield
32 * @FormElement("number")
34 class Number extends FormElement {
39 public function getInfo() {
40 $class = get_class($this);
45 [$class, 'processAjaxForm'],
47 '#element_validate' => [
48 [$class, 'validateNumber'],
51 [$class, 'preRenderNumber'],
53 '#theme' => 'input__number',
54 '#theme_wrappers' => ['form_element'],
59 * Form element validation handler for #type 'number'.
61 * Note that #required is validated by _form_validate() already.
63 public static function validateNumber(&$element, FormStateInterface $form_state, &$complete_form) {
64 $value = $element['#value'];
69 $name = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
71 // Ensure the input is numeric.
72 if (!is_numeric($value)) {
73 $form_state->setError($element, t('%name must be a number.', ['%name' => $name]));
77 // Ensure that the input is greater than the #min property, if set.
78 if (isset($element['#min']) && $value < $element['#min']) {
79 $form_state->setError($element, t('%name must be higher than or equal to %min.', ['%name' => $name, '%min' => $element['#min']]));
82 // Ensure that the input is less than the #max property, if set.
83 if (isset($element['#max']) && $value > $element['#max']) {
84 $form_state->setError($element, t('%name must be lower than or equal to %max.', ['%name' => $name, '%max' => $element['#max']]));
87 if (isset($element['#step']) && strtolower($element['#step']) != 'any') {
88 // Check that the input is an allowed multiple of #step (offset by #min if
90 $offset = isset($element['#min']) ? $element['#min'] : 0.0;
92 if (!NumberUtility::validStep($value, $element['#step'], $offset)) {
93 $form_state->setError($element, t('%name is not a valid number.', ['%name' => $name]));
99 * Prepares a #type 'number' render element for input.html.twig.
101 * @param array $element
102 * An associative array containing the properties of the element.
103 * Properties used: #title, #value, #description, #min, #max, #placeholder,
104 * #required, #attributes, #step, #size.
107 * The $element with prepared variables ready for input.html.twig.
109 public static function preRenderNumber($element) {
110 $element['#attributes']['type'] = 'number';
111 Element::setAttributes($element, ['id', 'name', 'value', 'step', 'min', 'max', 'placeholder', 'size']);
112 static::setAttributes($element, ['form-number']);