setting callback function within a model in config file
I have set the various validation rules for different pages in a config file. And in one page I want to check if a username exists or not. Is it possible to create a validation rule in the config file itself? but I created the function for checking in a model. I want to know if it is not possible to call the callback function in that model in config file?
I have read the article in http://codeigniter.com/wiki/MY_Validation_-_Callbacks_into_Models/. In that article it specified that we will have to be call the validation rule like:
$this->form_validation->run($this);
and by setting the valid开发者_如何学Goation rules in a config file, we will have to call the function like:
$this->form_validation->run('name of rule in config file');
How do I join both these?
setting a rule for validation by a callback is done just using a string. From that link:
$this->validation->set_rules(array(
'username' => 'trim|required|callback_users_model->is_unique[username]',
'password' => 'trim|required|matches[confirm]',
));
you can see that the callback method is set using a string
callback_users_model->is_unique[username]
where you just use a string
$rule = 'callback_' . $model_class . '->' $function_name . '[username]';
now you can just load $model_class and $function_name from your config the same as usual:
$model_class = $this->config->item('validation.model.class');
$function_name = $this->config->item('validation.method');
精彩评论