3 namespace Drupal\form_test\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Symfony\Component\HttpFoundation\JsonResponse;
10 * Tests checkboxes zero.
12 class FormTestCheckboxesZeroForm extends FormBase {
17 public function getFormId() {
18 return 'form_test_checkboxes_zero';
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'],
31 $form['checkbox_zero_default'] = [
32 '#title' => t('Zero default'),
33 '#type' => 'checkboxes',
34 '#options' => ['foo', 'bar', 'baz'],
35 '#default_value' => [0],
37 $form['checkbox_string_zero_default'] = [
38 '#title' => t('Zero default (string)'),
39 '#type' => 'checkboxes',
40 '#options' => ['foo', 'bar', 'baz'],
41 '#default_value' => ['0'],
53 public function submitForm(array &$form, FormStateInterface $form_state) {
54 if ($form_state->get('json')) {
55 $form_state->setResponse(new JsonResponse($form_state->getValues()));
58 $form_state->disableRedirect();