efb1d4d42110e61293b74b5708508849f19282c8
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestRadiosCheckedForm.php
1 <?php
2
3 namespace Drupal\form_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Symfony\Component\HttpFoundation\JsonResponse;
8
9 /**
10  * Form constructor to test #default_value settings of radios.
11  */
12 class FormTestRadiosCheckedForm extends FormBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public function getFormId() {
18     return 'form_test_radios_checked';
19   }
20
21   /**
22    * {@inheritdoc}
23    */
24   public function buildForm(array $form, FormStateInterface $form_state) {
25     $form['radios'] = [
26       '#type' => 'radios',
27       '#title' => 'Radios',
28       '#options' => [
29         0 => 'Zero',
30         'foo' => 'Foo',
31         1 => 'One',
32         'bar' => '<em>Bar - radios</em>',
33         '>' => "<em>Special Char</em><script>alert('radios');</script>",
34       ],
35       '#default_value' => 0,
36     ];
37     $form['radios-string'] = [
38       '#type' => 'radios',
39       '#title' => 'Radios',
40       '#options' => [
41         0 => 'Zero',
42         'foo' => 'Foo',
43         1 => 'One',
44         'bar' => '<em>Bar - radios</em>',
45         '>' => "<em>Special Char</em><script>alert('radios');</script>",
46       ],
47       '#default_value' => 'bar',
48     ];
49     $form['radios-boolean-true'] = [
50       '#type' => 'radios',
51       '#title' => 'Radios',
52       '#options' => [
53         'All' => '- Any -',
54         1 => 'True',
55         0 => 'False',
56       ],
57       '#default_value' => TRUE,
58     ];
59     $form['radios-boolean-false'] = [
60       '#type' => 'radios',
61       '#title' => 'Radios',
62       '#options' => [
63         'All' => '- Any -',
64         1 => 'True',
65         0 => 'False',
66       ],
67       '#default_value' => FALSE,
68     ];
69     $form['radios-boolean-any'] = [
70       '#type' => 'radios',
71       '#title' => 'Radios',
72       '#options' => [
73         'All' => '- Any -',
74         1 => 'True',
75         0 => 'False',
76       ],
77       '#default_value' => 'All',
78     ];
79     $form['radios-string-zero'] = [
80       '#type' => 'radios',
81       '#title' => 'Radios',
82       '#options' => [
83         'All' => '- Any -',
84         '0' => 'Zero',
85         100 => 'One hundred',
86       ],
87       '#default_value' => 0,
88     ];
89     $form['radios-int-non-zero'] = [
90       '#type' => 'radios',
91       '#title' => 'Radios',
92       '#options' => [
93         'All' => '- Any -',
94         0 => 'Zero',
95         10 => 'Ten',
96         100 => 'One hundred',
97       ],
98       '#default_value' => 10,
99     ];
100     $form['radios-int-non-zero-as-string'] = [
101       '#type' => 'radios',
102       '#title' => 'Radios',
103       '#options' => [
104         'All' => '- Any -',
105         '0' => 'Zero',
106         '10' => 'Ten',
107         '100' => 'One hundred',
108       ],
109       '#default_value' => '100',
110     ];
111     $form['radios-empty-string'] = [
112       '#type' => 'radios',
113       '#title' => 'Radios',
114       '#options' => [
115         'All' => '- Any -',
116         0 => 'None',
117       ],
118       '#default_value' => '',
119     ];
120     $form['radios-empty-array'] = [
121       '#type' => 'radios',
122       '#title' => 'Radios',
123       '#options' => [
124         'All' => '- Any -',
125         0 => 'None',
126       ],
127       '#default_value' => [],
128     ];
129     $form['radios-key-FALSE'] = [
130       '#type' => 'radios',
131       '#title' => 'Radios',
132       '#options' => [
133         'All' => '- Any -',
134         FALSE => 'False',
135       ],
136       '#default_value' => '',
137     ];
138
139     $form['submit'] = ['#type' => 'submit', '#value' => 'Submit'];
140
141     return $form;
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function submitForm(array &$form, FormStateInterface $form_state) {
148     $form_state->setResponse(new JsonResponse($form_state->getValues()));
149   }
150
151 }