You are here

public static function Cpf::validateCpf in CPF 8

Same name and namespace in other branches
  1. 8.2 src/Element/Cpf.php \Drupal\cpf\Element\Cpf::validateCpf()

Form element validation handler for #type 'cpf'.

Note that #maxlength and #required is validated by _form_validate() already.

File

src/Element/Cpf.php, line 80

Class

Cpf
Provides a form input element for entering an CPF number.

Namespace

Drupal\cpf\Element

Code

public static function validateCpf(&$element, FormStateInterface $form_state, &$complete_form) {
  $value = trim($element['#value']);
  $form_state
    ->setValueForElement($element, $value);
  if ($value !== '' && !\Drupal::service('cpf')
    ->isValid($value)) {
    $form_state
      ->setError($element, t('The CPF number %cpf is not valid.', [
      '%cpf' => $value,
    ]));
  }
}