[eluser]WanWizard[/eluser]
ExiteCMS uses this method.
To use it, extend the Form_validation library, and add it to the extension:
Code:
/**
* Run validation rules on an array of values, or a single value
*
* @access public
* @param mixed
* @param string
* @param string
* @return bool
*/
function run_value($value = FALSE, $rules = '', $label = '')
{
// save the current error messages
$errors = $this->_error_array;
$this->_error_array = array();
// save the current field data
$fielddata = $this->_field_data;
// create custom field data
if ( is_array($value) )
{
$this->_field_data = array();
foreach( $value as $key => $val )
{
if ( empty($val['label']) )
{
$this->_field_data['_value'.$key.'_'] = array(
'field' => '_value'.$key.'_',
'label' => '',
'rules' => $val['rules'],
'keys' => is_array($val['value']) ? array_keys($val['value']) : array(),
'postdata' => $val['value'],
'is_array' => is_array($val['value']),
'error' => ''
);
}
else
{
$this->_field_data[$val['label']] = array(
'field' => $val['label'],
'label' => $val['label'],
'rules' => $val['rules'],
'keys' => is_array($val['value']) ? array_keys($val['value']) : array(),
'postdata' => $val['value'],
'is_array' => is_array($val['value']),
'error' => ''
);
}
}
}
else
{
$field = empty($label) ? '_value_' : $label;
$this->_field_data = array($field => array(
'field' => $field,
'label' => $label,
'rules' => $rules,
'keys' => is_array($value) ? array_keys($value) : array(),
'postdata' => $value,
'is_array' => is_array($value),
'error' => ''
)
);
}
// Load the language file containing error messages
$this->CI->lang->load('form_validation');
// Cycle through the rules for each field and test for errors
foreach ($this->_field_data as $field => $row)
{
$this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
}
// store the validation errors (if any)
$this->run_value_errors = $this->_error_array;
// restore the validation values
$this->_error_array = $errors;
$this->_field_data = $fielddata;
// and return the validation result
return count($this->run_value_errors) ? FALSE : TRUE;
}