CodeIgniter Forums
checkbox array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: checkbox array (/showthread.php?tid=2938)



checkbox array - El Forum - 08-31-2007

[eluser]DIG[/eluser]
Forgive me for my very bad English.

I have read many of the forum on the arrays and checkbox, but bash can not solve a simple task.

In my view:
Code:
<input name="test[1]" type="checkbox" value="1">
<input name="test[2]" type="checkbox" value="1">
<?=$this->validation->test_error; ?>

In my controller:
Code:
$rules['test'] = "callback_";
$this->validation->set_rules($rules);

$fields['test']    = '"test checkbox"';
$this->validation->set_fields($fields);

function test_check()
{
  if(isset($_POST['test'])){
    $this->validation->set_message('test_error', 'Checked!');
    return TRUE;
  }else{
    $this->validation->set_message('test_error', 'Not Checked!');
    return FALSE;
    }
}
if ($this->validation->run() == FALSE){
...redisplay form...
This is the trouble place.
no message displayed.

}else{
...insert in db, redirect...
}

where I am wrong? or I am hopelessly stupid? Smile
Thanks.


checkbox array - El Forum - 08-31-2007

[eluser]alpar[/eluser]
Code:
$rules['test'] = "callback_";


should be

Code:
$rules['test'] = "callback_test_check";



checkbox array - El Forum - 08-31-2007

[eluser]DIG[/eluser]
Thanks for the quick response.
Really in my code
Code:
$rules['test'] = "callback_test_check";
it's my copy-paste error.


checkbox array - El Forum - 08-31-2007

[eluser]alpar[/eluser]
try using only test[] in the field name, it might not get merged to an array...

like so:

Code:
<input name="test[]" type="checkbox" value="1">
  <input name="test[]" type="checkbox" value="1">



checkbox array - El Forum - 08-31-2007

[eluser]DIG[/eluser]
Variations
Code:
<input name="test[]" type="checkbox" value="1">
<input name="test[]" type="checkbox" value="2">
and
Code:
<input name="test" type="checkbox" value="1">
<input name="test" type="checkbox" value="2">
and
Code:
<input name="test[1]" type="checkbox" value="1">
<input name="test[2]" type="checkbox" value="2">
not working...

I wrote testing code.
This controller:
Code:
<?php
class test extends controller {

    function index(){
        $this->load->library('validation');

        $rules['test'] = "callback_";
        $this->validation->set_rules($rules);

        $fields['test']    = '"test checkbox"';
        $this->validation->set_fields($fields);

        function test_check()
        {
          if(isset($_POST['test'])){
            $this->validation->set_message('test_error', 'Checked!');
            return TRUE;
          }else{
            $this->validation->set_message('test_error', 'Not Checked!');
            return FALSE;
            }
        }
        if ($this->validation->run() == FALSE){
            $this->load->view('test');
        }else{
            $this->load->view('test');
        }
    }

}
?>
This is view:
Code:
<html>
<body>
<form name="" action="<?=site_url();?>test" method="post">
<input name="test[]" type="checkbox" value="1">
<input name="test[]" type="checkbox" value="2">
<?=$this->validation->test_error; ?>
<input type="submit" value="Send">
</form>
</body>
</html>

This is not working. This should be easy, but it is not working. :/


checkbox array - El Forum - 08-31-2007

[eluser]alpar[/eluser]
try adding the callback function as a method of the controller.


checkbox array - El Forum - 08-31-2007

[eluser]Unknown[/eluser]
I started doing something similar today and ran into a problem,
To get the validation library to see the fields do this:
Code:
$this->load->library('validation');
foreach($_POST['field'] as $k => $v)
{
    $rules["field[$k]"] = "required";
    $fields["field[$k]"] = "Field #".$i++;
}
$this->validation->set_rules($rules);
$this->validation->set_fields($fields);
$this->validation->set_message('required', "%s is required");

The only problem now is even if there is a value in the field, it gets ignored and the set message does not work either...

Also, the set_fields and set_message is not required


checkbox array - El Forum - 08-31-2007

[eluser]alpar[/eluser]
there is some related bug with that, if i recall correctly, make a search on it


checkbox array - El Forum - 08-31-2007

[eluser]DIG[/eluser]
alpar, thanks for the help.

I rewrite test controller:
Code:
<?php
class test extends controller {

    function index(){
        $this->load->library('validation');

        $rules['test'] = "callback_test_check";
        $this->validation->set_rules($rules);

        $fields['test']    = '"test checkbox"';
        $this->validation->set_fields($fields);

        if ($this->validation->run() == FALSE){
            $this->load->view('test');
        }else{
            $this->load->view('test');
        }
    }

    function test_check()
    {
      if(isset($_POST['test'])){
        $this->validation->set_message('test_error', 'Checked!');
        return TRUE;
      }else{
        $this->validation->set_message('test_error', 'Not Checked!');
        return FALSE;
        }
    }
}
?>
And try again name="test[]", name="test" and name="test[1]", but bash is not working...
I am at a loss.


checkbox array - El Forum - 09-01-2007

[eluser]alpar[/eluser]
just got an idea... don't use a callback...use 'required' that comes with the framework...i think it works exactly as you would like it here, and also note, there is an 'isset' error in the language files that gets triggered when the field isn't set (like an unchecked check box), so you could just change the message for isset....

--- Somebody correct me if I'm wrong... I'm kind of busy and don't have the time to test what i am saying, nor run your code sry