function TriggeringElementProgrammedTest::buildForm

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Form/TriggeringElementProgrammedTest.php \Drupal\KernelTests\Core\Form\TriggeringElementProgrammedTest::buildForm()
  2. 10 core/tests/Drupal/KernelTests/Core/Form/TriggeringElementProgrammedTest.php \Drupal\KernelTests\Core\Form\TriggeringElementProgrammedTest::buildForm()
  3. 11.x 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 27

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.