Welcome Guest, Not a member yet? Register   Sign In
Re-populating Array Check Boxes after Validation Failure
#1

[eluser]gkchicago[/eluser]
I had trouble getting my array checkboxes to re-populate after a failed validation and have gotten it to work how I wanted without modifying the validation or input libraries so I thought I would share it here. I appreciate any input or opinions to help clean this up before I possibly make it a wiki entry.

Important Note: I am not doing any validation on the checkbox itself, the checkboxes for my needs are considered optional in the context of the form.

Methodology:

A simple in_array check is used in the view to set the 'checked' (TRUE/FALSE) flag that is used by the form_checkbox function when outputting the checkboxes. Because my checkboxes have numeric ids a loop is used to keep the code relatively clean but is not required. The controller must pass the POST-ed array data back to the view on validation failure.

View:
Code:
# Checkbox ids, probably loaded from a database
$checkboxes = array('1','2','7');
foreach ( $checkboxes as $checkbox )
{
    if ( isset($active_checkboxes) && in_array($checkbox, $active_checkboxes) ) $checkbox_status = TRUE; else $checkbox_status = FALSE;
    echo '<p>'.'Item '.$checkbox.' '.form_checkbox($data = array('name' => 'active_checkboxes[]', 'id' => 'active_checkbox'.$checkbox, 'value' => $checkbox, 'checked' => $checkbox_status)).'</p>';
}

active_checkboxes is set in the controller on validation failure.

Controller:
Code:
function add()
{
    $this->load->library('validation');
    
    $rules['somefield'] = "required";    
    $this->validation->set_rules($rules);

    $fields['somefield'] = "Some Field";
    $this->validation->set_fields($fields);
  
    if ($this->validation->run() == FALSE)
    {
        $data = array();
        if ( $this->input->post('active_checkboxes') ) $data['active_checkboxes'] = $this->input->post('active_checkboxes');
        $this->load->view('sandbox');
    }
    else
    {
        echo "success";
        print_r($_POST['active_checkboxes']);
    }
}

Because the checkboxes are outside the validation in the controller we need to check the post variables and set active_checkboxes for the reloaded view.
#2

[eluser]vadivelan[/eluser]
Hi
CI not dealing properly with user submitted array values through forms. To deal with that have a look at the below thread:

http://ellislab.com/forums/viewthread/73012/
#3

[eluser]gkchicago[/eluser]
[quote author="vadivelan" date="1211106793"]Hi
CI not dealing properly with user submitted array values through forms. To deal with that have a look at the below thread:

http://ellislab.com/forums/viewthread/73012/[/quote]

Hi vadivelan, I had run across your post while I was looking for the solution to this problem but I resisted using it because I didn't want to extend the code igniter validation library. But it looks like they are looking for something like your code for bug http://codeigniter.com/bug_tracker/bug/4530/




Theme © iAndrew 2016 - Forum software by © MyBB