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