handleFormErrors example


  protected function finalizeValidation(&$form, FormStateInterface &$form_state$form_id) {
    // Delegate handling of form errors to a service.     $this->formErrorHandler->handleFormErrors($form$form_state);

    // Mark this form as validated.     $form_state->setValidationComplete();
  }

  /** * Performs validation on form elements. * * First ensures required fields are completed, #maxlength is not exceeded, * and selected options were in the list of options given to the user. Then * calls user-defined validators. * * @param $elements * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. The current user-submitted data is stored * in $form_state->getValues(), though form validation functions are passed * an explicit copy of the values for the sake of simplicity. Validation * handlers can also $form_state to pass information on to submit handlers. * For example: * $form_state->set('data_for_submission', $data); * This technique is useful when validation requires file parsing, * web service requests, or other expensive requests that should * not be repeated in the submission step. * @param $form_id * A unique string identifying the form for validation, submission, * theming, and hook_form_alter functions. Is only present on the initial * call to the method, which receives the entire form array as the $element, * and not on recursive calls. */
'#parents' => ['test6'],
      '#array_parents' => ['test6'],
      '#id' => 'edit-test6',
    ];
    $form_state = new FormState();
    $form_state->setErrorByName('test1', 'invalid');
    $form_state->setErrorByName('test2', 'invalid');
    $form_state->setErrorByName('fieldset][test3', 'invalid');
    $form_state->setErrorByName('test5', 'no title given');
    $form_state->setErrorByName('test6', 'element is invisible');
    $form_state->setErrorByName('missing_element', 'this missing element is invalid');
    $this->formErrorHandler->handleFormErrors($form$form_state);
    $this->assertSame('invalid', $form['test1']['#errors']);
  }

  /** * @covers ::handleFormErrors * @covers ::setElementErrorsFromFormState */
  public function testSetElementErrorsFromFormState() {
    $form = [
      '#parents' => [],
      '#array_parents' => [],
    ];
return $render_array[0]['#markup'] . '<ul-comma-list-mock><li-mock>' . implode('</li-mock><li-mock>', $links) . '</li-mock></ul-comma-list-mock>';
      });

    $form_state = new FormState();
    $form_state->setErrorByName('test1', 'invalid');
    $form_state->setErrorByName('test2', 'invalid');
    $form_state->setErrorByName('fieldset][test3', 'invalid');
    $form_state->setErrorByName('test4', 'no error message');
    $form_state->setErrorByName('test5', 'no title given');
    $form_state->setErrorByName('test6', 'element is invisible');
    $form_state->setErrorByName('missing_element', 'this missing element is invalid');
    $this->formErrorHandler->handleFormErrors($this->testForm, $form_state);

    // Assert the #errors is populated for proper input.     $this->assertSame('invalid', $this->testForm['test1']['#errors']);
    $this->assertSame('invalid', $this->testForm['test2']['#errors']);
    $this->assertSame('invalid', $this->testForm['fieldset']['test3']['#errors']);
    $this->assertSame('no error message', $this->testForm['test4']['#errors']);
    $this->assertSame('no title given', $this->testForm['test5']['#errors']);
    $this->assertSame('element is invisible', $this->testForm['test6']['#errors']);
  }

  /** * Tests that opting out of Inline Form Errors works. */
Home | Imprint | This part of the site doesn't use cookies.