6d9f1b7fc53a457b98c842c834747c1b1d6a6198
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestCheckboxesZeroForm.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  * Tests checkboxes zero.
11  *
12  * @internal
13  */
14 class FormTestCheckboxesZeroForm extends FormBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function getFormId() {
20     return 'form_test_checkboxes_zero';
21   }
22
23   /**
24    * {@inheritdoc}
25    */
26   public function buildForm(array $form, FormStateInterface $form_state, $json = TRUE) {
27     $form_state->set('json', $json);
28     $form['checkbox_off'] = [
29       '#title' => t('Checkbox off'),
30       '#type' => 'checkboxes',
31       '#options' => ['foo', 'bar', 'baz'],
32     ];
33     $form['checkbox_zero_default'] = [
34       '#title' => t('Zero default'),
35       '#type' => 'checkboxes',
36       '#options' => ['foo', 'bar', 'baz'],
37       '#default_value' => [0],
38     ];
39     $form['checkbox_string_zero_default'] = [
40       '#title' => t('Zero default (string)'),
41       '#type' => 'checkboxes',
42       '#options' => ['foo', 'bar', 'baz'],
43       '#default_value' => ['0'],
44     ];
45     $form['submit'] = [
46       '#type' => 'submit',
47       '#value' => 'Save',
48     ];
49     return $form;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function submitForm(array &$form, FormStateInterface $form_state) {
56     if ($form_state->get('json')) {
57       $form_state->setResponse(new JsonResponse($form_state->getValues()));
58     }
59     else {
60       $form_state->disableRedirect();
61     }
62   }
63
64 }