Email.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\Core\Render\Element;
  5. /**
  6. * Provides a form input element for entering an email address.
  7. *
  8. * Properties:
  9. * - #default_value: An RFC-compliant email address.
  10. * - #size: The size of the input element in characters.
  11. * - #pattern: A string for the native HTML5 pattern attribute.
  12. *
  13. * Example usage:
  14. * @code
  15. * $form['email'] = array(
  16. * '#type' => 'email',
  17. * '#title' => $this->t('Email'),
  18. * '#pattern' => '*@example.com',
  19. * );
  20. * @end
  21. *
  22. * @see \Drupal\Core\Render\Element\Render\Textfield
  23. *
  24. * @FormElement("email")
  25. */
  26. class Email extends FormElement {
  27. /**
  28. * Defines the max length for an email address
  29. *
  30. * The maximum length of an email address is 254 characters. RFC 3696
  31. * specifies a total length of 320 characters, but mentions that
  32. * addresses longer than 256 characters are not normally useful. Erratum
  33. * 1690 was then released which corrected this value to 254 characters.
  34. * @see http://tools.ietf.org/html/rfc3696#section-3
  35. * @see http://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690
  36. */
  37. const EMAIL_MAX_LENGTH = 254;
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getInfo() {
  42. $class = get_class($this);
  43. return [
  44. '#input' => TRUE,
  45. '#size' => 60,
  46. '#maxlength' => self::EMAIL_MAX_LENGTH,
  47. '#autocomplete_route_name' => FALSE,
  48. '#process' => [
  49. [$class, 'processAutocomplete'],
  50. [$class, 'processAjaxForm'],
  51. [$class, 'processPattern'],
  52. ],
  53. '#element_validate' => [
  54. [$class, 'validateEmail'],
  55. ],
  56. '#pre_render' => [
  57. [$class, 'preRenderEmail'],
  58. ],
  59. '#theme' => 'input__email',
  60. '#theme_wrappers' => ['form_element'],
  61. ];
  62. }
  63. /**
  64. * Form element validation handler for #type 'email'.
  65. *
  66. * Note that #maxlength and #required is validated by _form_validate() already.
  67. */
  68. public static function validateEmail(&$element, FormStateInterface $form_state, &$complete_form) {
  69. $value = trim($element['#value']);
  70. $form_state->setValueForElement($element, $value);
  71. if ($value !== '' && !\Drupal::service('email.validator')->isValid($value)) {
  72. $form_state->setError($element, t('The email address %mail is not valid.', ['%mail' => $value]));
  73. }
  74. }
  75. /**
  76. * Prepares a #type 'email' render element for input.html.twig.
  77. *
  78. * @param array $element
  79. * An associative array containing the properties of the element.
  80. * Properties used: #title, #value, #description, #size, #maxlength,
  81. * #placeholder, #required, #attributes.
  82. *
  83. * @return array
  84. * The $element with prepared variables ready for input.html.twig.
  85. */
  86. public static function preRenderEmail($element) {
  87. $element['#attributes']['type'] = 'email';
  88. Element::setAttributes($element, ['id', 'name', 'value', 'size', 'maxlength', 'placeholder']);
  89. static::setAttributes($element, ['form-email']);
  90. return $element;
  91. }
  92. }