3 namespace Drupal\KernelTests\Core\Form;
5 use Drupal\Core\Form\FormInterface;
6 use Drupal\Core\Form\FormState;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\KernelTests\KernelTestBase;
11 * Tests detection of triggering_element for programmed form submissions.
15 class TriggeringElementProgrammedTest extends KernelTestBase implements FormInterface {
20 public function getFormId() {
21 return 'triggering_element_programmed_form';
27 public function buildForm(array $form, FormStateInterface $form_state) {
29 '#type' => 'textfield',
34 '#type' => 'textfield',
38 $form['actions'] = ['#type' => 'actions'];
39 $user_input = $form_state->getUserInput();
40 $form['actions']['submit'] = [
43 '#limit_validation_errors' => [
44 [$user_input['section']],
46 // Required for #limit_validation_errors.
47 '#submit' => [[$this, 'submitForm']],
55 public function validateForm(array &$form, FormStateInterface $form_state) {
56 // Verify that the only submit button was recognized as triggering_element.
57 $this->assertEqual($form['actions']['submit']['#array_parents'], $form_state->getTriggeringElement()['#array_parents']);
63 public function submitForm(array &$form, FormStateInterface $form_state) {
67 * Tests that #limit_validation_errors of the only submit button takes effect.
69 public function testLimitValidationErrors() {
70 // Programmatically submit the form.
71 $form_state = new FormState();
72 $form_state->setValue('section', 'one');
73 $form_builder = $this->container->get('form_builder');
74 $form_builder->submitForm($this, $form_state);
76 // Verify that only the specified section was validated.
77 $errors = $form_state->getErrors();
78 $this->assertTrue(isset($errors['one']), "Section 'one' was validated.");
79 $this->assertFalse(isset($errors['two']), "Section 'two' was not validated.");
81 // Verify that there are only values for the specified section.
82 $this->assertTrue($form_state->hasValue('one'), "Values for section 'one' found.");
83 $this->assertFalse($form_state->hasValue('two'), "Values for section 'two' not found.");