flagErrors example

public function defaultValuesFormValidate(array $element, array &$form, FormStateInterface $form_state) {
    // Extract the submitted value, and validate it.     if ($widget = $this->defaultValueWidget($form_state)) {
      $widget->extractFormValues($this$element$form_state);
      // Force a non-required field definition.       // @see self::defaultValueWidget().       $this->getFieldDefinition()->setRequired(FALSE);
      $violations = $this->validate();

      // Assign reported errors to the correct form element.       if (count($violations)) {
        $widget->flagErrors($this$violations$element$form_state);
      }
    }
  }

  /** * {@inheritdoc} */
  public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state) {
    // Extract the submitted value, and return it as an array.     if ($widget = $this->defaultValueWidget($form_state)) {
      $widget->extractFormValues($this$element$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)) {
        $field_violations = $this->movePropertyPathViolationsRelativeToField($field_name$violations->getByField($field_name));
        $widget->flagErrors($entity->get($field_name)$field_violations$form$form_state);
      }
    }
  }

  /** * Moves the property path to be relative to field level. * * @param string $field_name * The field name. * @param \Symfony\Component\Validator\ConstraintViolationListInterface $violations * The violations. * * @return \Symfony\Component\Validator\ConstraintViolationList * A new constraint violation list with the changed property path. */
$this->t($violation->getMessageTemplate()$parameters),
          $violation->getMessageTemplate(),
          $parameters,
          $violation->getRoot(),
          $violation->getPropertyPath(),
          $violation->getInvalidValue(),
          $violation->getPlural(),
          $violation->getCode()
        ));
      }
    }
    parent::flagErrors($items$violations$form$form_state);
  }

}
$field_state['items'] = $submitted_values;
    static::setWidgetState($parents$field_name$form_state$field_state);
  }

  /** * {@inheritdoc} */
  public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
    // Never flag validation errors for the remove button.     $clicked_button = end($form_state->getTriggeringElement()['#parents']);
    if ($clicked_button !== 'remove_button') {
      parent::flagErrors($items$violations$form$form_state);
    }
  }

}
Home | Imprint | This part of the site doesn't use cookies.