Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 300566

Password confirm field type should have support for #maxlength attribute

$
0
0

Use Case: password_confirm form field type isn't usable when there needs to be max character limit

When a custom form is to be developed that has password and confirm password fields, with a restriction on number of characters allowed (maxlength).

Based on the above requirements password_confirm is the correct form field type to use.

However since password_confirm field type does not support #maxlength attribute, Two seperate fields of type password will need to be provided, in addition custom form validator had to be written that duplicated the logic of what password_confirm field provides.

Proposed resolution: Allow #maxlength attribute for password_confirm field

Change required
A small change in form.inc should solve the problem. function form_process_password_confirm($element) , should have following additional lines

   if (isset($element['#maxlength'])) {
    $element['pass1']['#maxlength'] = $element['pass2']['#maxlength'] = $element['#maxlength'];
  }

Function should look as below

/**
 * Expand a password_confirm field into two text boxes.
 */
function form_process_password_confirm($element) {
  $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('password_confirm_validate');
  $element['#tree'] = TRUE;

  if (isset($element['#size'])) {
    $element['pass1']['#size'] = $element['pass2']['#size'] = $element['#size'];
  }
  if (isset($element['#maxlength'])) {
    $element['pass1']['#maxlength'] = $element['pass2']['#maxlength'] = $element['#maxlength'];
  }
  return $element;
}

Remaining tasks

API changes

Forms API


Viewing all articles
Browse latest Browse all 300566

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>