flagWidgetsErrorsFromViolations example


  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);
  }

  /** * Initializes the form state and the entity before the first form build. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */
  protected function init(FormStateInterface $form_state) {
    // Ensure we act on the translation object corresponding to the current form     // language.

  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);
  }

  /** * {@inheritdoc} */
  public function flagWidgetsErrorsFromViolations(EntityConstraintViolationListInterface $violations, array &$form, FormStateInterface $form_state) {
    $entity = $violations->getEntity();
    foreach ($violations->getFieldNames() as $field_name) {
      // Only show violations for fields that actually appear in the form, and       // let the widget assign the violations to the correct form elements.       if ($widget = $this->getRenderer($field_name)) {
        
Home | Imprint | This part of the site doesn't use cookies.