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.
14 class AjaxFormsTestSimpleForm extends FormBase {
19 public function getFormId() {
20 return 'ajax_forms_test_simple_form';
26 public function buildForm(array $form, FormStateInterface $form_state) {
27 $object = new Callbacks();
31 '#title' => $this->t('Color'),
39 'callback' => [$object, 'selectCallback'],
41 '#suffix' => '<div id="ajax_selected_color">No color yet selected</div>',
45 '#type' => 'checkbox',
46 '#title' => $this->t('Test checkbox'),
48 'callback' => [$object, 'checkboxCallback'],
50 '#suffix' => '<div id="ajax_checkbox_value">No action yet</div>',
54 '#value' => $this->t('submit'),
57 // This is for testing invalid callbacks that should return a 500 error in
58 // \Drupal\Core\Form\FormAjaxResponseBuilderInterface::buildResponse().
59 $invalid_callbacks = [
62 'nonexistent' => 'some_function_that_does_not_exist',
64 foreach ($invalid_callbacks as $key => $value) {
65 $form['select_' . $key . '_callback'] = [
67 '#title' => $this->t('Test %key callbacks', ['%key' => $key]),
68 '#options' => ['red' => 'red', 'green' => 'green'],
69 '#ajax' => ['callback' => $value],
73 $form['test_group'] = [
75 '#title' => $this->t('Test group'),
79 // Test ajax element in a #group.
80 $form['checkbox_in_group_wrapper'] = [
81 '#type' => 'container',
82 '#attributes' => ['id' => 'checkbox-wrapper'],
83 '#group' => 'test_group',
84 'checkbox_in_group' => [
85 '#type' => 'checkbox',
86 '#title' => $this->t('AJAX checkbox in a group'),
88 'callback' => [$object, 'checkboxGroupCallback'],
89 'wrapper' => 'checkbox-wrapper',
94 '#title' => $this->t('Nested group'),
97 'checkbox_in_nested' => [
98 '#type' => 'checkbox',
99 '#group' => 'nested_group',
100 '#title' => $this->t('AJAX checkbox in a nested group'),
102 'callback' => [$object, 'checkboxGroupCallback'],
103 'wrapper' => 'checkbox-wrapper',
108 $form['another_checkbox_in_nested'] = [
109 '#type' => 'checkbox',
110 '#group' => 'nested_group',
111 '#title' => $this->t('Another AJAX checkbox in a nested group'),
120 public function submitForm(array &$form, FormStateInterface $form_state) {