Welcome Guest, Not a member yet? Register   Sign In
checkbox array
#1

[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.
#2

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


should be

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

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

[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">
#5

[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. :/
#6

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

[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
#8

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

[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.
#10

[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




Theme © iAndrew 2016 - Forum software by © MyBB