[eluser]Michael Ekoka[/eluser]
Two approaches that I can suggest:
1-) Validation in the Model: be aware that the Validation class invokes the validation callback methods in the controller. To effectively bypass the controller and create your callbacks in the model, you can use a combination of the __call() magic method and the call_user_func_array(). If you are developing in php4 I'd suggest to also have a look at the overload() php function. The best approach is still to move on to php5.
Now, inside your controller you want to do something like this:
Code:
public function __call($function,$args){
if(method_exists($this->model,$function)){
return call_user_func_array(array(&$this->model,$function),$args);
}
}
If the callback that you're calling has not been defined in the controller, it will try to call it from the model ($this->model).
Note however that the Validation::run() method, has a little routine around line 271 that checks if the callback method has been defined in the controller.
Code:
if ( ! method_exists($this->CI, $rule))
{
continue;
}
That little check will effectively prevent the controller's __call() method to kick in. So, you will need to comment it out.
2-) Setting your basic validation rules in a parent controller: you can create your base controllers inside /application/libraries/MY_Controller.php. You can have as many base controller as you want. My setup usually looks like this:
Code:
// MY_Controller.php
class MY_Controller extends Controller{} // common settings for all my controllers
class Admin_Controller extends MY_Controller {} // common settings for my admin section controllers
class Application_Controller extends MY_Controller {} // common settings for front end controllers
// You can add as many base controllers here: e.g. Forms, Site, etc... I usually manage with 3
You can set your common validation rules inside these base controllers. These rules will be available to any controller that extends these parents.
In /application/controllers/
Code:
// controller_x.php
class Controller_x extends Application_Controller{}
// controller_y.php
class Controller_y extends Application_Controller{}