You are here

function _postal_code_postal_validate in Postal Code 7

Validation of Postal Code element.

1 string reference to '_postal_code_postal_validate'
postal_code_field_widget_form in ./postal_code.module
Implements hook_field_widget_form().

File

./postal_code.module, line 199

Code

function _postal_code_postal_validate($element, &$form_state, $form) {
  $value = trim($element['#value']);
  if (!empty($value) && variable_get('postal_code_validate', 0)) {

    // Locate 'postal_type' in the form.
    $arr = explode('[', str_replace(']', '', str_replace('[postal]', '[postal_type]', $element['#name'])));
    $country_code = $form_state['values'];
    for ($i = 0, $j = count($arr); $i < $j; $i++) {

      // Format the array properly to get back down to postal_type.
      $country_code = $country_code[$arr[$i]];
    }
    if (!empty($country_code)) {
      if ($country_code != 'any') {
        $error_array = _postal_code_validator($country_code, $value);
      }
      else {
        $validatable_countries = variable_get('postal_code_valid_countries');
        foreach ($validatable_countries as $key => $country) {
          $err_array[] = _postal_code_validator($country, $value);
        }
        foreach ($err_array as $k => $v) {
          $error_array[] = $v[0];
        }
      }
    }
    else {
      form_error($element, t('This form has been altered in a way in which Postal Code validation will not work, but the validation option remains enabled. Please correct the changes to the form or disable the validation option.'));
    }
    if (!in_array(TRUE, $error_array)) {
      form_error($element, t('Invalid Postal Code Provided.'));
    }
  }
}