public WidgetBase::flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state)
Reports field-level validation errors against actual form elements.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values.
\Symfony\Component\Validator\ConstraintViolationListInterface $violations: A list of constraint violations to flag.
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 WidgetBaseInterface::flagErrors
File
- core/lib/Drupal/Core/Field/WidgetBase.php, line 389
Class
- WidgetBase
- Base class for 'Field widget' plugin implementations.
Namespace
Drupal\Core\Field
Code
public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) { $field_name = $this->fieldDefinition->getName(); $field_state = static::getWidgetState($form['#parents'], $field_name, $form_state); if ($violations->count()) { // Locate the correct element in the form. $element = NestedArray::getValue($form_state->getCompleteForm(), $field_state['array_parents']); // Do not report entity-level validation errors if Form API errors have // already been reported for the field. // @todo Field validation should not be run on fields with FAPI errors to // begin with. See https://www.drupal.org/node/2070429. $element_path = implode('][', $element['#parents']); if ($reported_errors = $form_state->getErrors()) { foreach (array_keys($reported_errors) as $error_path) { if (strpos($error_path, $element_path) === 0) { return; } } } // Only set errors if the element is visible. if (Element::isVisibleElement($element)) { $handles_multiple = $this->handlesMultipleValues(); $violations_by_delta = array(); foreach ($violations as $violation) { // Separate violations by delta. $property_path = explode('.', $violation->getPropertyPath()); $delta = array_shift($property_path); $violations_by_delta[$delta][] = $violation; $violation->arrayPropertyPath = $property_path; } /** @var \Symfony\Component\Validator\ConstraintViolationInterface[] $delta_violations */ foreach ($violations_by_delta as $delta => $delta_violations) { // Pass violations to the main element: // - if this is a multiple-value widget, // - or if the violations are at the ItemList level. if ($handles_multiple || !is_numeric($delta)) { $delta_element = $element; } // Otherwise, pass errors by delta to the corresponding sub-element. else { $original_delta = $field_state['original_deltas'][$delta]; $delta_element = $element[$original_delta]; } foreach ($delta_violations as $violation) { // @todo: Pass $violation->arrayPropertyPath as property path. $error_element = $this->errorElement($delta_element, $violation, $form, $form_state); if ($error_element !== FALSE) { $form_state->setError($error_element, $violation->getMessage()); } } } } } }
Please login to continue.