You are here

function cck_phone_phone_number_validate in Phone Number 7

An #element_validate callback for the phone_number element.

1 string reference to 'cck_phone_phone_number_validate'
cck_phone_element_info in ./cck_phone.module
Implements hook_element_info().

File

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

Code

function cck_phone_phone_number_validate(&$element, &$form_state) {
  $item = $element['#value'];
  $field = field_widget_field($element, $form_state);
  $instance = field_widget_instance($element, $form_state);
  $settings = $instance['settings'];
  if (isset($item['number'])) {
    $phone_input = trim($item['number']);
  }
  if (isset($item['country_codes'])) {
    $countrycode = trim($item['country_codes']);
  }
  $ext_input = '';
  if ($settings['enable_extension'] && isset($item['extension'])) {
    $ext_input = trim($item['extension']);
  }
  if (isset($phone_input) && !empty($phone_input)) {
    if (empty($countrycode)) {
      form_set_error($field['field_name'], t('The phone number must be accompanied by a country code.'));
    }
    else {
      $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 ($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);
        }
        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));
              }
            }
          }
        }
      }
    }
  }
}