You are here

public function ShippingRateWidget::flagErrors in Commerce Shipping 8.2

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 WidgetBase::flagErrors

File

src/Plugin/Field/FieldWidget/ShippingRateWidget.php, line 199

Class

ShippingRateWidget
Plugin implementation of 'commerce_shipping_rate'.

Namespace

Drupal\commerce_shipping\Plugin\Field\FieldWidget

Code

public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
  foreach ($violations as $offset => $violation) {

    /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
    if ($violation
      ->getCode() == NotNullConstraint::IS_NULL_ERROR) {

      // There are no setters on ConstraintValidation.
      $new = new ConstraintViolation($this
        ->t('A valid shipping method must be selected in order to check out.'), $violation
        ->getMessageTemplate(), $violation
        ->getParameters(), $violation
        ->getRoot(), $violation
        ->getPropertyPath(), $violation
        ->getInvalidValue(), $violation
        ->getPlural(), $violation
        ->getCode(), new NotNullConstraint());
      $violations
        ->remove($offset);
      $violations
        ->add($new);
    }
  }
  return parent::flagErrors($items, $violations, $form, $form_state);
}