· 7 years ago · Jan 12, 2019, 07:06 PM
1<?php
2
3/**
4 * @file
5 * Contains Drupalcontact_formFormContributeForm.
6 */
7
8namespace Drupalcontact_formForm;
9use DrupalCoreDependencyInjectionContainerInjectionInterface;
10use DrupalCoreFormFormBase;
11use DrupalCoreFormFormInterface;
12use DrupalCoreFormFormStateInterface;
13use DrupalComponentUtilityUrlHelper;
14use DrupalCoreAjaxAjaxResponse;
15use DrupalCoreAjaxChangedCommand;
16use DrupalCoreAjaxCssCommand;
17use DrupalCoreAjaxHtmlCommand;
18use DrupalCoreAjaxInvokeCommand;
19
20/**
21 * Contribute form.
22 */
23
24class ContributeForm extends FormBase implements FormInterface{
25
26
27 protected $accepted_domains = ['gmail.com', 'yahoo.com'];
28 /**
29 * {@inheritdoc}
30 */
31 public function getFormId() {
32 return 'contact_form_contribute_form';
33 }
34
35 /**
36 * {@inheritdoc}
37 */
38
39 public function buildForm(array $form, FormStateInterface $form_state) {
40 /* $form[ '# attributs'] [ 'class'] [] = 'well white-box';*/
41 $form['intro'] = array(
42 '#markup' => t('Use this form to send a message to an e-mail address. No spamming!'),
43 );
44 $form['Full Name'] = array(
45 '#type' => 'textfield',
46 '#title' => '<label class="control-label" for="input01">'.t('Full Name').'</label>',
47 '#required' => TRUE,
48 /* '#attributes' => array('class' => 'input-xlarge text-tip'),*/
49 );
50 $form['email'] = array(
51 '#type' => 'email',
52 '#title' => $this->t('Email address'),
53 '#required' => true,
54 /* '#prefix' => '<div class="control-label">',
55 '#suffix' => '</div>' */
56
57 );
58
59 $form['Type of feedback'] = array(
60 '#type' => 'radios',
61 '#title' => $this->t('Feedback'),
62 '#description' => t('Type of feedback'),
63 '#options' => array(
64 t('Comments or suggestions'),
65 t('Questions'),
66 t('Report a problem(s)'),
67 t('Other'),
68 /* '#prefix' => '<div class="form-horizontal well">',
69 '#suffix' => '</div>'*/
70 )
71 );
72 $form['Subject'] = array(
73 '#type' => 'textfield',
74 '#title' => $this->t('Subject'),
75 '#required' => true,
76 /* '#prefix' => '<div class="form-horizontal well">',
77 '#suffix' => '</div>'*/
78 );
79 $form['Your Message'] = array(
80 '#type' => 'textarea',
81 '#title' => $this->t('Your Message'),
82 '#required' => true,
83 /*'#prefix' => '<div class="form-horizontal well">',
84 '#suffix' => '</div>'*/
85 );
86
87 $form['my_captcha_element'] = array(
88 '#type' => 'captcha',
89 '#captcha_type' => 'recaptcha/reCAPTCHA',
90 '#required' => true,
91 /* '#prefix' => '<div class="control-group">',
92 '#suffix' => '</div>'*/
93 );
94 /* $form['actions']['#type'] = 'actions';*/
95 $form['actions']['sendt'] = array(
96 '#type' => 'submit',
97 '#value' => $this->t('Send'),
98 );
99 $form['actions']['cancel'] = array(
100 '#name' => 'op',
101 '#type' => 'submit',
102 '#value' => t('Cancel'),
103
104 );
105 return $form;
106 }
107 /**
108 * {@inheritdoc}
109 */
110
111 public function validateForm(array &$form, FormStateInterface $form_state) {
112 if (!filter_var($form_state->getValue('email'), FILTER_VALIDATE_EMAIL)) {
113 $form_state->setError($form['email'], 'Email address is invalid.');
114 }
115
116 if (!$this->validEmailAddress($form_state->getValue('email'))) {
117 $form_state->setError($form['email'], 'Sorry, we only accept Gmail or Yahoo email addresses at this time.');
118 }
119 }
120
121 /**
122 * {@inheritdoc}
123 */
124 public function submitForm(array &$form, FormStateInterface $form_state) {
125 foreach ($form_state->getValues() as $key => $value) {
126 drupal_set_message($key . ': ' . $value);
127 }
128 }
129
130 protected function validEmailAddress($email) {
131 $domain = explode('@', $email)[1];
132 return in_array($domain, $this->accepted_domains);
133
134 }
135
136}