Populate a zend multiselect box while editing form
i have a multiselect box (of interests) in my zend form, and i am selecting multiple options from it while i am adding a user.
Now when i am editing this user, i need to set some options selected by default, (which i selected while adding the user).
How can i achieve this? I used populate(Array) in my controller's update action, but that does not work.
This is the code of multi select box in the user add/edit form:
$interests = new Zend_Form_Element_Multiselect('interest');
$days->setLabel('Interests')
->addMultiOptions($user_interests)
->setRequired(true)
->addValidator('NotEmpty');
And while adding the "interest" options in the form, $user_interests
array is:
array(1=>'Blogging', 2=>'Swimming', 3=>'Cricket', 4=>'Yoga')
I have selected the first 2 interests while adding the user.
Now while editing, i get the user data from a database query. This data array is used to populat开发者_StackOverflow社区e the form, and this array structure is like this:
Array (
[username] => john
[user_dob] => 1981-03-12
[email] => john@gmail.com
[interest] => Array ( [0] => 1 [1] => 2 )
)
and as you can see, the interests "Blogging" and "Swimming" are supposed to be selected in my edit form. But i see that only the "Swimming" option is getting selected.
$interest_data = $users->getUserInterests($id);
foreach($interest_data as $r)
{
$interests[$r['interest_id']] = $r['interest'];
}
$this->view->interests_selected = $interests;
After this we can have like this in the controller
$form->interest_id->setValue($interests);
Hope this helps someone
Best way to test in this situation is to submit the form with elements selected, dump the $form->getValues()
array and compare it to what you're trying to populate. If it's the same, then you may have other mistake in your code.
Well, I could achieve a solution with the help of jquery and javascript.
First in my controller, i queried the database and got all the interests of the editing user id. Then i assigned this array to the corresponding view:
$interest_data = $users->getUserInterests($id);
foreach($interest_data as $r)
{
$interests[$r['interest_id']] = $r['interest'];
}
$this->view->interests_selected = $interests;
Now in the view, at the top of the file, inside jquery's document ready function, i converted the assigned php array into a javascript array and then looped through this array to mark the selected options from all the options:
<script>
$(document).ready(function() {
<?php
// array of assigned interests to user
$arr = $this->interests_selected;
?>
// convert from php to js array
var jsArray = ["<?php echo join("\", \"", $arr); ?>"];
var optionsToSelect = jsArray;
var select = document.getElementById( 'interest_id' ); // interests dropdown
// loop through all the select box options and mark those in the jsArray as selected
for ( var i = 0, l = select.options.length, o; i < l; i++ )
{
o = select.options[i];
if ( optionsToSelect.indexOf( o.text ) != -1 )
{
o.selected = true;
}
}
});
</script>
精彩评论