HTML_QuickForm2 help loading checkbox group defaults from an array

From: Date: Fri, 06 Apr 2012 12:39:52 +0000
Subject: HTML_QuickForm2 help loading checkbox group defaults from an array
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-31496@lists.php.net to get a copy of this message
Hi, 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);
} 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),
           ...
); 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". Can anyone give me any clues as to the format of the array i should be passing to $form->addDataSource(new HTML_QuickForm2_DataSource_Array()) in order to achieve this? Or perhaps there is a better way to do the grouping? Many thanks, James Tandy

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