You are here

public static function Nif::validateNif in Field NIF 8

Form element validation handler for NIF/NIE/CIF elements.

Parameters

array $element: The form element to validate.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: The complete form structure.

Return value

array The processed element.

File

src/Element/Nif.php, line 85

Class

Nif
Provides a NIF/NIE/CIF render element.

Namespace

Drupal\field_nif\Element

Code

public static function validateNif(&$element, FormStateInterface $form_state, &$complete_form) {
  $value = trim($element['#value']);
  $supported_types = array_filter($element['#supported_types']);
  $form_state
    ->setValueForElement($element, $value);
  if ($value !== '' && !NifUtils::validateNifCifNie($value, $element['#supported_types'])) {
    $form_state
      ->setError($element, t('@value is not a valid @document_type document number.', [
      '@value' => $value,
      '@document_type' => mb_strtoupper(implode('/', $supported_types)),
    ]));
  }
}