3 namespace Drupal\ajax_forms_test\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\ajax_forms_test\Callbacks;
7 use Drupal\Core\Form\FormStateInterface;
10 * Form builder: Builds a form that triggers a simple AJAX callback.
12 class AjaxFormsTestSimpleForm extends FormBase {
17 public function getFormId() {
18 return 'ajax_forms_test_simple_form';
24 public function buildForm(array $form, FormStateInterface $form_state) {
25 $object = new Callbacks();
29 '#title' => $this->t('Color'),
36 'callback' => [$object, 'selectCallback'],
38 '#suffix' => '<div id="ajax_selected_color">No color yet selected</div>',
42 '#type' => 'checkbox',
43 '#title' => $this->t('Test checkbox'),
45 'callback' => [$object, 'checkboxCallback'],
47 '#suffix' => '<div id="ajax_checkbox_value">No action yet</div>',
51 '#value' => $this->t('submit'),
54 // This is for testing invalid callbacks that should return a 500 error in
55 // \Drupal\Core\Form\FormAjaxResponseBuilderInterface::buildResponse().
56 $invalid_callbacks = [
59 'nonexistent' => 'some_function_that_does_not_exist',
61 foreach ($invalid_callbacks as $key => $value) {
62 $form['select_' . $key . '_callback'] = [
64 '#title' => $this->t('Test %key callbacks', ['%key' => $key]),
65 '#options' => ['red' => 'red'],
66 '#ajax' => ['callback' => $value],
70 $form['test_group'] = [
72 '#title' => $this->t('Test group'),
76 // Test ajax element in a #group.
77 $form['checkbox_in_group_wrapper'] = [
78 '#type' => 'container',
79 '#attributes' => ['id' => 'checkbox-wrapper'],
80 '#group' => 'test_group',
81 'checkbox_in_group' => [
82 '#type' => 'checkbox',
83 '#title' => $this->t('AJAX checkbox in a group'),
85 'callback' => [$object, 'checkboxGroupCallback'],
86 'wrapper' => 'checkbox-wrapper',
91 '#title' => $this->t('Nested group'),
94 'checkbox_in_nested' => [
95 '#type' => 'checkbox',
96 '#group' => 'nested_group',
97 '#title' => $this->t('AJAX checkbox in a nested group'),
99 'callback' => [$object, 'checkboxGroupCallback'],
100 'wrapper' => 'checkbox-wrapper',
105 $form['another_checkbox_in_nested'] = [
106 '#type' => 'checkbox',
107 '#group' => 'nested_group',
108 '#title' => $this->t('Another AJAX checkbox in a nested group'),
118 public function submitForm(array &$form, FormStateInterface $form_state) {