[Skip Header and Navigation] [Jump to Main Content]
Home
Rymland
Where the Rymlands meet

Languages

  • English
  • עברית

User login

  • Create new account
  • Request new password
Home » Blogs » boaz's blog

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

Submitted by boaz on Wed, 07/01/2009 - 12:21
  • howto
  • technical
  • drupal 6
  • form api

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/form..inc. 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.
Boaz.

  • boaz's blog
  • Add new comment

Thanks yaar

Submitted by Anonymous on Fri, 11/27/2009 - 16:36.

Good Explanation.Thanks Boaz.

  • reply
[Jump to Top] [Jump to Main Content]