You are here

public function ContentEntityForm::validateForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/ContentEntityForm.php \Drupal\Core\Entity\ContentEntityForm::validateForm()

Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level validation handler, otherwise an exception will be thrown.

Overrides FormBase::validateForm

2 calls to ContentEntityForm::validateForm()
MessageForm::validateForm in core/modules/contact/src/MessageForm.php
Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level…
TermForm::validateForm in core/modules/taxonomy/src/TermForm.php
Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level…
3 methods override ContentEntityForm::validateForm()
ContentEntityConfirmFormBase::validateForm in core/lib/Drupal/Core/Entity/ContentEntityConfirmFormBase.php
Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level…
MessageForm::validateForm in core/modules/contact/src/MessageForm.php
Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level…
TermForm::validateForm in core/modules/taxonomy/src/TermForm.php
Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level…

File

core/lib/Drupal/Core/Entity/ContentEntityForm.php, line 183

Class

ContentEntityForm
Entity form variant for content entity types.

Namespace

Drupal\Core\Entity

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $this
    ->buildEntity($form, $form_state);
  $violations = $entity
    ->validate();

  // Remove violations of inaccessible fields.
  $violations
    ->filterByFieldAccess($this
    ->currentUser());

  // In case a field-level submit button is clicked, for example the 'Add
  // another item' button for multi-value fields or the 'Upload' button for a
  // File or an Image field, make sure that we only keep violations for that
  // specific field.
  $edited_fields = [];
  if ($limit_validation_errors = $form_state
    ->getLimitValidationErrors()) {
    foreach ($limit_validation_errors as $section) {
      $field_name = reset($section);
      if ($entity
        ->hasField($field_name)) {
        $edited_fields[] = $field_name;
      }
    }
    $edited_fields = array_unique($edited_fields);
  }
  else {
    $edited_fields = $this
      ->getEditedFieldNames($form_state);
  }

  // Remove violations for fields that are not edited.
  $violations
    ->filterByFields(array_diff(array_keys($entity
    ->getFieldDefinitions()), $edited_fields));
  $this
    ->flagViolations($violations, $form, $form_state);

  // The entity was validated.
  $entity
    ->setValidationRequired(FALSE);
  $form_state
    ->setTemporaryValue('entity_validated', TRUE);
  return $entity;
}