Welcome Guest, Not a member yet? Register   Sign In
Validation, re-populating array checkbox
#1

[eluser]Kopel[/eluser]
Hi,

I need to re-populate my checkbox when my form has been submitted with errors.
Everything is fine with regular checkbox, but my checkbox has to be an array (name="mycheck[]").

See below my simplified code as an example.

- In order to have an error i leave the input blank
- I check some boxes
- I submit the form
- The error on the input is found, the controller reload the form view
- The checkboxes are unchecked...

I tried all day long to find the solution of this apparently simple problem, but i'm still unsuccessful.
Please, let me know what is wrong with my code.
Thanks.

View file:
Code:
<?php echo form_open('sandbox'); ?>

&lt;input type="text" name="username" value="&lt;?php echo $this-&gt;validation->username; ?&gt;" size="50" /><br />
&lt;?php echo $this->validation->error_string; ?&gt;

&lt;input type="checkbox" name="mycheck[]" value="1" &lt;?php echo $this-&gt;validation->set_checkbox('mycheck', '1'); ?&gt; /><br />
&lt;input type="checkbox" name="mycheck[]" value="2" &lt;?php echo $this-&gt;validation->set_checkbox('mycheck', '2'); ?&gt; /><br />
&lt;input type="checkbox" name="mycheck[]" value="3" &lt;?php echo $this-&gt;validation->set_checkbox('mycheck', '3'); ?&gt; /><br />

&lt;input type="submit" value="Submit" /&gt;

&lt;/form&gt;

Controller file
Code:
&lt;?php

class Sandbox extends Controller {

    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('validation');
            
        $rules['username'] = "required";
        $rules['mycheck']  = "";
    
        $this->validation->set_rules($rules);
            
      
        $fields['username'] = "user name";
        $fields['mycheck']  = "my check";
    
        $this->validation->set_fields($fields);
            
      
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('sandbox');
        }
        else
        {
          echo "success";
          print_r($_POST['mycheck']);
        }
    }
}
?&gt;
#2

[eluser]Référencement Google[/eluser]
Unfortunaly and we hope that this will come in a next version of CI, it doesn't validate Checkboxes or Radio like other fieds type, so &lt;?php echo $this->validation->error_string; ?&gt; will return nothing.

Make a test on $_POST datas should resolve your problem. This could be simply:

Code:
if(!$this->input->post('mycheck'))
{
    // Returned false, then send an error message to the view
}
#3

[eluser]Kopel[/eluser]
Thanks for the reply elitemedia.

Well, the error string is not my problem.
In fact, the user can check/uncheck all or just some of these checkboxes , so they do not generate errors...

My concern was about the re-populating of the array checkbox.
Especially when the validation failed because of the others widget (like with the input text Username on my example).

I lost too much time on this crazy thing today.
Finally i did something not as clean as i would but now it works.
Anyway, if someone have THE solution, i'm still interested...

Thx Smile
#4

[eluser]Référencement Google[/eluser]
Ok, understood.
Look at this one:

[url="http://ellislab.com/forums/viewthread/51260/"]http://ellislab.com/forums/viewthread/51260/[/url]

And read all the thread, there are bug fixes along it.
#5

[eluser]gkchicago[/eluser]
Old thread I know, but I have posted my solution to this problem here:

http://ellislab.com/forums/viewthread/79792/
#6

[eluser]vadivelan[/eluser]
Hi

Validation of listbox, checkbox groups and reselect multiple values using set_select, set_checkbox / Sol: Array to string conversion error (line 709)

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

[eluser]Nexus Rex[/eluser]
I believe all that was missed in the example in the initial post were the square brackets in the form object name within the set_checkbox() function.

The proper name of the checkbox object is "mycheck[]" and NOT "mycheck"

Code:
&lt;?php echo form_open('sandbox'); ?&gt;

&lt;input type="text" name="username" value="&lt;?php echo $this-&gt;validation->username; ?&gt;" size="50" /><br />
&lt;?php echo $this->validation->error_string; ?&gt;

&lt;input type="checkbox" name="mycheck[]" value="1" &lt;?php echo $this-&gt;validation->set_checkbox('mycheck[]', '1'); ?&gt; /><br />
&lt;input type="checkbox" name="mycheck[]" value="2" &lt;?php echo $this-&gt;validation->set_checkbox('mycheck[]', '2'); ?&gt; /><br />
&lt;input type="checkbox" name="mycheck[]" value="3" &lt;?php echo $this-&gt;validation->set_checkbox('mycheck[]', '3'); ?&gt; /><br />

&lt;input type="submit" value="Submit" /&gt;

&lt;/form&gt;
#8

[eluser]Unknown[/eluser]
ok, so I pulled some hair trying to make this work and here is what I come up with as the quickest solution:
(in my view)
Code:
$checkedLangs = $this->input->post('language');
echo $this->layout->formItem('open');
echo form_label('Additional Active Languages', '', $labelAtt);
foreach($site_config['langs'] as $lang){
  if(!$checkedLangs){  
   if($lang['active'] =='y'){ $checked = TRUE; } else { $checked = FALSE;}
  }
  else{
   if(in_array($lang['id'], $checkedLangs)){
    $checked = TRUE;
   }
   else{
    $checked=FALSE;
   }
  }
  $fd = array(
       'name'        => 'language[]',
       'value'       => $lang['id'],
       'title'       => $lang['name'],
       'checked'     =>  $checked
      );
  echo '<div class="langSelect" title="'.$lang['name'].'">'.form_checkbox($fd).'<img src="'.base_url().'images/flags24/'.$lang['fileName'].'"/></div>';
}
echo $this->layout->formItem('close');

then from validation:

Code:
$this->form_validation->set_rules('language', 'Language', 'less_than[9999]');

I now its not your example code and i have my own unrelated functions but you should be able to figure it out.

- seems like a bit much but you get what you get with someone else's framework i guess




Theme © iAndrew 2016 - Forum software by © MyBB