Multi-select does not save value after post in codeigniter

I have a form in my application with multiple select. I am using the CI form helper to create my forms, so the item assembly looks like this:

return form_multiselect('authors[response][]', $faculty->get_all_for_multiselect(), 
                         $pre_selected, $additional_attributes);

      

It is good and good if the items are in the database ($ pre_selected gets existing responses). However, I also run the form using CI form validation, and when that happens, if validation fails, then the multi-select loses the values ​​that were selected.

I'm sure this is something simple that I'm just looking at, so hopefully someone can help me here.

Adding additional information

The field is marked as needed, so it goes through the validator (although it will always go through when I automatically select the current user).

+3


source to share


1 answer


(I'm assuming $ pre_selected is an array of values?)

You can reset the selected values ​​after a failed form submission using the $ _POST array.



Since you are already using $ pre_selected, you should be able to use the following:

return form_multiselect('authors[response][]', $faculty->get_all_for_multiselect(), 
                         array_unique(array_merge($pre_selected, $_POST['response'])), $additional_attributes);

      

+1


source







All Articles