Welcome Guest, Not a member yet? Register   Sign In
Multiselect problem
#1

[eluser]Marcel Karras[/eluser]
Hello,

can someone assist me in using multiselects with CodeIgniter?
I tried to implement it the way shown in the documentation but I only get the last selected element inside the $_POST request.

My form_dropdown function:
Code:
echo form_dropdown (   'installation',
                       $installations,
                       $data['installation'],
                       'style="height:100px;" multiple="multiple"'
                    );

The $data['installation'] array element can be one or more elements so I added the multiple property in order to force the form_dropdown function to make multiple selections possible. Is there a more common way to enable the multiselect property without setting more than one default selection?

When I var_dump my $_POST array variable I can see a 'installation' index but it is not an array but a string with the selection option value of the last selected item. Any idea how to solve this issue?

Thanks in advance!
#2

[eluser]xwero[/eluser]
I guess the next line is causing your headache
Code:
$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
The function is adding a multiple attribute only when there is more than one selection and the attribute isn't found in the extra parameter.

I would change
Code:
$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';

$form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
to
Code:
$form = '<select name="'.$name.'"'.$extra.">\n";
and then you will have no more problems
#3

[eluser]Marcel Karras[/eluser]
This form_helper modification would not solve my problem as the $multiple variable will only be set to ' multiple="multiple"' if I don't set it manually using the $extra parameter. As I do set it manually there's no change to me.
#4

[eluser]xwero[/eluser]
My modification removes the $multiple line entirely. The only way to add the attribute is to put it in the extra parameter.

IMO the count of the selected options makes no sense as the user is free to choose none or one option(s). It's not up to the html generating function to decide whether it is a single or a multiple dropdown.
It would more sense if the multiple variable is selected as follows
Code:
$multiple = (strpos($extra, 'size') !== FALSE && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Most of the time if you show a select as a list it is because you can select multiple options.
#5

[eluser]Marcel Karras[/eluser]
I think the problem I have doesn't have something to do with the form_dropdown(...) function as the HTML form output is okay. Also the multiselect behavior works as expected but the POST request will only contain a string instead of an array containing all selected elements. If I change the name of the dropdown to "installation[]" I get a string with the value "Array" as POST result. So where does CodeIgniter add the selection result variable (should be an array but is a string) into the POST? Seems to me if something alters the $_POST before I get rid of it.
#6

[eluser]xwero[/eluser]
sorry i was looking at the wrong problem Smile

I guess you echoed the result, try print_r instead. The php parser treats everything as a string if you output it. false becomes 0, true becomes 1, array(1.2) becomes Array.

Your solution, adding the square brackets, is correct.
#7

[eluser]Marcel Karras[/eluser]
I finally found the problem. Smile

After having compared various print_r($_POST) dumps I recognized that the form_validation->run() function alters my $_POST['installation'] element. So I checked my validation function and saw that there was a "trim" rule set that was a result of a dirty copy&paste;. Wink

Thank you for your assistance.
#8

[eluser]markup2go[/eluser]
Care to share your results? I've been having trouble use form_dropdown as a multiple select + validation + set_select(). Can anyone be so kind as to post some example usage for this? This should be covered in the user manual in my opinion.

Ideally the select should take an array of key=>val pairs. It should be able to be validated. It should auto select options that were selected before a validation error. It should be able to set_select with an array from the database (for edit views).

Thank you much!
#9

[eluser]markup2go[/eluser]
Well after some testing I finally got it down.


In the view: (In this example $users contains array of id/usernames, $selected is $this->input->post('users'))
Code:
&lt;?=form_dropdown('users[]', $users, $selected, 'multiple="multiple"')?&gt;

In the controller:

Code:
$rules = array('users' => 'required');
$fields = array('users' => 'User Assigned To');

$this->validation->set_rules($rules);
$this->validation->set_fields($fields);

if ($this->validation->run() == FALSE)
{
    $data = array('users'=>$this->users_model->getUsersDropdown(),  'selected'=>$this->input->post('users'));
            $this->load->view('admin/companies/add', $data);
}
else
{    
// validation success
}

You will probably want to check for $_POST before you assign $selected.
#10

[eluser]bob.rosset[/eluser]
So you just added a pair of [] at the end of dropdown name! Thanks for the solution, i've been working on that a lot today!




Theme © iAndrew 2016 - Forum software by © MyBB