[eluser]marlar[/eluser]
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:
Code: 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:
Code: 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:
Code: // 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:
Code: <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')?>>
[eluser]Kinsbane[/eluser]
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!
[eluser]marlar[/eluser]
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.
[eluser]Jeffrey04[/eluser]
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
Quote: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
Code: <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
Code: 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
[eluser]WeeJames[/eluser]
Was getting the same as jeffery04. Fixed it by doing the following in Validation.php
Code: 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
Code: 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("'", """, '<', '>'), stripslashes($data));
}
|