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'),
37 'callback' => [$object, 'selectCallback'],
39 '#suffix' => '<div id="ajax_selected_color">No color yet selected</div>',
43 '#type' => 'checkbox',
44 '#title' => $this->t('Test checkbox'),
46 'callback' => [$object, 'checkboxCallback'],
48 '#suffix' => '<div id="ajax_checkbox_value">No action yet</div>',
52 '#value' => $this->t('submit'),
55 // This is for testing invalid callbacks that should return a 500 error in
56 // \Drupal\Core\Form\FormAjaxResponseBuilderInterface::buildResponse().
57 $invalid_callbacks = [
60 'nonexistent' => 'some_function_that_does_not_exist',
62 foreach ($invalid_callbacks as $key => $value) {
63 $form['select_' . $key . '_callback'] = [
65 '#title' => $this->t('Test %key callbacks', ['%key' => $key]),
66 '#options' => ['red' => 'red'],
67 '#ajax' => ['callback' => $value],
71 $form['test_group'] = [
73 '#title' => $this->t('Test group'),
77 // Test ajax element in a #group.
78 $form['checkbox_in_group_wrapper'] = [
79 '#type' => 'container',
80 '#attributes' => ['id' => 'checkbox-wrapper'],
81 '#group' => 'test_group',
82 'checkbox_in_group' => [
83 '#type' => 'checkbox',
84 '#title' => $this->t('AJAX checkbox in a group'),
86 'callback' => [$object, 'checkboxGroupCallback'],
87 'wrapper' => 'checkbox-wrapper',
92 '#title' => $this->t('Nested group'),
95 'checkbox_in_nested' => [
96 '#type' => 'checkbox',
97 '#group' => 'nested_group',
98 '#title' => $this->t('AJAX checkbox in a nested group'),
100 'callback' => [$object, 'checkboxGroupCallback'],
101 'wrapper' => 'checkbox-wrapper',
106 $form['another_checkbox_in_nested'] = [
107 '#type' => 'checkbox',
108 '#group' => 'nested_group',
109 '#title' => $this->t('Another AJAX checkbox in a nested group'),
118 public function submitForm(array &$form, FormStateInterface $form_state) {