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.
14 class FormTestCheckboxesZeroForm extends FormBase {
19 public function getFormId() {
20 return 'form_test_checkboxes_zero';
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'],
33 $form['checkbox_zero_default'] = [
34 '#title' => t('Zero default'),
35 '#type' => 'checkboxes',
36 '#options' => ['foo', 'bar', 'baz'],
37 '#default_value' => [0],
39 $form['checkbox_string_zero_default'] = [
40 '#title' => t('Zero default (string)'),
41 '#type' => 'checkboxes',
42 '#options' => ['foo', 'bar', 'baz'],
43 '#default_value' => ['0'],
55 public function submitForm(array &$form, FormStateInterface $form_state) {
56 if ($form_state->get('json')) {
57 $form_state->setResponse(new JsonResponse($form_state->getValues()));
60 $form_state->disableRedirect();