You are here

public function RegexFieldValidationRule::validate in Field Validation 8

Overrides FieldValidationRuleBase::validate

File

src/Plugin/FieldValidationRule/RegexFieldValidationRule.php, line 71

Class

RegexFieldValidationRule
RegexFieldValidationRule.

Namespace

Drupal\field_validation\Plugin\FieldValidationRule

Code

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;
  }
  $pattern = isset($settings['setting']) ? $settings['setting'] : '';

  //$settings = $this->rule->settings;
  if ($value != '' && !preg_match($pattern, $value)) {
    $context
      ->addViolation($rule
      ->getErrorMessage());
  }
}