[eluser]skunkbad[/eluser]
I find it odd that the set_value method that is in the Form validation library uses array_shift() to return the first value in the array, and any other elements of the array are unusable. What is the reason for this?
I extended the Form validation library to suit my needs, but it seems odd that there isn't an easier way to validate AND REPOPULATE checkboxes. It took quite a bit of work for me to get the validation of a group of checkboxes working.
view:
Code:
<div class="form-row">
<?php
/*
* GET VALUE OF CHECKBOXES GROUP
*/
$submission_values = set_value('checkboxes[]', ( isset($personal_data->checkboxes) ) ? unserialize($personal_data->checkboxes) : array() );
$checkbox_data = array(
'name' => 'checkboxes[]',
'id' => 'checkbox_one',
'value' => 'one',
'checked' => ( ! empty( $submission_values ) ) ? ((in_array('one', $submission_values)) ? 'checked' : '') : '',
'class' => 'form_checkbox'
);
echo form_checkbox($checkbox_data);
echo form_label('One', 'checkbox_one', array('class'=>'checkbox_label'));
?>
</div>
<div class="form-row">
<?php
$checkbox_data = array(
'name' => 'checkboxes[]',
'id' => 'checkbox_two',
'value' => 'two',
'checked' => ( ! empty( $submission_values ) ) ? ((in_array('two', $submission_values)) ? 'checked' : '') : '',
'class' => 'form_checkbox'
);
echo form_checkbox($checkbox_data);
echo form_label('Two', 'checkbox_two', array('class'=>'checkbox_label'));
?>
</div>
MY_Form_validation.php:
Code:
<?php
class MY_Form_validation extends CI_Form_validation {
// need arrays to stay intact!
function set_value($field = '', $default = '')
{
if ( ! isset($this->_field_data[$field]))
{
return $default;
}
return $this->_field_data[$field]['postdata'];
}
}
What am I doing wrong? Why is it so hard to achieve checkbox validation and repopulation? Maybe it is because I am removing data from $this->form_validation->_field_data if it didn't pass validation:
Code:
// do not repopulate with data that did not validate
foreach( $_POST as $k => $v )
{
if( array_key_exists( $k, $this->form_validation->_error_array ))
{
//kill set_value()
unset($this->form_validation->_field_data[$k]);
}
}
There's a lot to be desired...