protected function ContentEntityForm::flagViolations

protected ContentEntityForm::flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state)

Flags violations for the current form.

If the entity form customly 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.

File

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

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('', $violation->getMessage());
  }
  // Let the form display flag violations of its fields.
  $this->getFormDisplay($form_state)->flagWidgetsErrorsFromViolations($violations, $form, $form_state);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!ContentEntityForm.php/function/ContentEntityForm::flagViolations/8.1.x