-
Notifications
You must be signed in to change notification settings - Fork 0
Form validation helpers support prepopulation
When working with the Form validator I always missed a feature to pre-populate a form (most commonly seen when editing an item) with some checkboxes and radio buttons already checked.
I modified the set_radio, set_checkbox and set_select helpers a bit so they accept different input in the $default parameter besides only true or false.
Some examples of how to use these modifications:
Controller:
[code] $this->load->library("Form_validation");
$set = new stdClass();
$set->defaults = array(4, 21, 72);
$this->form_validation->set_rules('sector[]', 'Sector');
$this->form_validation->set_rules('license', 'License');
$this->form_validation->set_rules('country', 'Country');
$this->load->view("header");
if($this->form_validation->run() === FALSE)
$this->load->view("form", array("set" => $set));
$this->load->view("footer");
[/code]
View:
[code]
<?php
for($i = 0; $i <= 10; $i++):
$id = $i * 3;
?>
<p><input <?= set_checkbox('sector[]', $id, (isset($set->defaults)) ? $set->defaults : FALSE); ?> type="checkbox" name="sector[]" value="<?=$id?>" />Sector <?=$id?></p>
<?php
endfor;
?>
<?php
for($i = 0; $i <= 10; $i++):
$id = $i * 8;
?>
<p><input <?= set_radio('license', $id, (isset($set->defaults)) ? $set->defaults : FALSE); ?> type="radio" name="license" value="<?=$id?>" />License <?=$id?></p>
<?php
endfor;
?>
<select name="country">
<?php
for($i = 0; $i <= 10; $i++):
$id = $i * 2;
?>
<option value="<?=$id?>" <?= set_select('country', $id, (isset($set->defaults)) ? $set->defaults : FALSE); ?>>Country <?=$id?></option>
<?php
endfor;
?>
</select>
[/code]
And here's the source code. Just name it MY_Form_validation.php and place it into your application/libraries folder to be used automatically when loading the Form_validation library.
[code] <?php
/* @author: Coen de Jong co.dejong@gmail.com
*/
/* This extension of the CI validation class is intended to improve the working of the set_select, set_radio and set_checkbox functions to support a second 'source' to check if POST doesn't exist yet */
class MY_Form_validation extends CI_Form_validation {
// --------------------------------------------------------------------
/**
* Set Select
*
* Enables pull-down lists to be set to the value the user
* selected in the event of an error
*
* @access public
* @param string
* @param string
* @return string
*/
function set_select($field = '', $value = '', $default = FALSE)
{
if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
{
//To not disturb the standard behavior of the function, we check if there is a bool or array in the $default param
if(is_bool($default))
{
//Do the standard behvior of the function
if ($default === TRUE AND count($this->_field_data) === 0)
{
return ' selected="selected"';
}
return '';
}
elseif(is_array($default))
{
//An array means there is a set of default values we need to check against
if(in_array($value, $default))
return ' selected="selected"';
else
return '';
}
elseif(is_int($default) || is_string($default))
{
//An integer or string means we can litteraly compare the two values with each other
if($value == $default)
return ' selected="selected"';
else
return '';
}
return '';
}
$field = $this->_field_data[$field]['postdata'];
if (is_array($field))
{
if ( ! in_array($value, $field))
{
return '';
}
}
else
{
if (($field == '' OR $value == '') OR ($field != $value))
{
return '';
}
}
return ' selected="selected"';
}
// --------------------------------------------------------------------
/**
* Set Radio
*
* Enables radio buttons to be set to the value the user
* selected in the event of an error
*
* @access public
* @param string
* @param string
* @return string
*/
function set_radio($field = '', $value = '', $default = FALSE)
{
if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
{
//To not disturb the standard behavior of the function, we check if there is a bool or array in the $default param
if(is_bool($default))
{
//Do the standard behvior of the function
if ($default === TRUE AND count($this->_field_data) === 0)
{
return ' checked="checked"';
}
return '';
}
elseif(is_array($default))
{
//An array means there is a set of default values we need to check against
if(in_array($value, $default))
return ' checked="checked"';
else
return '';
}
elseif(is_int($default) || is_string($default))
{
//An integer or string means we can litteraly compare the two values with each other
if($value == $default)
return ' checked="checked"';
else
return '';
}
return '';
}
$field = $this->_field_data[$field]['postdata'];
if (is_array($field))
{
if ( ! in_array($value, $field))
{
return '';
}
}
else
{
if (($field == '' OR $value == '') OR ($field != $value))
{
return '';
}
}
return ' checked="checked"';
}
// --------------------------------------------------------------------
/**
* Set Checkbox
*
* Enables checkboxes to be set to the value the user
* selected in the event of an error
*
* @access public
* @param string
* @param string
* @return string
*/
function set_checkbox($field = '', $value = '', $default = FALSE)
{
if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
{
//To not disturb the standard behavior of the function, we check if there is a bool or array in the $default param
if(is_bool($default))
{
//Do the standard behvior of the function
if ($default === TRUE AND count($this->_field_data) === 0)
{
return ' checked="checked"';
}
return '';
}
elseif(is_array($default))
{
//An array means there is a set of default values we need to check against
if(in_array($value, $default))
{
return ' checked="checked"';
}
else
{
return '';
}
}
elseif(is_int($default) || is_string($default))
{
//An integer or string means we can litteraly compare the two values with each other
if($value == $default)
return ' checked="checked"';
else
return '';
}
else
{
return '';
}
}
$field = $this->_field_data[$field]['postdata'];
if (is_array($field))
{
if ( ! in_array($value, $field))
{
return '';
}
}
else
{
if (($field == '' OR $value == '') OR ($field != $value))
{
return '';
}
}
return ' checked="checked"';
}
}
/* End of file MY_Form_validation.php / / Location: ./system/application/libraries/MY_Form_validation.php */ [/code]
- Original author: Derek Jones
- How to extend helpers: See User Guide
- Modified by: Thomas Stapleton (id, classes, selected country option and all option)
- Modified by: Bradley De-Lar (construct, setLayout example)