title: Codeigniter checkbox array
body: <p>My checkboxes look like this</p>
  
  <pre><code>&lt;div&gt;
      &lt;input type="checkbox" name="fruits[]" value="apple" &lt;?php echo set_checkbox('fruits[]', 'apple'); ?&gt; /&gt;
      &lt;input type="checkbox" name="fruits[]" value="banana" &lt;?php echo set_checkbox('fruits[]', 'banana'); ?&gt; /&gt;
      &lt;input type="checkbox" name="fruits[]" value="peach" &lt;?php echo set_checkbox('fruits[]', 'peach'); ?&gt; /&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I am using array in the names so that I can populate a text field (via jquery) with the values of the checkboxes I select. I am using a script found on stackoverflow.</p>
  
  <pre><code>function calculate() {
  
          var fruit = $.map($('input:checkbox:checked'), function(e, i) {
              return e.value;
          });
  
          $('#fruits').val(fruit.join(',')); //adds values separated by comma to #fruits input 
      }
  
      calculate();
  
      $('div').delegate('input:checkbox', 'click', calculate);
  </code></pre>
  
  <p>So far it works but I'm having trouble setting validation rules for the checkboxes because they all have the same name. I tried..</p>
  
  <pre><code>$this-&gt;form_validation-&gt;set_rules('fruits[]', 'Apple', 'required|xss_clean');
  $this-&gt;form_validation-&gt;set_rules('fruits[]', 'Banana', 'required|xss_clean');
  $this-&gt;form_validation-&gt;set_rules('fruits[]', 'Peach', 'required|xss_clean');
  </code></pre>
  
  <p>and used <code>echo form_error('fruits[]');</code> to display validation error but nothing happens. Ultimately I want to display a checkbox in the state it was submitted, should an error occur on another field in my form. Instead all the checkboxes uncheck. I'm not sure how to fix this.</p>
  
  <p>POST</p>
  
  <pre><code>Array ( [FRUITS] =&gt; Array ( [0] =&gt; apple [1] =&gt; banana [2] =&gt; peach )
  </code></pre>
  
  <p>this is when they are checked</p>