You are here

public function WordsFieldValidationRule::validate in Field Validation 8

Overrides FieldValidationRuleBase::validate

File

src/Plugin/FieldValidationRule/WordsFieldValidationRule.php, line 82

Class

WordsFieldValidationRule
WordsFieldValidationRule.

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;
  }
  if ($value != '') {
    $flag = TRUE;
    $length = count(explode(' ', trim(preg_replace('/\\s+/', ' ', str_replace('&nbsp;', ' ', strip_tags(str_replace('<', ' <', $value)))))));
    if (isset($settings['min']) && $settings['min'] != '') {
      $min = $settings['min'];
      if ($length < $min) {
        $flag = FALSE;
      }
    }
    if (isset($settings['max']) && $settings['max'] != '') {
      $max = $settings['max'];
      if ($length > $max) {
        $flag = FALSE;
      }
    }
    if (!$flag) {

      //$this->set_error($token);
      $context
        ->addViolation($rule
        ->getErrorMessage());
    }
  }

  //return true;
}