2

Inside my .module file, I have created a hook for alter register_form and add a password field with a custom field-validation:

function xenforo_form_user_register_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $form['password'] = array(
    '#type' => 'password_confirm',
    '#size' => 25,
    '#required' => true,
    '#element_validate' => array('check_password'),
    );
}
function check_password($element, &$form_state) {
  if ($element['#value'] != 'test') {
    form_error($element, t('Error: TEST'));
  }
}

I use #element_validate, and when I submit my form with a bad value $element['#value'] != 'test', I don't have an error and the registration is completed, why #element_validate don't work here please ?

Thanks.

EDIT:

I have find a solution:

  function xenforo_form_user_register_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $form['password'] = array(
      '#type' => 'password_confirm',
      '#size' => 25,
      '#required' => true,
      '#attributes' => array('autocomplete' => 'off'),
      //'#element_validate' => array('check_password'),
      '#process' => array('processPasswordConfirm'),
    );
  }

  function processPasswordConfirm(&$element, \Drupal\Core\Form\FormStateInterface $form_state, &$complete_form) {
    $element['pass1'] =  array(
      '#type' => 'password',
      '#title' => t('Password'),
      '#value' => empty($element['#value']) ? NULL : $element['#value']['pass1'],
      '#required' => $element['#required'],
      '#attributes' => array('class' => array('password-field')),
    );
    $element['pass2'] =  array(
      '#type' => 'password',
      '#title' => t('Confirm password'),
      '#value' => empty($element['#value']) ? NULL : $element['#value']['pass2'],
      '#required' => $element['#required'],
      '#attributes' => array('class' => array('password-confirm')),
    );
    $element['#element_validate'] = array('check_password');
    $element['#tree'] = TRUE;

    return $element;
  }

  function check_password($element, &$form_state) {
    if ($element['#value'] != 'test') {
      //form_error($element, t('Error: TEST'));
    }
  }

But I have again the error with form_error() and password as displayed as two field without enter image description here

2
  • 1
    Probably because password_confirm isn't a single element, it gets converted into two separate fields in a process callback, and doesn't apply the #element_validate to either of those because it can't reconcile which one you were referring to
    – Clive
    Commented Jan 29, 2015 at 14:15
  • Have you a solution please ? I have try with password only, but I need two fields and with "password" and not "password_confirm" I have an another error: form_error($element, t('Error: TEST'));, form_error is not available on .module file ? I search a solution for have password fields and get a plainpassword inside my validate() function and set my own encryption and push the password inside the database by myself. Commented Jan 29, 2015 at 14:24

1 Answer 1

0

As per @Clive comment, it's probably because password_confirm isn't a single element, it gets converted into two separate fields in a process callback, and doesn't apply the #element_validate to either of those because it can't reconcile which one you were referring to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.