You are here

fapi_validators.inc in Form API Validation 8

File

src/fapi_validators/fapi_validators.inc
View source
<?php

/**
 * Implementation of hook_fapi_validation_validators
 */
function fapi_validation_fapi_validation_validators() {
  return array(
    'numeric' => array(
      'callback' => 'fapi_validation_validator_numeric',
      'error_msg' => t('Use only numbers at %field.'),
    ),
    'alpha' => array(
      'callback' => 'fapi_validation_validator_alpha',
      'error_msg' => t('Use only alpha characters at %field.'),
    ),
    'alpha_numeric' => array(
      'callback' => 'fapi_validation_validator_alpha_numeric',
      'error_msg' => t('Use only alpha numerics characters at %field.'),
    ),
    'email' => array(
      'callback' => 'fapi_validation_validator_email',
      'error_msg' => t('%field is not a valid email.'),
    ),
    'length' => array(
      'callback' => 'fapi_validation_validator_length',
      'error_msg' => t('Invalid size of %field value.'),
    ),
    'chars' => array(
      'callback' => 'fapi_validation_validator_chars',
      'error_msg' => t('Invalid characters on %field value.'),
    ),
    'url' => array(
      'callback' => 'fapi_validation_validator_url',
      'error_msg' => t('Invalid format of %field.'),
    ),
    'ipv4' => array(
      'callback' => 'fapi_validation_validator_ipv4',
      'error_msg' => t('Invalid format of %field.'),
    ),
    'alpha_dash' => array(
      'callback' => 'fapi_validation_validator_alpha_dash',
      'error_msg' => t('Use only alpha numerics, hyphen and underscore at %field.'),
    ),
    'digit' => array(
      'callback' => 'fapi_validation_validator_digit',
      'error_msg' => t('Use only digits on %field.'),
    ),
    'decimal' => array(
      'callback' => 'fapi_validation_validator_decimal',
      'error_msg' => t('Use only decimal on %field.'),
    ),
    'regexp' => array(
      'callback' => 'fapi_validation_validator_regexp',
      'error_msg' => t('%field value does not match rule.'),
    ),
    'match_field' => array(
      'callback' => 'fapi_validation_validator_match_field',
      'error_msg' => t('%field value does not match other field.'),
    ),
    'range' => array(
      'callback' => 'fapi_validation_validator_range',
      'error_msg' => t('%field value is out of range.'),
    ),
    'limit_decimals' => array(
      'callback' => 'fapi_validation_validator_limit_decimals',
      'error_msg' => t('Invalid value for %field or too many decimal digits'),
    ),
  );
}

/**
 * Validators
 */
function fapi_validation_validator_numeric($value) {
  return is_numeric($value);
}
function fapi_validation_validator_alpha($value) {
  return (bool) preg_match('/^[\\pL]++$/uD', (string) $value);
}
function fapi_validation_validator_alpha_numeric($value) {
  return (bool) preg_match('/^[\\pL\\pN]++$/uD', (string) $value);
}
function fapi_validation_validator_email($value) {
  return \Drupal::service('email.validator')
    ->isValid($value);
}
function fapi_validation_validator_length($value, $params) {
  $size = mb_strlen($value);
  if (count($params) == 1) {
    return $size == (int) $params[0];
  }
  elseif (count($params) == 2) {
    if ($params[1] == '*') {
      return $size >= (int) $params[0];
    }
    return $size >= (int) $params[0] && $size <= (int) $params[1];
  }
}
function fapi_validation_validator_url($value, $params = array()) {
  return valid_url($value, !empty($params) && $params[0] == 'absolute');
}
function fapi_validation_validator_ipv4($value) {
  $pattern = '/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])' . '(\\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/';
  return (bool) preg_match($pattern, $value);
}
function fapi_validation_validator_alpha_dash($value) {
  return (bool) preg_match('/^[-\\pL\\pN_]+$/uD', (string) $value);
}
function fapi_validation_validator_digit($value) {
  return (bool) preg_match('/^\\d+$/', $value);
}
function fapi_validation_validator_chars($value, $params) {
  for ($i = 0; $i < mb_strlen($value); $i++) {
    $current = substr($value, $i, 1);
    if (!in_array($current, $params)) {
      return FALSE;
    }
  }
  return TRUE;
}
function fapi_validation_validator_decimal($value, $params = array()) {
  if (count($params) == 2) {
    $pattern = '/^[0-9]{' . $params[0] . '}\\.[0-9]{' . $params[1] . '}$/';
    return (bool) preg_match($pattern, (string) $value);
  }
  else {
    return (bool) preg_match('/\\d+\\.\\d+/', $value);
  }
  return FALSE;
}
function fapi_validation_validator_regexp($value, $params) {

  // Some FAPI elements types, such as those provided by the Date API module,
  // will come in as an array (with date in one element and time in another). To
  // handle this use-case we simply implode them into a string.
  if (is_array($value)) {

    // Using array filter ensures that empty array elements do not cause an
    // extra space to be added to the value. We can't use trim to fix this issue
    // cause trim will remove all trailing whitespace in a string, which may be
    // meaningful.
    $value = implode(' ', array_filter($value));
  }
  return (bool) preg_match($params[0], (string) $value);
}
function fapi_validation_validator_match_field($value, $params, $element, $form_state) {
  return isset($form_state['values'][$params[0]]) && $value == $form_state['values'][$params[0]];
}
function fapi_validation_validator_range($value, $params) {
  $min = $params[0];
  $max = $params[1];
  return $min <= $value && $max >= $value;
}

/**
 *  Limits decimals allowed
 */
function fapi_validation_validator_limit_decimals($value, $params) {
  if (!is_numeric($value)) {
    return FALSE;
  }
  if (count($params) > 0) {
    $value = (double) $value;
    $pattern = '/^[^\\.]*\\.?[0-9]{0,' . $params[0] . '}$/';
    return (bool) preg_match($pattern, (string) $value);
  }
  return TRUE;
}