Fill in the zend multitasking field when editing a form

I have a multi-sector block of (interests) in my zend form, and I select several parameters from it while I add the user.

Now, when I edit this user, I need to set some default options (which I selected when adding the user).

How can i achieve this? I used populate (Array) in my controller update, but this does not work.

This is the code of the multi-position window in the form of adding / editing a user:

$interests = new Zend_Form_Element_Multiselect('interest'); $days->setLabel('Interests') ->addMultiOptions($user_interests) ->setRequired(true) ->addValidator('NotEmpty'); 

And when adding the "interest" options in the form of an array of $user_interests :

 array(1=>'Blogging', 2=>'Swimming', 3=>'Cricket', 4=>'Yoga') 

I selected the first 2 questions when adding the user.

Now when editing, I get user data from the database query. This data array is used to fill out the form, and this array structure is as follows:

 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 of โ€œBlogsโ€ and โ€œSwimmingโ€ should be selected in my editing form. But I see that only the "Swimming" option is selected.

+4
source share
3 answers

Well, I could achieve a solution using jquery and javascript.

First, in my controller, I requested a database and got all rights to the edit user ID. Then I assigned this array to the appropriate form:

 $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 the jquery document ready function, I converted the assigned php array to a javascript array, and then went through that array to mark the selected options from all the parameters:

 <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> 
0
source
 $interest_data = $users->getUserInterests($id); foreach($interest_data as $r) { $interests[$r['interest_id']] = $r['interest']; } $this->view->interests_selected = $interests; 

After that we can have it in the controller

 $form->interest_id->setValue($interests); 

Hope this helps someone

+6
source

The best way to check in this situation is to submit the form with the selected elements, unload the $form->getValues() array and compare it with what you are trying to fill out. If this is the same then you may have a different error in the code.

+1
source

All Articles