function AjaxTestForm::buildForm
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- 
              core/
modules/ system/ tests/ modules/ ajax_test/ src/ Form/ AjaxTestForm.php, line 28  
Class
- AjaxTestForm
 - Dummy form for testing DialogRenderer with _form routes.
 
Namespace
Drupal\ajax_test\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
  $form['#action'] = Url::fromRoute('ajax_test.dialog')->toString();
  $form['description'] = [
    '#markup' => '<p>' . $this->t("Ajax Form contents description.") . '</p>',
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this->t('Do it'),
  ];
  $form['actions']['preview'] = [
    '#title' => 'Preview',
    '#type' => 'link',
    '#url' => Url::fromRoute('ajax_test.dialog_form'),
    '#attributes' => [
      'class' => [
        'use-ajax',
        'button',
      ],
      'data-dialog-type' => 'modal',
    ],
  ];
  $form['actions']['hello_world'] = [
    '#type' => 'submit',
    '#value' => $this->t('Hello world'),
    // No regular submit-handler. This form only works via JavaScript.
'#submit' => [],
    '#ajax' => [
      'callback' => '::helloWorld',
      'event' => 'click',
    ],
  ];
  return $form;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.