Setting "maxlength" attribute on password_confirm field type in Drupal 6

Submitted by boaz on Wednesday, July 1, 2009.

I recently needed to alter a standard password_confirm field type in a form that I maintain. Apparently, setting '#maxlength' property on password_confirm form field has no effect. No helpful at all also proved Drupal FAPI official reference (oddly) which has no mention of password_confirm element(!). Anybody who knows why is welcome to hint me.
Anyhow, after some reaserch on this, and a little help from yhager on IRC, I ended up doing it without hacking Drupal code. Here's how:

  1. Alter the password_confirm form element and put
     $form['password'] = array(
                '#type' => 'password_confirm',
                '#size' => 15,
                '#required' => TRUE,
                '#process'  => array('expand_password_confirm', '<module_name>_pass_confirm_maxlegth'),
    The relevant line is the bold one. Naturally, you can change the second parameter. This parameter is a function name that will be called before the form is rendered.
  2. Implement the <module_name>_pass_confirm_maxlegth and put in it:
     function <module_name>_pass_confirm_maxlegth($element) {
        $element['pass1']["#maxlength"] = 10;
        $element['pass2']["#maxlength"] = 10;
        return $element;   

Explanation: the default #process entry for password_confirm is the "expand_password_confirm" function which is located in includes/ We just added another processing function, which runs before the form element is rendered.

I hope this little recipe would be found useful to other.


Asha says:
July 16, 2012 at 09:22 am
By soulston January 21, 2010 - 3:24 pmJust in case anoyne else finds this, the better approach would be to use hook_form_alter() rather than hacking core.something like:/** * Implementation of hook_form_alter(). */function INSERT_form_alter(&$form, $form_state, $form_id) { if ($form_id == poll_node_form') { $form['title']['#maxlength'] = '255 ; }}
Venkat says:
June 29, 2012 at 04:39 pm
Excellent. It helped me. Thanks.

Leave a Comment

Fields with * are required.