abstract class CvValidatorBase in Clientside Validation 3.0.x
Same name and namespace in other branches
- 8.2 src/CvValidatorBase.php \Drupal\clientside_validation\CvValidatorBase
- 8 src/CvValidatorBase.php \Drupal\clientside_validation\CvValidatorBase
- 2.0.x src/CvValidatorBase.php \Drupal\clientside_validation\CvValidatorBase
Class CvValidatorBase.
@package Drupal\clientside_validation
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\clientside_validation\CvValidatorBase implements CvValidatorInterface uses StringTranslationTrait
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
File
- src/
CvValidatorBase.php, line 16
Namespace
Drupal\clientside_validationView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CvValidatorBase:: |
public | function |
Makes the necessary changes to the form element so it can be validated. Overrides CvValidatorInterface:: |
1 |
CvValidatorBase:: |
protected | function | Get the value of an attribute of an element. | |
CvValidatorBase:: |
protected | function | Get title for form element. | |
CvValidatorBase:: |
public | function |
Return the name of the validator flavor. Overrides CvValidatorInterface:: |
|
CvValidatorBase:: |
abstract protected | function | Get the validation rules for this form element. | 10 |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |