You are here

function field_nif_validate_nif_number in Field NIF 7

Validate handler to check if the number is correct and store the values splitted into their correct fields in the database.

1 string reference to 'field_nif_validate_nif_number'
field_nif_field_widget_form in ./field_nif.module
Implements hook_field_widget_form().

File

./field_nif.module, line 94

Code

function field_nif_validate_nif_number($element, &$form_state) {
  if (!empty($element['nif']['#value'])) {
    if (strlen($element['nif']['#value']) != 9) {
      form_error($element['nif'], t('NIF/CIF/NIE number needs to be 9 characters long'));
    }
    else {

      // Load the utilities for getting the NIF splitted.
      module_load_include('inc', 'field_nif', 'field_nif.utils');
      $nif_parts = _field_nif_validate_dni_cif_nie($element['nif']['#value'], $form_state['instance']['settings']['supported_numbers']);
      if ($nif_parts) {
        form_set_value($element['number'], $nif_parts['number'], $form_state);
        form_set_value($element['first_letter'], isset($nif_parts['first_letter']) ? $nif_parts['first_letter'] : '', $form_state);
        form_set_value($element['last_letter'], isset($nif_parts['last_letter']) ? $nif_parts['last_letter'] : '', $form_state);
        form_set_value($element['type'], isset($nif_parts['type']) ? $nif_parts['type'] : '', $form_state);
      }
      else {
        form_error($element['nif'], t('NIF/CIF/NIE number is not correct'));
      }
    }
  }
}