You are here

public function EntityFormDisplay::validateFormValues in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php \Drupal\Core\Entity\Entity\EntityFormDisplay::validateFormValues()
  2. 9 core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php \Drupal\Core\Entity\Entity\EntityFormDisplay::validateFormValues()

Validates submitted widget values and sets the corresponding form errors.

This method invokes entity validation and takes care of flagging them on the form. This is particularly useful when all elements on the form are managed by the form display.

As an alternative, entity validation can be invoked separately such that some violations can be flagged manually. In that case \Drupal\Core\Entity\Display\EntityFormDisplayInterface::flagViolations() must be used for flagging violations related to the form display.

Note that there are two levels of validation for fields in forms: widget validation and field validation:

  • Widget validation steps are specific to a given widget's own form structure and UI metaphors. They are executed during normal form validation, usually through Form API's #element_validate property. Errors reported at this level are typically those that prevent the extraction of proper field values from the submitted form input.
  • If no form / widget errors were reported for the field, field validation steps are performed according to the "constraints" specified by the field definition as part of the entity validation. That validation is independent of the specific widget being used in a given form, and is also performed on REST entity submissions.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity.

array $form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

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

Overrides EntityFormDisplayInterface::validateFormValues

File

core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php, line 245

Class

EntityFormDisplay
Configuration entity that contains widget options for all components of an entity form in a given form mode.

Namespace

Drupal\Core\Entity\Entity

Code

public function validateFormValues(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state) {
  $violations = $entity
    ->validate();
  $violations
    ->filterByFieldAccess();

  // Flag entity level violations.
  foreach ($violations
    ->getEntityViolations() as $violation) {

    /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
    $form_state
      ->setError($form, $violation
      ->getMessage());
  }
  $this
    ->flagWidgetsErrorsFromViolations($violations, $form, $form_state);
}