function public function Element::setError

8.x-3.x Element.php public Element::setError($message = '', FormStateInterface $form_state = NULL)

Flags an element as having an error.

Parameters

string $message: (optional) The error message to present to the user.

\Drupal\Core\Form\FormStateInterface $form_state: Optional. The current state of the form. If not provided, it will attempt to use the form state passed when constructing the element.

Return value

static

Throws

\BadMethodCallException When the element instance was not constructed with a valid form state object.

Class

Element
Provides helper methods for Drupal render elements.

Namespace

Drupal\bootstrap\Utility

Source src/Utility/Element.php (line 672)

public function setError($message = '', FormStateInterface $form_state = NULL) {
  if (!isset($form_state)) {
    if (!$this->formState) {
      throw new \BadMethodCallException('The element instance must be constructed with a valid form state object to use this method.');
    }
    $form_state = $this->formState;
  }

  // Form errors cannot be set after validation has already completed.
  if (!$form_state->isValidationComplete() && isset($this->array['#parents'])) {
    $form_state->setError($this->array, $message);
  }
  else {
    Bootstrap::message($message, 'error');
  }
  return $this;
}