You are here

public function field_validation_numeric_validator::validate in Field Validation 7.2

Validate field.

Overrides field_validation_validator::validate

File

field_validation_extras/plugins/validator/field_validation_numeric_validator.inc, line 21

Class

field_validation_numeric_validator

Code

public function validate() {
  $settings = $this->rule->settings;
  if ($this->value !== '' && !is_null($this->value)) {
    $flag = TRUE;
    if (!is_numeric($this->value)) {
      $flag = FALSE;
    }
    else {
      if (isset($settings['min']) && $settings['min'] != '') {
        $min = token_replace($settings['min'], array(
          $this
            ->get_token_type() => $this->entity,
        ));
        if ($this->value < $min) {
          $flag = FALSE;
        }
      }
      if (isset($settings['max']) && $settings['max'] != '') {
        $max = token_replace($settings['max'], array(
          $this
            ->get_token_type() => $this->entity,
        ));
        if ($this->value > $max) {
          $flag = FALSE;
        }
      }
      if (isset($settings['step']) && strtolower($settings['step']) != 'any') {

        // Check that the input is an allowed multiple of #step (offset by #min if
        // #min is set).
        $offset = isset($settings['min']) ? $settings['min'] : 0.0;
        $step = token_replace($settings['step'], array(
          $this
            ->get_token_type() => $this->entity,
        ));

        // The logic code was copied from Drupal 8 core.
        if ($step > 0 && !$this
          ->valid_number_step($this->value, $step, $offset)) {
          $flag = FALSE;
        }
      }
    }
    if (!$flag) {
      $token = array(
        '[min]' => isset($min) ? $min : '',
        '[max]' => isset($max) ? $max : '',
        '[step]' => isset($step) ? $step : '',
      );
      $this
        ->set_error($token);
    }
  }
}