I’ve done my share of bitching about CI lately, nevertheless, I thought I’d share a little code that makes working with bigger applications more manageable. CI does not by itself accommodate external callbacks for form validation, and models are a great place to store them, since the CI super object is accessible from inside them.
What I’ve done is created a generic external_callbacks method in MY_Controller. The method explodes the comma seperated string that contains the model name, method, and any arguments that should be passed along as an array.
Here is the method:
/*
* external_callbacks method handles form validation callbacks that are not located
* in the controller where the form validation was run.
*
* $param is a comma delimited string where the first value is the name of the model
* where the callback lives. The second value is the method name, and any additional
* values are sent to the method as a one dimensional array.
*
* EXAMPLE RULE:
* callback_external_callbacks[some_model,some_method,some_string,another_string]
*/
public function external_callbacks( $postdata, $param )
{
$param_values = explode( ',', $param );
// Make sure the model is loaded
$model = $param_values[0];
$this->load->model( $model );
// Rename the second element in the array for easy usage
$method = $param_values[1];
// Check to see if there are any additional values to send as an array
if( count( $param_values ) > 2 )
{
// Remove the first two elements in the param_values array
array_shift( $param_values );
array_shift( $param_values );
$argument = $param_values;
}
// Do the actual validation in the external callback
if( isset( $argument ) )
{
$callback_result = $this->$model->$method( $postdata, $argument );
}
else
{
$callback_result = $this->$model->$method( $postdata );
}
return $callback_result;
}
/*******************************************************************/
