summaryrefslogtreecommitdiffstats
path: root/core/modules/system/tests/modules/form_test/src/Form/FormTestVerticalTabsAccessForm.php
blob: c2d062a64f704551d48b826680d7de4af6e830dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

namespace Drupal\form_test\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Builds a form to test vertical tabs access.
 *
 * @internal
 */
class FormTestVerticalTabsAccessForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'form_test_vertical_tabs_access_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['vertical_tabs1'] = [
      '#type' => 'vertical_tabs',
    ];
    $form['tab1'] = [
      '#type' => 'fieldset',
      '#title' => t('Tab 1'),
      '#collapsible' => TRUE,
      '#group' => 'vertical_tabs1',
    ];
    $form['tab1']['field1'] = [
      '#title' => t('Field 1'),
      '#type' => 'checkbox',
      '#default_value' => TRUE,
    ];
    $form['tab2'] = [
      '#type' => 'fieldset',
      '#title' => t('Tab 2'),
      '#collapsible' => TRUE,
      '#group' => 'vertical_tabs1',
    ];
    $form['tab2']['field2'] = [
      '#title' => t('Field 2'),
      '#type' => 'textfield',
      '#default_value' => 'field2',
    ];

    $form['fieldset1'] = [
      '#type' => 'fieldset',
      '#title' => t('Fieldset'),
    ];
    $form['fieldset1']['field3'] = [
      '#type' => 'checkbox',
      '#title' => t('Field 3'),
      '#default_value' => TRUE,
    ];

    $form['container'] = [
      '#type' => 'container',
    ];
    $form['container']['field4'] = [
      '#type' => 'checkbox',
      '#title' => t('Field 4'),
      '#default_value' => TRUE,
    ];
    $form['container']['subcontainer'] = [
      '#type' => 'container',
    ];
    $form['container']['subcontainer']['field5'] = [
      '#type' => 'checkbox',
      '#title' => t('Field 5'),
      '#default_value' => TRUE,
    ];

    $form['vertical_tabs2'] = [
      '#type' => 'vertical_tabs',
    ];
    $form['tab3'] = [
      '#type' => 'fieldset',
      '#title' => t('Tab 3'),
      '#collapsible' => TRUE,
      '#group' => 'vertical_tabs2',
    ];
    $form['tab3']['field6'] = [
      '#title' => t('Field 6'),
      '#type' => 'checkbox',
      '#default_value' => TRUE,
    ];

    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('Submit'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValues();
    if (empty($values['field1'])) {
      $form_state->setErrorByName('tab1][field1', t('This checkbox inside a vertical tab does not have its default value.'));
    }
    if ($values['field2'] != 'field2') {
      $form_state->setErrorByName('tab2][field2', t('This textfield inside a vertical tab does not have its default value.'));
    }
    if (empty($values['field3'])) {
      $form_state->setErrorByName('fieldset][field3', t('This checkbox inside a fieldset does not have its default value.'));
    }
    if (empty($values['field4'])) {
      $form_state->setErrorByName('container][field4', t('This checkbox inside a container does not have its default value.'));
    }
    if (empty($values['field5'])) {
      $form_state->setErrorByName('container][subcontainer][field5', t('This checkbox inside a nested container does not have its default value.'));
    }
    if (empty($values['field5'])) {
      $form_state->setErrorByName('tab3][field6', t('This checkbox inside a vertical tab whose fieldset access is allowed does not have its default value.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->messenger()->addStatus(t('The form submitted correctly.'));
  }

}