Reputation: 238667
Just wondering how it works and how to handle the information.
Let's say I have a form like this:
$multi = new Zend_Form_Element_Multiselect('users');
$multi->setMultiOptions(array(
//'option value' => 'option label'
'21' => 'John Doe',
'22' => 'Joe Schmoe',
'23' => 'Foobar Bazbat'
));
$form->addElement($multi);
If a user selects one, or multiple values from a multi-select box...
Upvotes: 6
Views: 18140
Reputation: 1429
use this to handle multi-select boxes in a zend framework form bro :
$multi->setAttrib('multiple', 'multiple');
so it will be like this in your own code:
$multi = new Zend_Form_Element_Multiselect('users');
$multi->setAttrib('multiple', 'multiple');
$multi->setMultiOptions(array(
//'option value' => 'option label'
'21' => 'John Doe',
'22' => 'Joe Schmoe',
'23' => 'Foobar Bazbat'
));
$form->addElement($multi);
Upvotes: 0
Reputation: 1259
It may be helpful to others: I found on Zend Framework 1.12 that if you don't pass the multi element a name ending in [] it throws an error in Zend Form.
E.g.
$this->addElement('multiselect', 'somename'); // throws error
while:
$this->addElement('multiselect', 'somename[]'); // works
Upvotes: 0
Reputation: 315
Also one remark, maybe useful for someone here (I spent some time to get it):
If you are creating your own multi checkbox element, you must extend Zend_Form_Element_MultiCheckbox
, because validation does not work correctly, when you are extending just Zend_Form_Element_Multi
.
Upvotes: 0
Reputation: 1520
See the answer from brad. The especial part is the name of the element
$multi = $form->createElement('multiselect', 'name[]');
if you call the element with squares it will be handled as an array by the browser (not a zf behavior). Otherwise you'll get only the first selected element
Upvotes: 1
Reputation: 238667
Using a multi select element like this one:
$multi = new Zend_Form_Element_Multiselect('users');
$multi->setMultiOptions(array(
//'option value' => 'option label'
'21' => 'John Doe',
'22' => 'Joe Schmoe',
'23' => 'Foobar Bazbat'
));
$form->addElement($multi);
You can get the values of the element like this:
public function indexAction()
{
$form = new MyForm();
$request = $this->getRequest();
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
$values = $form->getValues();
$users = $values['users']; //'users' is the element name
var_dump $users;
}
}
$this->view->form = $form;
}
$users
will contain an array of the values that have been selected:
array(
0 => '21',
1 => '23'
)
Upvotes: 11
Reputation: 5488
$form->getElement('name')->getValue()
will return the value of $_POST['name']. You can make
$_POST['name']
be an array by defining the name of the element with brackets at the end. So in this case, 'name[]'. In the Zend Framework, use an element that extends
Zend_Form_Element_Multi
For example:
$multi = $form->createElement('multiselect', 'name[]');
$multi->setMultiOptions($options);
$form->addElement($multi);
if ($form->isValid($_POST)) {
$userSelectedOptions = $form->getElement('name')->getValue();
}
Upvotes: 3