Welcome Guest, Not a member yet? Register   Sign In
having trouble with set_select() with multi-dimensional array form elements

Hi there, wondering whether anyone could point me in the right direction... I am trying to use the form_validation class for a form that has some select boxes and some input boxes. The fields are named as arrays because the user can add more fields using a jquery script that adds a new line of fields to the form when user clicks add button. However if the post doesn't pass validation checks, i want the user to be returned to the form with the errors highlighted and the values in place with the correct value selected in the select box. Here is my view code (thats not working)

for($i = 1; $i <= 5; $i++) {
    echo '<div class="hamperRow" id="row'.$i.'">';
    echo form_error('hamper_items['.$i.'][quantity]');
    echo '<div class="big_item_cell">';
    echo '<select name="hamper_items['.$i.'][items]">';
    foreach ($items as $k => $v) {
        $sel = 'hamper_items['.$i.'][items]';
        if ($k == '') {
            echo '<option value="'.$k.'" '.set_select($sel, $k, TRUE).' >'.$v.'</option>';
        } else {
            echo '<option value="'.$k.'" '.set_select($sel, $k).' >'.$v.'</option>';
    echo '</select>';
    echo '</div>';
    echo '<div class="small_item_cell">'.form_input(array('size' => '5', 'name' => 'hamper_items['.$i.'][quantity]')).'</div>';
    echo '<div class="small_item_cell">'.form_dropdown('hamper_items['.$i.'][units]',$units).'</div>';
    echo '<div class="clear"></div></div>';

The for loop is because when the page initially loads, i want the user to have 5 rows of fields (then after that when clicking a button (code not included) a new row gets added using jquery's clone function.

My controller code is as follows:
if($this->dx_auth->is_logged_in()) {
            if($_POST) {
                foreach ($this->input->post('hamper_items') as $k => $v) {
                    if ($v['items']) {
            if ($this->form_validation->run() == TRUE) {
            $data['title'] = 'Add Hamper';
            $data['items'] = $this->Items_model->get_items('dropdown_array');
            $data['units'] = $this->Units_model->get_units();



The print_r($_POST) is just for my reference really (you can see i havent finished the function off yet).

Can anyone see why the select boxes aren't being selected correctly when the form fails validation? the error messages come up fine...



Have tried changing the for loop to a while but still no luck,

Is there a limitation to the set_select with multi-dimentional arrays?




I worked it out on my own, I was missing a validation rule in the controller... Which means set_select will not work unless a validation rule is passed in the controller. The rule I put in was:

Then it all worked... I guess for all fields where you want to set_value or set_select or set_checkbox but don't necessarily want to add a validation rule, you must add the rule but fill it with something general like 'alphanumeric', 'trim' or 'xss_clean'.

Theme © iAndrew 2016 - Forum software by © MyBB