class Validator in Form API Validation 8.2
Validator Class to parse Form Element validators content.
Hierarchy
- class \Drupal\fapi_validation\Validator
Expanded class hierarchy of Validator
18 files declare their use of Validator
- AlphaDashValidator.php in src/
Plugin/ FapiValidationValidator/ AlphaDashValidator.php - AlphaNumericValidator.php in src/
Plugin/ FapiValidationValidator/ AlphaNumericValidator.php - AlphaValidator.php in src/
Plugin/ FapiValidationValidator/ AlphaValidator.php - AlphaValidatorTest.php in tests/
src/ Unit/ Validators/ AlphaValidatorTest.php - CharsValidator.php in src/
Plugin/ FapiValidationValidator/ CharsValidator.php
File
- src/
Validator.php, line 8
Namespace
Drupal\fapi_validationView source
class Validator {
/**
* Raw validator content.
*
* @var array|string
*/
private $rawValidator;
/**
* Form element value.
*
* @var string
*/
private $value;
/**
* Rule name.
*
* @var string
*/
private $name;
/**
* Rule parameters.
*
* @var array
*/
private $params = [];
/**
* User defined error message.
*
* @var string
*/
private $errorMessage;
/**
* User defined error callback.
*
* @var string
*/
private $errorCallback;
/**
* Create object and parse validator data.
*
* @param array|string $raw_validator
* Raw user defined validator.
* @param string $value
* Form element value.
*/
public function __construct($raw_validator, $value) {
$this->rawValidator = $raw_validator;
$this->value = $value;
$this
->parse();
}
/**
* Parse user defined validator.
*/
private function parse() {
if (is_array($this->rawValidator)) {
if (isset($this->rawValidator['error'])) {
$this->error_message = $this->rawValidator['error'];
}
if (isset($this->rawValidator['error callback'])) {
$this->error_callback = $this->rawValidator['error callback'];
}
if (!isset($this->rawValidator['rule'])) {
throw new \LogicException("You can't define a validator as array and don't define 'rule' key.");
}
$this->rawValidator = $this->rawValidator['rule'];
}
preg_match('/^(.*?)(\\[(.*)\\])?$/', $this->rawValidator, $rs);
$this->name = $rs[1];
if (isset($rs[3])) {
if ($this->name == 'regexp') {
$this->params = [
$rs[3],
];
}
else {
$this->params = preg_split('/ *, */', $rs[3]);
}
}
}
/**
* Return Form Element value.
*
* @return string
* Value.
*/
public function getValue() {
return $this->value;
}
/**
* Return rule name.
*
* @return string
* Rule name.
*/
public function getName() {
return $this->name;
}
/**
* Return rule parameters.
*
* @return array
* Params.
*/
public function getParams() {
return $this->params;
}
/**
* Check if there is user defined error message.
*
* @return bool
* Check.
*/
public function hasErrorMessageDefined() {
return $this->errorMessage !== NULL;
}
/**
* Get User defined error error message.
*
* @return string
* Error messaage.
*/
public function getErrorMessage() {
return $this->errorMessage;
}
/**
* Check if there is user defined error callback.
*
* @return bool
* Check.
*/
public function hasErrorCallbackDefined() {
return $this->errorMessage !== NULL;
}
/**
* Return user defined error callback.
*
* @return string
* Erro Callback.
*/
public function getErrorCallback() {
return $this->errorCallback;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Validator:: |
private | property | User defined error callback. | |
Validator:: |
private | property | User defined error message. | |
Validator:: |
private | property | Rule name. | |
Validator:: |
private | property | Rule parameters. | |
Validator:: |
private | property | Raw validator content. | |
Validator:: |
private | property | Form element value. | |
Validator:: |
public | function | Return user defined error callback. | |
Validator:: |
public | function | Get User defined error error message. | |
Validator:: |
public | function | Return rule name. | |
Validator:: |
public | function | Return rule parameters. | |
Validator:: |
public | function | Return Form Element value. | |
Validator:: |
public | function | Check if there is user defined error callback. | |
Validator:: |
public | function | Check if there is user defined error message. | |
Validator:: |
private | function | Parse user defined validator. | |
Validator:: |
public | function | Create object and parse validator data. |