function ContentEntityForm::flagViolations

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/ContentEntityForm.php \Drupal\Core\Entity\ContentEntityForm::flagViolations()
  2. 8.9.x core/lib/Drupal/Core/Entity/ContentEntityForm.php \Drupal\Core\Entity\ContentEntityForm::flagViolations()
  3. 11.x core/lib/Drupal/Core/Entity/ContentEntityForm.php \Drupal\Core\Entity\ContentEntityForm::flagViolations()

Flags violations for the current form.

If a custom entity form adds some fields to the form (i.e. without using the form display), it needs to add its fields to array returned by getEditedFieldNames() and overwrite this method in order to show any violations for those fields; e.g.:

foreach ($violations->getByField('name') as $violation) {
    $form_state->setErrorByName('name', $violation->getMessage());
}
parent::flagViolations($violations, $form, $form_state);

Parameters

\Drupal\Core\Entity\EntityConstraintViolationListInterface $violations: The violations to flag.

array $form: A nested array of form elements comprising the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

5 calls to ContentEntityForm::flagViolations()
AccountForm::flagViolations in core/modules/user/src/AccountForm.php
CommentForm::flagViolations in core/modules/comment/src/CommentForm.php
ContentEntityForm::validateForm in core/lib/Drupal/Core/Entity/ContentEntityForm.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::flagViolations in core/modules/taxonomy/src/TermForm.php
WorkspaceForm::flagViolations in core/modules/workspaces/src/Form/WorkspaceForm.php
4 methods override ContentEntityForm::flagViolations()
AccountForm::flagViolations in core/modules/user/src/AccountForm.php
CommentForm::flagViolations in core/modules/comment/src/CommentForm.php
TermForm::flagViolations in core/modules/taxonomy/src/TermForm.php
WorkspaceForm::flagViolations in core/modules/workspaces/src/Form/WorkspaceForm.php

File

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

Class

ContentEntityForm
Entity form variant for content entity types.

Namespace

Drupal\Core\Entity

Code

protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
    // Flag entity level violations.
    foreach ($violations->getEntityViolations() as $violation) {
        
        /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
        $form_state->setErrorByName(str_replace('.', '][', $violation->getPropertyPath()), $violation->getMessage());
    }
    // Let the form display flag violations of its fields.
    $this->getFormDisplay($form_state)
        ->flagWidgetsErrorsFromViolations($violations, $form, $form_state);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.