Version 1
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestVerticalTabsAccessForm.php
1 <?php
2
3 namespace Drupal\form_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 class FormTestVerticalTabsAccessForm extends FormBase {
9
10   /**
11    * {@inheritdoc}
12    */
13   public function getFormId() {
14     return 'form_test_vertical_tabs_access_form';
15   }
16
17   /**
18    * {@inheritdoc}
19    */
20   public function buildForm(array $form, FormStateInterface $form_state) {
21     $form['vertical_tabs1'] = [
22       '#type' => 'vertical_tabs',
23     ];
24     $form['tab1'] = [
25       '#type' => 'fieldset',
26       '#title' => t('Tab 1'),
27       '#collapsible' => TRUE,
28       '#group' => 'vertical_tabs1',
29     ];
30     $form['tab1']['field1'] = [
31       '#title' => t('Field 1'),
32       '#type' => 'checkbox',
33       '#default_value' => TRUE,
34     ];
35     $form['tab2'] = [
36       '#type' => 'fieldset',
37       '#title' => t('Tab 2'),
38       '#collapsible' => TRUE,
39       '#group' => 'vertical_tabs1',
40     ];
41     $form['tab2']['field2'] = [
42       '#title' => t('Field 2'),
43       '#type' => 'textfield',
44       '#default_value' => 'field2',
45     ];
46
47     $form['fieldset1'] = [
48       '#type' => 'fieldset',
49       '#title' => t('Fieldset'),
50     ];
51     $form['fieldset1']['field3'] = [
52       '#type' => 'checkbox',
53       '#title' => t('Field 3'),
54       '#default_value' => TRUE,
55     ];
56
57     $form['container'] = [
58       '#type' => 'container',
59     ];
60     $form['container']['field4'] = [
61       '#type' => 'checkbox',
62       '#title' => t('Field 4'),
63       '#default_value' => TRUE,
64     ];
65     $form['container']['subcontainer'] = [
66       '#type' => 'container',
67     ];
68     $form['container']['subcontainer']['field5'] = [
69       '#type' => 'checkbox',
70       '#title' => t('Field 5'),
71       '#default_value' => TRUE,
72     ];
73
74     $form['vertical_tabs2'] = [
75       '#type' => 'vertical_tabs',
76     ];
77     $form['tab3'] = [
78       '#type' => 'fieldset',
79       '#title' => t('Tab 3'),
80       '#collapsible' => TRUE,
81       '#group' => 'vertical_tabs2',
82     ];
83     $form['tab3']['field6'] = [
84       '#title' => t('Field 6'),
85       '#type' => 'checkbox',
86       '#default_value' => TRUE,
87     ];
88
89     $form['actions'] = [
90       '#type' => 'actions',
91     ];
92     $form['actions']['submit'] = [
93       '#type' => 'submit',
94       '#value' => t('Submit'),
95     ];
96     return $form;
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function validateForm(array &$form, FormStateInterface $form_state) {
103     $values = $form_state->getValues();
104     if (empty($values['field1'])) {
105       $form_state->setErrorByName('tab1][field1', t('This checkbox inside a vertical tab does not have its default value.'));
106     }
107     if ($values['field2'] != 'field2') {
108       $form_state->setErrorByName('tab2][field2', t('This textfield inside a vertical tab does not have its default value.'));
109     }
110     if (empty($values['field3'])) {
111       $form_state->setErrorByName('fieldset][field3', t('This checkbox inside a fieldset does not have its default value.'));
112     }
113     if (empty($values['field4'])) {
114       $form_state->setErrorByName('container][field4', t('This checkbox inside a container does not have its default value.'));
115     }
116     if (empty($values['field5'])) {
117       $form_state->setErrorByName('container][subcontainer][field5', t('This checkbox inside a nested container does not have its default value.'));
118     }
119     if (empty($values['field5'])) {
120       $form_state->setErrorByName('tab3][field6', t('This checkbox inside a vertical tab whose fieldset access is allowed does not have its default value.'));
121     }
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function submitForm(array &$form, FormStateInterface $form_state) {
128     drupal_set_message(t('The form submitted correctly.'));
129   }
130
131 }