Pull merge.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / form / simple.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Provides a {{ name }} form.
10  */
11 class {{ class }} extends FormBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getFormId() {
17     return '{{ form_id }}';
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   public function buildForm(array $form, FormStateInterface $form_state) {
24
25     $form['message'] = [
26       '#type' => 'textarea',
27       '#title' => $this->t('Message'),
28       '#required' => TRUE,
29     ];
30
31     $form['actions'] = [
32       '#type' => 'actions',
33     ];
34     $form['actions']['submit'] = [
35       '#type' => 'submit',
36       '#value' => $this->t('Send'),
37     ];
38
39     return $form;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function validateForm(array &$form, FormStateInterface $form_state) {
46     if (mb_strlen($form_state->getValue('message')) < 10) {
47       $form_state->setErrorByName('name', $this->t('Message should be at least 10 characters.'));
48     }
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function submitForm(array &$form, FormStateInterface $form_state) {
55     $this->messenger()->addStatus($this->t('The message has been sent.'));
56     $form_state->setRedirect('<front>');
57   }
58
59 }