Number.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\Core\Render\Element;
  5. use Drupal\Component\Utility\Number as NumberUtility;
  6. /**
  7. * Provides a form element for numeric input, with special numeric validation.
  8. *
  9. * Properties:
  10. * - #default_value: A valid floating point number.
  11. * - #min: Minimum value.
  12. * - #max: Maximum value.
  13. * - #step: Ensures that the number is an even multiple of step, offset by #min
  14. * if specified. A #min of 1 and a #step of 2 would allow values of 1, 3, 5,
  15. * etc.
  16. * - #size: The size of the input element in characters.
  17. *
  18. * Usage example:
  19. * @code
  20. * $form['quantity'] = array(
  21. * '#type' => 'number',
  22. * '#title' => $this->t('Quantity'),
  23. * );
  24. * @endcode
  25. *
  26. * @see \Drupal\Core\Render\Element\Range
  27. * @see \Drupal\Core\Render\Element\Textfield
  28. *
  29. * @FormElement("number")
  30. */
  31. class Number extends FormElement {
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getInfo() {
  36. $class = get_class($this);
  37. return [
  38. '#input' => TRUE,
  39. '#step' => 1,
  40. '#process' => [
  41. [$class, 'processAjaxForm'],
  42. ],
  43. '#element_validate' => [
  44. [$class, 'validateNumber'],
  45. ],
  46. '#pre_render' => [
  47. [$class, 'preRenderNumber'],
  48. ],
  49. '#theme' => 'input__number',
  50. '#theme_wrappers' => ['form_element'],
  51. ];
  52. }
  53. /**
  54. * Form element validation handler for #type 'number'.
  55. *
  56. * Note that #required is validated by _form_validate() already.
  57. */
  58. public static function validateNumber(&$element, FormStateInterface $form_state, &$complete_form) {
  59. $value = $element['#value'];
  60. if ($value === '') {
  61. return;
  62. }
  63. $name = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
  64. // Ensure the input is numeric.
  65. if (!is_numeric($value)) {
  66. $form_state->setError($element, t('%name must be a number.', ['%name' => $name]));
  67. return;
  68. }
  69. // Ensure that the input is greater than the #min property, if set.
  70. if (isset($element['#min']) && $value < $element['#min']) {
  71. $form_state->setError($element, t('%name must be higher than or equal to %min.', ['%name' => $name, '%min' => $element['#min']]));
  72. }
  73. // Ensure that the input is less than the #max property, if set.
  74. if (isset($element['#max']) && $value > $element['#max']) {
  75. $form_state->setError($element, t('%name must be lower than or equal to %max.', ['%name' => $name, '%max' => $element['#max']]));
  76. }
  77. if (isset($element['#step']) && strtolower($element['#step']) != 'any') {
  78. // Check that the input is an allowed multiple of #step (offset by #min if
  79. // #min is set).
  80. $offset = isset($element['#min']) ? $element['#min'] : 0.0;
  81. if (!NumberUtility::validStep($value, $element['#step'], $offset)) {
  82. $form_state->setError($element, t('%name is not a valid number.', ['%name' => $name]));
  83. }
  84. }
  85. }
  86. /**
  87. * Prepares a #type 'number' render element for input.html.twig.
  88. *
  89. * @param array $element
  90. * An associative array containing the properties of the element.
  91. * Properties used: #title, #value, #description, #min, #max, #placeholder,
  92. * #required, #attributes, #step, #size.
  93. *
  94. * @return array
  95. * The $element with prepared variables ready for input.html.twig.
  96. */
  97. public static function preRenderNumber($element) {
  98. $element['#attributes']['type'] = 'number';
  99. Element::setAttributes($element, ['id', 'name', 'value', 'step', 'min', 'max', 'placeholder', 'size']);
  100. static::setAttributes($element, ['form-number']);
  101. return $element;
  102. }
  103. }