You are here

public function MobileNumberWidget::errorElement in Mobile Number 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldWidget/MobileNumberWidget.php \Drupal\mobile_number\Plugin\Field\FieldWidget\MobileNumberWidget::errorElement()

Assigns a field-level validation error to the right widget sub-element.

Depending on the widget's internal structure, a field-level validation error needs to be flagged on the right sub-element.

Parameters

array $element: An array containing the form element for the widget, as generated by formElement().

\Symfony\Component\Validator\ConstraintViolationInterface $violation: A constraint violation reported during the validation phase.

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 current state of the form.

Return value

array|bool The element on which the error should be flagged, or FALSE to completely ignore the violation (use with care!).

Overrides WidgetBase::errorElement

File

src/Plugin/Field/FieldWidget/MobileNumberWidget.php, line 195

Class

MobileNumberWidget
Plugin implementation of the 'mobile_number' widget.

Namespace

Drupal\mobile_number\Plugin\Field\FieldWidget

Code

public function errorElement(array $element, ConstraintViolationInterface $error, array $form, FormStateInterface $form_state) {

  /** @var \Drupal\mobile_number\Element\MobileNumberUtilInterface $util */
  $util = \Drupal::service('mobile_number.util');
  $op = MobileNumber::getOp($element, $form_state);
  $mobile_number = MobileNumber::getMobileNumber($element);
  if ($op == 'mobile_number_send_verification' && $mobile_number && ($util
    ->checkFlood($mobile_number) || $util
    ->checkFlood($mobile_number, 'sms'))) {
    return FALSE;
  }
  return parent::errorElement($element, $error, $form, $form_state);
}