3 namespace Drupal\form_test\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
8 class FormTestVerticalTabsAccessForm extends FormBase {
13 public function getFormId() {
14 return 'form_test_vertical_tabs_access_form';
20 public function buildForm(array $form, FormStateInterface $form_state) {
21 $form['vertical_tabs1'] = [
22 '#type' => 'vertical_tabs',
25 '#type' => 'fieldset',
26 '#title' => t('Tab 1'),
27 '#collapsible' => TRUE,
28 '#group' => 'vertical_tabs1',
30 $form['tab1']['field1'] = [
31 '#title' => t('Field 1'),
32 '#type' => 'checkbox',
33 '#default_value' => TRUE,
36 '#type' => 'fieldset',
37 '#title' => t('Tab 2'),
38 '#collapsible' => TRUE,
39 '#group' => 'vertical_tabs1',
41 $form['tab2']['field2'] = [
42 '#title' => t('Field 2'),
43 '#type' => 'textfield',
44 '#default_value' => 'field2',
47 $form['fieldset1'] = [
48 '#type' => 'fieldset',
49 '#title' => t('Fieldset'),
51 $form['fieldset1']['field3'] = [
52 '#type' => 'checkbox',
53 '#title' => t('Field 3'),
54 '#default_value' => TRUE,
57 $form['container'] = [
58 '#type' => 'container',
60 $form['container']['field4'] = [
61 '#type' => 'checkbox',
62 '#title' => t('Field 4'),
63 '#default_value' => TRUE,
65 $form['container']['subcontainer'] = [
66 '#type' => 'container',
68 $form['container']['subcontainer']['field5'] = [
69 '#type' => 'checkbox',
70 '#title' => t('Field 5'),
71 '#default_value' => TRUE,
74 $form['vertical_tabs2'] = [
75 '#type' => 'vertical_tabs',
78 '#type' => 'fieldset',
79 '#title' => t('Tab 3'),
80 '#collapsible' => TRUE,
81 '#group' => 'vertical_tabs2',
83 $form['tab3']['field6'] = [
84 '#title' => t('Field 6'),
85 '#type' => 'checkbox',
86 '#default_value' => TRUE,
92 $form['actions']['submit'] = [
94 '#value' => t('Submit'),
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.'));
107 if ($values['field2'] != 'field2') {
108 $form_state->setErrorByName('tab2][field2', t('This textfield inside a vertical tab does not have its default value.'));
110 if (empty($values['field3'])) {
111 $form_state->setErrorByName('fieldset][field3', t('This checkbox inside a fieldset does not have its default value.'));
113 if (empty($values['field4'])) {
114 $form_state->setErrorByName('container][field4', t('This checkbox inside a container does not have its default value.'));
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.'));
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.'));
127 public function submitForm(array &$form, FormStateInterface $form_state) {
128 drupal_set_message(t('The form submitted correctly.'));