public function RegexpValidator::validate in Form API Validation 8.2
Execute validation.
Parameters
\Drupal\fapi_validation\Validator $validator: Validator.
array $element: Form Element.
\Drupal\Core\Form\FormStateInterface $form_state: Form State.
Return value
bool Check.
Overrides FapiValidationValidatorsInterface::validate
File
- src/
Plugin/ FapiValidationValidator/ RegexpValidator.php, line 22
Class
- RegexpValidator
- Fapi Validation Plugin for Regex validation.
Namespace
Drupal\fapi_validation\Plugin\FapiValidationValidatorCode
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);
}