Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / modules / ajax_forms_test / src / Form / AjaxFormsTestValidationForm.php
1 <?php
2
3 namespace Drupal\ajax_forms_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Form builder: Builds a form that triggers a simple AJAX callback.
10  *
11  * @internal
12  */
13 class AjaxFormsTestValidationForm extends FormBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getFormId() {
19     return 'ajax_forms_test_validation_form';
20   }
21
22   /**
23    * {@inheritdoc}
24    */
25   public function buildForm(array $form, FormStateInterface $form_state) {
26     $form['drivertext'] = [
27       '#title' => $this->t('AJAX-enabled textfield.'),
28       '#description' => $this->t("When this one AJAX-triggers and the spare required field is empty, you should not get an error."),
29       '#type' => 'textfield',
30       '#default_value' => $form_state->getValue('drivertext', ''),
31       '#ajax' => [
32         'callback' => 'ajax_forms_test_validation_form_callback',
33         'wrapper' => 'message_area',
34         'method' => 'replace',
35       ],
36       '#suffix' => '<div id="message_area"></div>',
37     ];
38
39     $form['drivernumber'] = [
40       '#title' => $this->t('AJAX-enabled number field.'),
41       '#description' => $this->t("When this one AJAX-triggers and the spare required field is empty, you should not get an error."),
42       '#type' => 'number',
43       '#default_value' => $form_state->getValue('drivernumber', ''),
44       '#ajax' => [
45         'callback' => 'ajax_forms_test_validation_number_form_callback',
46         'wrapper' => 'message_area_number',
47         'method' => 'replace',
48       ],
49       '#suffix' => '<div id="message_area_number"></div>',
50     ];
51
52     $form['spare_required_field'] = [
53       '#title' => $this->t("Spare Required Field"),
54       '#type' => 'textfield',
55       '#required' => TRUE,
56     ];
57
58     $form['submit'] = [
59       '#type' => 'submit',
60       '#value' => $this->t('Submit'),
61     ];
62
63     return $form;
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function submitForm(array &$form, FormStateInterface $form_state) {
70     $this->messenger()->addStatus($this->t("Validation form submitted"));
71   }
72
73 }