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