[QuickForm] compare two fields

From: Date: Sat, 01 Jun 2002 09:53:49 +0000
Subject: [QuickForm] compare two fields
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-1558@lists.php.net to get a copy of this message
<graeme@inetix.com.au> wrote : > Been meaning to drop you a note anyway and ask how you'd write a rule to > compare two fields in a form - say for password confirmation. Any clues? I > got as far as: > > $form->addElement('password', 'passwd1', 'Enter password'); > $form->addElement('password', 'passwd2', 'Confirm password'); > $form->addRule('passwd1', 'Please enter password', > 'required'); > $form->addRule('passwd2', 'Please confirm password', > 'required'); > > Then I tried tinkering with addRule and registerRule in order to compare the > contents of passwd1 and passwd2 to check of they match. Hi Graeme, I post the answer to the list because it can be interesting for others. This is a three steps process: 1. create the function (directly in your script or by extending QuickForm, in which case you will use $this instead of global $form). 2. register the rule so that QuickForm knows about it. 3. add the rule to the validation rule by using addRule(). Example: function cmpPass($element, $pass1val) { global $form; $pass2val = $form->getElementValue('passwd2'); return ($pass1val == $pass2val) ? true : false; } $form->registerRule('compare', 'function', 'cmpPass'); $form->addElement('password', 'passwd1', 'Enter password'); $form->addElement('password', 'passwd2', 'Confirm password'); $form->addElement('submit', 'submit', 'submit'); $form->addRule('passwd1', 'Please enter password', 'required'); $form->addRule('passwd2', 'Please confirm password', 'required'); $form->addRule('passwd2', 'Passwords are not the same', 'compare', 'function'); $form->validate(); $form->display(); There will probably be some other ways later, I have some ideas about that. But this way will always work. BTW, thanks for the tips about writing documentation. Cheers, Bertrand Mansion Mamasam

« previous php.pear.general (#1558) next »