public function PhoneFieldValidationRule::validate in Field Validation 8
Overrides FieldValidationRuleBase::validate
File
- src/
Plugin/ FieldValidationRule/ PhoneFieldValidationRule.php, line 75
Class
- PhoneFieldValidationRule
- PhoneFieldValidationRule.
Namespace
Drupal\field_validation\Plugin\FieldValidationRuleCode
public function validate($params) {
$value = isset($params['value']) ? $params['value'] : '';
$rule = isset($params['rule']) ? $params['rule'] : null;
$context = isset($params['context']) ? $params['context'] : null;
$settings = array();
if (!empty($rule) && !empty($rule->configuration)) {
$settings = $rule->configuration;
}
if ($value !== '' && !is_null($value)) {
$country_code = isset($settings['country']) ? $settings['country'] : '';
$country_regex = '';
$countries = $this
->phone_countries();
$country_regex = isset($countries[$country_code]['regex']) ? $countries[$country_code]['regex'] : '';
if (!preg_match($country_regex, $value)) {
$context
->addViolation($rule
->getErrorMessage());
}
}
}