2014-10-30

Multi checkbox or multiselect in zend framework 1

For creating a form with multi checkboxes you can add to your form:
$possibleExchange = $this->createElement('MultiCheckbox', 'possible_exchange');
$possibleExchange->setRequired(true)->setLabel('MyTitle')->addMultiOptions($items);
then in populating the form you have to do something like that:
$populateData = $epsCurrencyItem->toArray();
$populateData['possible_exchange'] = explode(',', $populateData['possible_exchange']);
$form->populate($populateData);
this is needed for selecting checkboxes.
And when you save data to db you have to:
$data['possible_exchange'] = implode(',', $data['possible_exchange']);
$epsCurrencyItem->setFromArray($data);
$epsCurrencyItem->save();
Similarly you can use multiselect. Difference will be only when you create the form:
$possibleExchange = $this->createElement('multiselect', 'possible_exchange');
$possibleExchange->setRequired(true)
   ->setLabel('MyTitle')
   ->setAttrib('size', 10)
   ->addMultiOptions($items);

No comments:

Post a Comment