View source
<?php
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
class FormTestCheckboxForm extends FormBase {
public function getFormId() {
return '_test_checkbox_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['required_checkbox'] = array(
'#type' => 'checkbox',
'#required' => TRUE,
'#title' => 'required_checkbox',
);
$form['disabled_checkbox_on'] = array(
'#type' => 'checkbox',
'#disabled' => TRUE,
'#return_value' => 'disabled_checkbox_on',
'#default_value' => 'disabled_checkbox_on',
'#title' => 'disabled_checkbox_on',
);
$form['disabled_checkbox_off'] = array(
'#type' => 'checkbox',
'#disabled' => TRUE,
'#return_value' => 'disabled_checkbox_off',
'#default_value' => NULL,
'#title' => 'disabled_checkbox_off',
);
$form['checkbox_on'] = array(
'#type' => 'checkbox',
'#return_value' => 'checkbox_on',
'#default_value' => 'checkbox_on',
'#title' => 'checkbox_on',
);
$form['checkbox_off'] = array(
'#type' => 'checkbox',
'#return_value' => 'checkbox_off',
'#default_value' => 'checkbox_on',
'#title' => 'checkbox_off',
);
$form['zero_checkbox_on'] = array(
'#type' => 'checkbox',
'#return_value' => '0',
'#default_value' => '0',
'#title' => 'zero_checkbox_on',
);
$form['zero_checkbox_off'] = array(
'#type' => 'checkbox',
'#return_value' => '0',
'#default_value' => '1',
'#title' => 'zero_checkbox_off',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->setResponse(new JsonResponse($form_state
->getValues()));
}
}