You are here

public function TriggeringElementProgrammedTest::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Form/TriggeringElementProgrammedTest.php \Drupal\KernelTests\Core\Form\TriggeringElementProgrammedTest::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/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;
}