class RegexpValidator in Form API Validation 8.2
Fapi Validation Plugin for Regex validation.
Plugin annotation
@FapiValidationValidator(
id = "regexp",
error_message = "%field value does not match rule."
)
Hierarchy
- class \Drupal\fapi_validation\Plugin\FapiValidationValidator\RegexpValidator implements FapiValidationValidatorsInterface
Expanded class hierarchy of RegexpValidator
File
- src/
Plugin/ FapiValidationValidator/ RegexpValidator.php, line 17
Namespace
Drupal\fapi_validation\Plugin\FapiValidationValidatorView source
class RegexpValidator implements FapiValidationValidatorsInterface {
/**
* {@inheritdoc}
*/
public function validate(Validator $validator, array $element, FormStateInterface $form_state) {
$params = $validator
->getParams();
$value = $validator
->getValue();
// Some FAPI elements types, such as those provided by the Date API module,
// will come in as an array (with date in one element and time in another).
// To handle this use-case we simply implode them into a string.
if (is_array($value)) {
// Using array filter ensures that empty array elements do not cause an
// extra space to be added to the value. We can't use trim to fix this
// issue cause trim will remove all trailing whitespace in a string,
// which may be meaningful.
$value = implode(' ', array_filter($value));
}
return (bool) preg_match($params[0], (string) $value);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RegexpValidator:: |
public | function |
Execute validation. Overrides FapiValidationValidatorsInterface:: |