Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Render / Element / Number.php
1 <?php
2
3 namespace Drupal\Core\Render\Element;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element;
7 use Drupal\Component\Utility\Number as NumberUtility;
8
9 /**
10  * Provides a form element for numeric input, with special numeric validation.
11  *
12  * Properties:
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,
18  *   etc.
19  * - #size: The size of the input element in characters.
20  *
21  * Usage example:
22  * @code
23  * $form['quantity'] = array(
24  *   '#type' => 'number',
25  *   '#title' => $this->t('Quantity'),
26  * );
27  * @endcode
28  *
29  * @see \Drupal\Core\Render\Element\Range
30  * @see \Drupal\Core\Render\Element\Textfield
31  *
32  * @FormElement("number")
33  */
34 class Number extends FormElement {
35
36   /**
37    * {@inheritdoc}
38    */
39   public function getInfo() {
40     $class = get_class($this);
41     return [
42       '#input' => TRUE,
43       '#step' => 1,
44       '#process' => [
45         [$class, 'processAjaxForm'],
46       ],
47       '#element_validate' => [
48         [$class, 'validateNumber'],
49       ],
50       '#pre_render' => [
51         [$class, 'preRenderNumber'],
52       ],
53       '#theme' => 'input__number',
54       '#theme_wrappers' => ['form_element'],
55     ];
56   }
57
58   /**
59    * Form element validation handler for #type 'number'.
60    *
61    * Note that #required is validated by _form_validate() already.
62    */
63   public static function validateNumber(&$element, FormStateInterface $form_state, &$complete_form) {
64     $value = $element['#value'];
65     if ($value === '') {
66       return;
67     }
68
69     $name = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
70
71     // Ensure the input is numeric.
72     if (!is_numeric($value)) {
73       $form_state->setError($element, t('%name must be a number.', ['%name' => $name]));
74       return;
75     }
76
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']]));
80     }
81
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']]));
85     }
86
87     if (isset($element['#step']) && strtolower($element['#step']) != 'any') {
88       // Check that the input is an allowed multiple of #step (offset by #min if
89       // #min is set).
90       $offset = isset($element['#min']) ? $element['#min'] : 0.0;
91
92       if (!NumberUtility::validStep($value, $element['#step'], $offset)) {
93         $form_state->setError($element, t('%name is not a valid number.', ['%name' => $name]));
94       }
95     }
96   }
97
98   /**
99    * Prepares a #type 'number' render element for input.html.twig.
100    *
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.
105    *
106    * @return array
107    *   The $element with prepared variables ready for input.html.twig.
108    */
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']);
113
114     return $element;
115   }
116
117 }