You are here

function vat_number_field_widget_element_validate in VAT Number 7

Custom validation callback for vat_number_field_widget_form()

Parameters

array $element:

array $form_state:

1 string reference to 'vat_number_field_widget_element_validate'
vat_number_field_widget_form in ./vat_number.module
Implements hook_field_widget_form().

File

./vat_number.module, line 122
Define a VAT Number field for use on Drupal Commerce Customer Profiles.

Code

function vat_number_field_widget_element_validate($element, &$form_state) {

  //$field = field_widget_field($element, $form_state);

  // Find out are we tending to skip validation if service is having troubles.
  $instance = field_widget_instance($element, $form_state);
  $skip_validation_on_service_failure = $instance['widget']['settings']['skip_validation_on_service_failure'];
  $value = $element['#value'];
  $message = '';

  // Reject invalid characters.
  if (!empty($value)) {

    //we want all messages inside the form so we create them here

    // 1: Check structure, 2: Check validity

    //at first check the format of VAT, if its ok ...
    $vat_format_check = _vat_number_check_vat_format($value);
    if (isset($vat_format_check['message'])) {
      $message = $vat_format_check['message'];
    }
    elseif (_vat_number_validate_vat($value, $skip_validation_on_service_failure) === FALSE) {
      $url_vies = 'http://ec.europa.eu/taxation_customs/vies/';
      $link_vies = l(t('the European Union\'s VAT Information Exchange System'), $url_vies, array(
        'attributes' => array(
          'target' => '_blank',
          'title' => $url_vies,
        ),
      ));
      $placeholders = array(
        '!link_vies' => $link_vies,
      );
      $message .= t('The provided VAT ID number is invalid according to !link_vies.<br />Please ensure to enter your correct VAT ID number!', $placeholders);
    }
    if (!empty($message)) {
      form_error($element, $message);
    }
  }
}