1

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 validation 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?

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

1 Comment

I editted the form_validation library to suit my needs. Now its working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.