function SubmitDriven::buildForm

Same name in other branches
  1. 3.x modules/ajax_example/src/Form/SubmitDriven.php \Drupal\ajax_example\Form\SubmitDriven::buildForm()

Overrides FormInterface::buildForm

File

modules/ajax_example/src/Form/SubmitDriven.php, line 23

Class

SubmitDriven
Submit a form without a page reload.

Namespace

Drupal\ajax_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    // This container wil be replaced by AJAX.
    $form['container'] = [
        '#type' => 'container',
        '#attributes' => [
            'id' => 'box-container',
        ],
    ];
    // The box contains some markup that we can change on a submit request.
    $form['container']['box'] = [
        '#type' => 'markup',
        '#markup' => '<h1>Initial markup for box</h1>',
    ];
    $form['submit'] = [
        '#type' => 'submit',
        // The AJAX handler will call our callback, and will replace whatever page
        // element has id box-container.
'#ajax' => [
            'callback' => '::promptCallback',
            'wrapper' => 'box-container',
        ],
        '#value' => $this->t('Submit'),
    ];
    return $form;
}