You are here

public function CvValidatorBase::addValidation in Clientside Validation 8

Same name and namespace in other branches
  1. 8.2 src/CvValidatorBase.php \Drupal\clientside_validation\CvValidatorBase::addValidation()
  2. 3.0.x src/CvValidatorBase.php \Drupal\clientside_validation\CvValidatorBase::addValidation()
  3. 2.0.x src/CvValidatorBase.php \Drupal\clientside_validation\CvValidatorBase::addValidation()

Makes the necessary changes to the form element so it can be validated.

Parameters

array $element: The form element to validate.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the form this element belongs to.

Overrides CvValidatorInterface::addValidation

1 call to CvValidatorBase::addValidation()
UrlInternalExternal::addValidation in src/Plugin/CvValidator/UrlInternalExternal.php
Makes the necessary changes to the form element so it can be validated.
1 method overrides CvValidatorBase::addValidation()
UrlInternalExternal::addValidation in src/Plugin/CvValidator/UrlInternalExternal.php
Makes the necessary changes to the form element so it can be validated.

File

src/CvValidatorBase.php, line 41

Class

CvValidatorBase
Class CvValidatorBase.

Namespace

Drupal\clientside_validation

Code

public function addValidation(array &$element, FormStateInterface $form_state) {
  $rules = $this
    ->getRules($element, $form_state);
  if (isset($rules['rules'])) {
    foreach ($rules['rules'] as $rulename => $rulearg) {
      $element['#attributes']['data-rule-' . mb_strtolower($rulename)] = is_object($rulearg) || is_array($rulearg) ? Json::encode($rulearg) : $rulearg;
    }
  }
  if (isset($rules['messages'])) {
    foreach ($rules['messages'] as $rulename => $message) {
      $element['#attributes']['data-msg-' . mb_strtolower($rulename)] = $message;
    }
  }
  if (isset($rules['messages']) || isset($rules['rules'])) {
    if (!isset($element['#attached'])) {
      $element['#attached'] = [];
    }
    $element['#attached'] = NestedArray::mergeDeep($element['#attached'], $this
      ->getPluginDefinition()['attachments']);
  }
}