Number.php 3.5 KB

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