You are here

public function IntegerFieldValidationRule::validate in Field Validation 8

Overrides FieldValidationRuleBase::validate

File

src/Plugin/FieldValidationRule/IntegerFieldValidationRule.php, line 77

Class

IntegerFieldValidationRule
IntegerFieldValidationRule.

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;
  }

  //$settings = $this->rule->settings;
  if ($value !== '' && !is_null($value)) {
    $options = array();
    if (isset($settings['min']) && $settings['min'] != '') {
      $min = $settings['min'];
      $options['options']['min_range'] = $min;
    }
    if (isset($settings['max']) && $settings['max'] != '') {
      $max = $settings['max'];
      $options['options']['max_range'] = $max;
    }
    if (FALSE === filter_var($value, FILTER_VALIDATE_INT, $options)) {
      $context
        ->addViolation($rule
        ->getErrorMessage());
    }
  }

  //return true;
}