Part of the EllisLab Network
   
 
Population of checkbox arrays in Validation library
Posted: 26 January 2008 10:43 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  49
Joined  12-05-2007

Hi,

I think I have found a bug in the validation library.

Checkboxes don’t seem to get re-populated if they are part of a group, like:

echo form_checkbox('group[]','family', $this->validation->set_checkbox('group', 'family'));
echo
form_checkbox('group[]','friends', $this->validation->set_checkbox('group', 'friends'));

MODIFIED CODE; NOW IT ALSO WORKS FOR CHECKBOX GROUPS!
Here is a modification of set_checkbox() in the validation libary so it works as expected:

function set_checkbox($field = '', $value = '')
    
{
        
if ($field == '' OR $value == '')
        
{
            
return '';
        
}
        
if(isset($_POST[$field])) {
            
if ($_POST[$field] == $value || ( is_array($_POST[$field]) && in_array($value, $_POST[$field])) )
            
{
                
return ' checked="checked"';
            
}
        }
        
elseif(isset($this->$field)) {
            
if($this->$field==$value || ( is_array($this->$field) && array_search($value, $this->$field)!==FALSE) ) {
                
return ' checked="checked"';
            
}
        }
    }

HOW TO USE IT:

In the controller:

// for a single checkbox just use the checkbox' value:
$this->validation->mycheckbox = 1;
$this->validation->mycar = "big";

// for a checkbox group, use an array with the values that should be checked:
$this->validation->mycolors = array("red","blue","yellow");

These lines must come AFTER $this->validation->set_fields() !

In the view:

<input type="CHECKBOX" name="mycheckbox" value="1" <?=$this->validation->set_checkbox('mycheckbox', '1')?>>
<
input type="CHECKBOX" name="mycar" value="big" <?=$this->validation->set_checkbox('mycar', 'big')?>>
<
input type="CHECKBOX" name="mycolors[]" value="red" <?=$this->validation->set_checkbox('mycolors', 'red')?>>
<
input type="CHECKBOX" name="mycolors[]" value="blue" <?=$this->validation->set_checkbox('mycolors', 'blue')?>>
<
input type="CHECKBOX" name="mycolors[]" value="green" <?=$this->validation->set_checkbox('mycolors', 'green')?>>
<
input type="CHECKBOX" name="mycolors[]" value="yellow" <?=$this->validation->set_checkbox('mycolors', 'yellow')?>>
Profile
 
 
Posted: 07 February 2008 01:11 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  52
Joined  10-16-2007

Hi, question about this:

If we have many checkboxes that store one or more values, and they share the name “mycolors[]”, how would we go
about setting that using $this->validation->set_fields() ?

Would it be something like:

$fields[mycolors[]] = mycolors[];

?

or just $fields[mycolors] = ‘’;


Thanks for this!

Profile
 
 
Posted: 07 February 2008 02:24 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  49
Joined  12-05-2007

I’m not sure I understand your question, but if you want to pre-fill the checkboxes, you do like this:

$this->validation->mycolors = array(“red”,“blue”,“yellow”);

Where red, blue, yellow are the fields you want to have checked.

Profile
 
 
Posted: 16 April 2008 10:08 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  24
Joined  03-19-2008

so will this fix be committed to the development repository soon?

anyway, I just got this error after I replaced the code. The posting back of value seems to be correct though

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: libraries/Validation.php

Line Number: 715

my view file looked like this

<li>
                    <
input type="checkbox" id="meeting_user[]" name="meeting_user[]" value="1" <?php echo $this->validation->set_checkbox('meeting_user', '1'); ?> />
                    <
label for="meeting_user">A</label>
                    
<?php $this->validation->meeting_user_error; ?>
                
</li>
                <
li>
                    <
input type="checkbox" id="meeting_user[]" name="meeting_user[]" value="2" <?php echo $this->validation->set_checkbox('meeting_user', '2'); ?> />
                    <
label for="meeting_user">B</label>
                    
<?php $this->validation->meeting_user_error; ?>
                
</li>

and the controller

private function _get_meeting_validation_rules() {
            
return array(
                
'event_start' => 'callback_start_time_check',
                
'event_end' => 'callback_end_time_check',
                
'event_subject' => 'required',
                
'event_des' => 'required',

                
'meeting_venue' => 'required',
                
'meeting_user' => 'callback_meeting_attendees_check'
            
);
        
}

        private
function _get_meeting_field_data() {
            
return array(
                
'event_start' => 'Start Time and Date',
                
'event_end' => 'End Time and Date',
                
'event_subject' => 'Subject',
                
'event_des' => 'Description',

                
'meeting_venue' => 'Venue',
                
'meeting_user' => 'Attendees'
            
);
        
}

        public
function meeting_attendees_check($attendees) {
            
// some codes
        
}

using codeigniter version 1.6.1

Profile
 
 
Posted: 17 April 2008 09:39 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  70
Joined  02-20-2007

Was getting the same as jeffery04.  Fixed it by doing the following in Validation.php

function set_checkbox($field = '', $value = '')
    
{
        
if ($field == '' OR $value == '' OR  ! isset($_POST[$field]))
        
{
            
return '';
        
}
        
if ($_POST[$field] == $value || (is_array($_POST[$field]) && in_array($value, $_POST[$field]) ) )
        
{
            
return ' checked="checked"';
        
}
    }

and

function prep_for_form($data = '')
    
{
        
if (is_array($data))
        
{
            
foreach ($data as $key => $val)
            
{
                $data[$key]
= $this->prep_for_form($val);
            
}
            
            
return $data;
        
}
        
        
if ($this->_safe_form_data == FALSE OR $data == '')
        
{
            
return $data;
        
}

        
return str_replace(array("'", '"', '<', '>'), array("&#39;", "&quot;", '<', '>'), stripslashes($data));
    
}
Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 115027 Total Logged-in Users: 64
Total Topics: 122466 Total Anonymous Users: 3
Total Replies: 647381 Total Guests: 460
Total Posts: 769847    
Members ( View Memberlist )