Welcome Guest, Not a member yet? Register   Sign In
set_value() in a form_multiselect()
#1

[eluser]jamesf[/eluser]
Using version 2.0.2 when trying to re-populate a multiselect form I only ever get one value returned.

Looking at the Form_validation.php class this is the default action for any value that's an array:

Code:
// If the data is an array output them one at a time.
//     E.g: form_input('name[]', set_value('name[]');
if (is_array($this->_field_data[$field]['postdata']))
{
    return array_shift($this->_field_data[$field]['postdata']);
}

Surely this is a bug as currently there's no way to use this to re-populate a multiselect form input.

For example this is being returned:
Code:
1

Instead of this:
Code:
Array
(
    [0] => 1
    [1] => 2
)

Any reason why this is the case?
#2

[eluser]Aken[/eluser]
First, set_value() is not meant to use with drop downs.

Second, most of the set_value()-type functions are meant to use specifically with each individual value. They're not meant to handle arrays of selected elements.

It's true that there should be some additional functionality specifically meant for generating and handling multiselect elements (drop downs, check boxes, etc).

In the meantime, this little hack works:
Code:
form_multiselect('items[]', $options_array, $this->input->post('items'));
Though probably not the safest method, it worked fine in a quick test. I'd recommend purposefully trying to break that to find any flaws, and compensating for them.

In the meantime, maybe file a Feature Request on the Bit Bucket CI Reactor page: https://bitbucket.org/ellislab/codeignit...tor/issues
#3

[eluser]jamesf[/eluser]
Thanks Aken. Much appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB