You are here

abstract class CvValidatorBase in Clientside Validation 3.0.x

Same name and namespace in other branches
  1. 8.2 src/CvValidatorBase.php \Drupal\clientside_validation\CvValidatorBase
  2. 8 src/CvValidatorBase.php \Drupal\clientside_validation\CvValidatorBase
  3. 2.0.x src/CvValidatorBase.php \Drupal\clientside_validation\CvValidatorBase

Class CvValidatorBase.

@package Drupal\clientside_validation

Hierarchy

Expanded class hierarchy of CvValidatorBase

10 files declare their use of CvValidatorBase
Email.php in src/Plugin/CvValidator/Email.php
EqualTo.php in clientside_validation_jquery/src/Plugin/CvValidator/EqualTo.php
Max.php in src/Plugin/CvValidator/Max.php
MaxLength.php in src/Plugin/CvValidator/MaxLength.php
Min.php in src/Plugin/CvValidator/Min.php

... See full list

File

src/CvValidatorBase.php, line 16

Namespace

Drupal\clientside_validation
View source
abstract class CvValidatorBase extends PluginBase implements CvValidatorInterface {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this->pluginDefinition['name'];
  }

  /**
   * Get the validation rules for this form element.
   *
   * @return array
   *   An array with following keys:
   *     - rules: An array with the rulename as key and the rule arguments as
   *       value.
   *     - messages: An array with the rulename as key and the message for this
   *       rule as argument.
   */
  protected abstract function getRules($element, FormStateInterface $form_state);

  /**
   * {@inheritdoc}
   */
  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']);
    }
  }

  /**
   * Get the value of an attribute of an element.
   *
   * @param array $element
   *   The element to get the attribute from.
   * @param string $attribute
   *   The attribute of which to get the value.
   *
   * @return mixed
   *   The attribute value.
   */
  protected function getAttributeValue(array $element, $attribute) {
    return isset($element['#' . $attribute]) ? $element['#' . $attribute] : (isset($element['#attributes'][$attribute]) ? $element['#attributes'][$attribute] : NULL);
  }

  /**
   * Get title for form element.
   *
   * @param array $element
   *   Element.
   * @param string $default
   *   Default value for the title.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup|mixed
   *   Title value.
   */
  protected function getElementTitle(array $element, $default = '') {
    $title = $this
      ->getAttributeValue($element, 'title');
    if (empty($title) && empty($default)) {
      return t('This field');
    }
    return $title ? $title : $default;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CvValidatorBase::addValidation public function Makes the necessary changes to the form element so it can be validated. Overrides CvValidatorInterface::addValidation 1
CvValidatorBase::getAttributeValue protected function Get the value of an attribute of an element.
CvValidatorBase::getElementTitle protected function Get title for form element.
CvValidatorBase::getName public function Return the name of the validator flavor. Overrides CvValidatorInterface::getName
CvValidatorBase::getRules abstract protected function Get the validation rules for this form element. 10
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.