Re: Problem with quickform checkbox

From: Date: Tue, 22 May 2007 14:02:52 +0000
Subject: Re: Problem with quickform checkbox
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-27348@lists.php.net to get a copy of this message
Eugen Stoianovici wrote:
I have a form with a few checkboxes used for updating some entrys. The problem is that if i use checkbox::setChecked(true), the checkbox remains checked even if the user unchecks it here's the code: $form = new HTML_QuickForm('events','POST',$_SERVER[REQUEST_URI],NULL,true); $form->addElement('text', 'ca', 'CA:'); $rmodem =& $form->addElement('checkbox', 'rmodem', 'Reset modem:'); $rsoft =& $form->addElement('checkbox', 'rsoft', 'Reset soft:'); $rhard =& $form->addElement('checkbox', 'rhard', 'Reset hard:'); $form->addElement('text', 'phone, 'phone:'); if($def["rhard"]>0) $rhard->setChecked(true); if($def["rmodem"]>0) $rmodem->setChecked(true); if($def["rsoft"]>0) $rsoft->setChecked(true); if($form->validate()){ $form->freeze(); savedata($form); } echo $form->toHtml(); If rhard is checked in the inital display and the user unchecks it, form->freeze still sees it checked what am i doing wrong?
Normally you would set the *defaults* of the form so that if the user changes the form, the defaults are overridden. The setChecked() method ensures that the box is checked. Depending on the defaults and user input, setChecked is called with the appropriate value. Try this: $form->setDefaults(array(
    'rmodem' => ($def['rmodem'] > 0),
    'rsoft'  => ($def['rsoft'] > 0),
    'rhard'  => ($def['rhard'] > 0),
    ));
-- David Sanders shangxiao

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