You are here

public function BlacklistFieldValidationRule::validate in Field Validation 8

Overrides FieldValidationRuleBase::validate

File

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

Class

BlacklistFieldValidationRule
BlacklistFieldValidationRule.

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;
  }
  $setting = isset($settings['setting']) ? $settings['setting'] : '';
  $blacklist = explode(',', $setting);
  $blacklist = array_map('trim', $blacklist);
  $blacklist_regex = implode('|', $blacklist);

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