function TriggeringElementProgrammedTest::buildForm

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Form/TriggeringElementProgrammedTest.php \Drupal\KernelTests\Core\Form\TriggeringElementProgrammedTest::buildForm()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Form/TriggeringElementProgrammedTest.php \Drupal\KernelTests\Core\Form\TriggeringElementProgrammedTest::buildForm()
  3. 10 core/tests/Drupal/KernelTests/Core/Form/TriggeringElementProgrammedTest.php \Drupal\KernelTests\Core\Form\TriggeringElementProgrammedTest::buildForm()

Overrides FormInterface::buildForm

File

core/tests/Drupal/KernelTests/Core/Form/TriggeringElementProgrammedTest.php, line 29

Class

TriggeringElementProgrammedTest
Tests detection of triggering_element for programmed form submissions.

Namespace

Drupal\KernelTests\Core\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    $form['one'] = [
        '#type' => 'textfield',
        '#title' => 'One',
        '#required' => TRUE,
    ];
    $form['two'] = [
        '#type' => 'textfield',
        '#title' => 'Two',
        '#required' => TRUE,
    ];
    $form['actions'] = [
        '#type' => 'actions',
    ];
    $user_input = $form_state->getUserInput();
    $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => 'Save',
        '#limit_validation_errors' => [
            [
                $user_input['section'],
            ],
        ],
        // Required for #limit_validation_errors.
'#submit' => [
            [
                $this,
                'submitForm',
            ],
        ],
    ];
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.