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, DrupalCoreFormFormStateInterface $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, DrupalCoreFormFormStateInterface $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, DrupalCoreFormFormStateInterface $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
Sponsored by SupremePR