Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @file
  5. * Contains \Drupal\page_example\Form\PageExampleForm.
  6. */
  7.  
  8. namespace Drupal\page_example\Form;
  9.  
  10. use Drupal\Core\Form\FormBase;
  11. use Drupal\Core\Form\FormStateInterface;
  12.  
  13. class PageExampleForm extends FormBase {
  14.  
  15. /**
  16. * {@inheritdoc}.
  17. */
  18. public function getFormId() {
  19. return 'page_example_form';
  20. }
  21.  
  22. /**
  23. * {@inheritdoc}.
  24. */
  25. public function buildForm(array $form, FormStateInterface $form_state) {
  26. $form['email'] = [
  27. '#type' => 'email',
  28. '#title' => $this->t('Your .com email address.')
  29. ];
  30. $form['show'] = [
  31. '#type' => 'submit',
  32. '#value' => $this->t('Submit'),
  33. ];
  34. return $form;
  35. }
  36.  
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function validateForm(array &$form, FormStateInterface $form_state) {
  41. if (strpos($form_state->getValue('email'), '.com') === FALSE) {
  42. $form_state->setErrorByName('email', $this->t('This is not a .com email address.'));
  43. }
  44. }
  45.  
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function submitForm(array &$form, FormStateInterface $form_state) {
  50. drupal_set_message($this->t('Your email address is @email', ['@email' => $form_state->getValue('email')]));
  51. }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement