You are here

protected function CommentForm::flagViolations in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/comment/src/CommentForm.php \Drupal\comment\CommentForm::flagViolations()

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.

Overrides ContentEntityForm::flagViolations

File

core/modules/comment/src/CommentForm.php, line 343

Class

CommentForm
Base handler for comment forms.

Namespace

Drupal\comment

Code

protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {

  // Manually flag violations of fields not handled by the form display.
  foreach ($violations
    ->getByField('created') as $violation) {
    $form_state
      ->setErrorByName('date', $violation
      ->getMessage());
  }
  foreach ($violations
    ->getByField('name') as $violation) {
    $form_state
      ->setErrorByName('name', $violation
      ->getMessage());
  }
  parent::flagViolations($violations, $form, $form_state);
}