EllisLab text mark
Advanced Search
     
checkbox array
Posted: 31 August 2007 03:51 PM   [ Ignore ]
Joined: 2007-08-31
5 posts

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:

<input name="test[1]" type="checkbox" value="1">
<
input name="test[2]" type="checkbox" value="1">
<?=$this->validation->test_error?> 

In my controller:

$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 dbredirect...

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

 
Posted: 31 August 2007 04:24 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-07-17
122 posts
$rules['test'"callback_"

should be

$rules['test'"callback_test_check"
 Signature 

Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination.

                                    A. Einstein

 
Posted: 31 August 2007 04:33 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2007-08-31
5 posts

Thanks for the quick response.
Really in my code

$rules['test'"callback_test_check"

it’s my copy-paste error.

 
Posted: 31 August 2007 04:52 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2007-07-17
122 posts

try using only test[] in the field name, it might not get merged to an array…

like so:

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

Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination.

                                    A. Einstein

 
Posted: 31 August 2007 05:32 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2007-08-31
5 posts

Variations

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

and

<input name="test" type="checkbox" value="1">
<
input name="test" type="checkbox" value="2"

and

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

not working…

I wrote testing code.
This controller:

<?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:

<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. :/

 
Posted: 31 August 2007 07:19 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2007-07-17
122 posts

try adding the callback function as a method of the controller.

 Signature 

Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination.

                                    A. Einstein

 
Posted: 31 August 2007 07:21 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2007-08-31
1 posts

I started doing something similar today and ran into a problem,
To get the validation library to see the fields do this:

$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

 
Posted: 31 August 2007 07:38 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2007-07-17
122 posts

there is some related bug with that, if i recall correctly, make a search on it

 Signature 

Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination.

                                    A. Einstein

 
Posted: 01 September 2007 01:49 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2007-08-31
5 posts

alpar, thanks for the help.

I rewrite test controller:

<?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.

 
Posted: 01 September 2007 07:38 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2007-07-17
122 posts

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

 Signature 

Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination.

                                    A. Einstein

 
Posted: 01 September 2007 09:31 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2007-08-31
5 posts

alpar
Thank you for your attention to me.
I tried use ‘required’, but this is not working.
I decide this issue a few hidden fields and js, it’s bad solution, but I have no more energy and time for this small problem.

 
Posted: 06 September 2007 04:34 PM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2006-04-20
712 posts

I’ve got the same issue, if somebody find the real solution on that, please write it here.
Also I think a simple rule like for other type of input (required rule) should be part of CI for checkboxes.

 Signature 

Un blog seo white-hat expliquant quelques techniques intéressantes sur le seo black hat

Ionize CMS - Webdesigner CMS based on CodeIgniter
http://www.ionizecms.com

My website: Webagency Too Pixel

 
Posted: 06 September 2007 06:18 PM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2006-04-20
712 posts

This may help some coders about this problem, I searched the forums for hours and found this:
http://ellislab.com/forums/viewthread/51260/

 Signature 

Un blog seo white-hat expliquant quelques techniques intéressantes sur le seo black hat

Ionize CMS - Webdesigner CMS based on CodeIgniter
http://www.ionizecms.com

My website: Webagency Too Pixel