You are here

function _cck_phone_validate in Phone Number 7

Same name and namespace in other branches
  1. 6 cck_phone.module \_cck_phone_validate()
1 call to _cck_phone_validate()
cck_phone_field_validate in ./cck_phone.module
Implements hook_field_validate().

File

./cck_phone.module, line 466
Defines phone number fields for CCK. Provide some verifications on the phone numbers

Code

function _cck_phone_validate(&$item, $delta, $field, $instance, $langcode, &$errors) {
  if (isset($item['number'])) {
    $phone_input = trim($item['number']);
  }
  if (isset($item['country_codes'])) {
    $countrycode = trim($item['country_codes']);
  }
  $ext_input = '';
  $settings = $instance['settings'];
  if ($settings['enable_extension']) {
    $ext_input = trim($item['extension']);
  }
  if (isset($phone_input) && !empty($phone_input)) {
    $error_params = array(
      '%phone_input' => check_plain($phone_input),
      // original phone input
      '%countrycode' => check_plain($countrycode),
      '%min_length' => CCK_PHONE_PHONE_MIN_LENGTH,
      '%max_length' => CCK_PHONE_PHONE_MAX_LENGTH,
      '%ext_input' => check_plain($ext_input),
      '%ext_max_length' => CCK_PHONE_EXTENSION_MAX_LENGTH,
    );

    // Only allow digit, dash, space and bracket
    if (!_cck_phone_valid_input($phone_input, $ext_input)) {
      $error = t('The phone number must be between %min_length and %max_length digits in length.', $error_params);
      if ($settings['enable_extension'] && $ext_input != '') {
        $error .= '<br />' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
      }
      form_set_error($field['field_name'], $error);
    }
    else {
      if (!$settings['all_country_codes']) {
        if (!_cck_phone_valid_cc_input($settings['country_codes']['country_selection'], $countrycode)) {
          $error = t('Invalid country code "%countrycode" submitted.', $error_params);
          form_set_error($field['field_name'], $error);
        }
      }

      // Generic number validation
      if (!cck_phone_validate_number($countrycode, $phone_input, $ext_input)) {
        $error = t('The phone number must be between %min_length and %max_length digits in length.', $error_params);
        if ($field['enable_extension'] && $ext_input != '') {
          $error .= '<br />' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
        }
        form_set_error($field['field_name'], $error);
      }
      elseif ($settings['enable_country_level_validation']) {
        $custom_cc = _cck_phone_custom_cc();
        if (isset($custom_cc[$countrycode])) {
          $validate_function = $countrycode . '_validate_number';
          if (function_exists($validate_function)) {
            $error = '';
            if (!$validate_function($phone_input, $ext_input, $error)) {
              form_set_error($field['field_name'], t($error, $error_params));
            }
          }
        }
      }
    }
  }
}