Re: HTML_QuickForm2 help loading checkbox group defaults from an array

From: Date: Fri, 06 Apr 2012 15:25:52 +0000
Subject: Re: HTML_QuickForm2 help loading checkbox group defaults from an array
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-31497@lists.php.net to get a copy of this message
Hi James, On 06.04.2012 16:39, James Tandy wrote:
I am having problems trying to work out how to load form defaults on the following form. I have an object which holds my database data. Among the options are a series of arrays, eg $object->roles = array(0=>'Tank',1=>'Healer',2=>'Melee DPS',3=>'Ranged DPS',50=>'All'); $object>rolevalues = array(0=>true,1=>false,2=>true,3=>true,50=>false); $roles = $form->addElement('group','roles')->setLabel('Applicable Roles'); foreach($object->roles as $id=>$name){ $roles->addElement('checkbox',$id,array('value'=>$id))->setLabel($name); }
You'd basically want to give your checkboxes either unique 'name' attributes <input type="checkbox" value="1" name="roles[0]" id="0-0" checked="checked" /> <input type="checkbox" value="1" name="roles[1]" id="1-0" checked="checked" /> or unique 'value' attributes <input type="checkbox" value="0" name="roles[]" id="0-0" checked="checked" /> <input type="checkbox" value="1" name="roles[]" id="1-0" checked="checked" /> You end up giving both, thus the problem. The former is achieved by // value="1" will be set automatically $roles->addElement('checkbox',$id) the latter by $roles->addElement('checkbox', '', array('value' => $id))
So far all works fine, i get an array of checkboxes on the page, and i can submit, validate, etc. The problem comes when i try to load the default values for the checkboxes. $form->addDataSource(new HTML_QuickForm2_DataSource_Array($object->getFormDefaults())); I am stuggling to work out the format required for the array. Currently $object->getFormDefaults() returns an array with the following format: array( 'roles'=> array(0=>true, 1=>false, 2=>true, 3=>true, 50=>false), ... );
This array will work with "unique names" approach above. For "unique values" approach you'll need array(0, 2, 3) You'll be able to set the values even now, but the array will be a bit redundant: array(0=>0, 2=>2, 3=>3)
When my form is rendered however, i get the following input elements on the form: (Note i have stripped most html) <fieldset id="roles-0"> <legend id="roles-0-legend">Applicable Roles</legend> <label for="0-0">Tank</label> <input type="checkbox" value="1" name="roles[0]" id="0-0" checked="checked" /> <label for="1-0">Healer</label> <input type="checkbox" value="1" name="roles[1]" id="1-0" checked="checked" /> <label for="2-0">Melee DPS</label> <input type="checkbox" value="2" name="roles[2]" id="2-0" /> <label for="3-0">Ranged DPS</label> <input type="checkbox" value="3" name="roles[3]" id="3-0" /> <label for="50-0">All</label> <input type="checkbox" value="50" name="roles[50]" id="50-0" /> </fieldset> What I am expecting, is roles[0], roles[2] and roles[3] checked, and the rest unchecked. Also note the roles[0] input has value="1" when i am expecting value="0".
Looks like you've hit a bug here, indeed. It can be worked around, I think, by doing an explicit setAttribute call instead of passing value to addElement(): $roles->addElement('checkbox',$id)->setAttribute('value', $id); Another bug is that autogenerated ids like "0-0" and "1-0" are definitely invalid in HTML4 and XHTML...

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