Welcome Guest, Not a member yet? Register   Sign In
Checkbox arrays, validation and repopulating: my solution
#1

[eluser]nuwanda[/eluser]
I struggled with this, and it seems many others have done. I thought I'd post some test code that I finally got to work as intended.

It's a color selection form containing a text input and two checkboxes. The checkboxes are part of a checkbox array, both with the name 'colors'. That means they must be referred to with the name 'colors[]'.

It's the set_checkbox() method that caused me the real problems. By looking at the following code you'll be able to see how it works to repopulate the form values upon validation failure.

First the color selection form.

Important to note is the form_checkbox() function that outputs the checkbox input. The first argument is the $data array which I've used to set some parameters including the default value. The second param is blank '' because that's the value param and it's already been set in the $data array. The third param is the set_checkbox() method of the form_validation class. It takes the name of the input (colors[]) and the value of the input which was set in the $data array.

You'll also notice for the color green there's a fourth param which is the preferred default state of the checkbox. In this example I've chosen to have the green checkbox ticked on form load. If you should untick it and submit the form, and the form reloads due to errors, it will remain unticked because that's the state you submitted it in.

Code:
<h2>Color Selection</h2>

&lt;?php
echo validation_errors();

echo form_open('colors/list_colors');

  $data = array(
      'name'        => 'name',
      'id'          => 'name_input',
      'value'       => 'Name',
      );
    
    echo '<p>';
  echo form_label('Name', 'name_input');
    echo form_input($data);
    echo '</p>';

  $data = array(
      'name'        => 'colors[]',
      'id'          => 'color_green',
      'value'       => 'green'
      );

    echo '<p>';
    echo form_checkbox($data,'',set_checkbox('colors[]','green',TRUE));
  echo form_label('Green', 'color_green');
    echo '</p>';

  $data = array(
      'name'        => 'colors[]',
      'id'          => 'color_blue',
      'value'       => 'blue'
      );
            
    echo '<p>';
    echo form_checkbox($data,'',set_checkbox('colors[]','blue'));
  echo form_label('Blue', 'color_blue');
    echo '</p>';    
    
    echo '<p>';
  echo form_submit('submit','List Colors');
    echo '</p>';
    
echo form_close();

Here's the controller.

Note the $this->form_validation->set_rules('colors[]','error','')

That sets the rules for the checkbox array. The third param is empty because in this case I don't care if the colors are checked. You could set 'required' which requires at least one box in the array to be checked.

Code:
class Colors extends Controller{

  var $data=array();
    
  function __construct(){
    parent::Controller();
      $this->load->library('form_validation');        
  }
  
  function index(){
    $this->display_color_form();
  }
    
    function display_color_form()
    {
      $this->data['content']='color_form';
    $this->load->view('template',$this->data);
    }
    
    function list_colors()
    {

        //field,error message,rules
        $this->form_validation->set_rules('name','Name Required','required');        
        $this->form_validation->set_rules('colors[]','error','');
        
        if($this->form_validation->run()==FALSE)
        {
          $this->display_color_form();
        }
        else
        {
          //show color checkbox array
          echo '<pre>';
            print_r($this->input->post('colors'));
            echo '</pre>';
          //do other successful stuff here...
        }
                                        
    }

}

I hope this helps others.




Theme © iAndrew 2016 - Forum software by © MyBB