You are here

function _clientside_validation_set_minmax in Clientside Validation 7

Same name and namespace in other branches
  1. 6 clientside_validation.module \_clientside_validation_set_minmax()

Set validation rule for fields with a minimum and/or a maximum value.

5 calls to _clientside_validation_set_minmax()
clientside_validation_field_validation_regular in clientside_validation_field_validation/clientside_validation_field_validation.module
clientside_validation_regular in clientside_validation_form/clientside_validation_form.module
clientside_validation_webform_add_webform_validation in clientside_validation_webform/clientside_validation_webform.module
Support webform_validation
clientside_validation_webform_after_build_recurse in clientside_validation_webform/clientside_validation_webform.module
_clientside_validation_set_minmax_html5 in clientside_validation_html5/clientside_validation_html5.module

File

./clientside_validation.module, line 612
Add client side validation to forms.

Code

function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules, $message = '', $decimal_separator = '.') {
  $title = _clientside_validation_set_title($title);
  if (isset($min) && $min !== '' && isset($max) && $max !== '') {
    $rule = $decimal_separator == '.' ? 'range' : 'range_comma';
    $js_rules[$name][$rule] = array(
      $min,
      $max,
    );
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to be between !min and !max.',
        'placeholders' => array(
          '!title' => $title,
          '!min' => $min,
          '!max' => $max,
        ),
        'error_type' => $rule,
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => $rule,
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages'][$rule] = theme('clientside_error', $variables);
  }
  elseif (isset($min) && $min !== '') {
    $rule = $decimal_separator == '.' ? 'min' : 'min_comma';
    $js_rules[$name][$rule] = $min;
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to be greater than !min.',
        'placeholders' => array(
          '!title' => $title,
          '!min' => $min,
        ),
        'error_type' => $rule,
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => $rule,
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages'][$rule] = theme('clientside_error', $variables);
  }
  elseif (isset($max) && $max !== '') {
    $rule = $decimal_separator == '.' ? 'max' : 'max_comma';
    $js_rules[$name][$rule] = $max;
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to be smaller than !max.',
        'placeholders' => array(
          '!title' => $title,
          '!max' => $max,
        ),
        'error_type' => $rule,
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => $rule,
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages'][$rule] = theme('clientside_error', $variables);
  }
}