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
- issue summary needs updating to summarize different possible approaches. See contributor task on updating issue summaries http://drupal.org/node/1427826
- Forms API documentation to be updated
- Forms Examples to be updated
- needs screenshot. See contributor task doc on adding screenshots: http://drupal.org/node/1859584 Before screenshot can be made. Waiting on patch to make after screenshot.
- Possible backport to D7 & D6
API changes
Forms API