Our own Validation Functions using Callbacks

Previously we have seen form validation using CI. This validation also supports callbacks to our own validation functions. For example, if we need to run a database query to see if the user is choosing a unique username, we can create a callback function that does that. Let’s create a example of this.
Suppose we have a registration form where a user can register his profile. Here we need to maintain the unique username. The form looks like:

Login Form

Signup Form

We  have to create a callback function to run a database query to get  unique username .

Syntax: $this->form_validation->set_rules(‘username’, ‘Username’, ‘callback_username_check‘);
Notes: To invoke a callback just put the function name in a rule, with “callback_” as the rule prefix.
So the controller look like:

                      class Login extends CI_Controller {
                      public function create()
                      {
                                $this->load->helper(array(‘form’, ‘url’));
                                $this->load->library(‘form_validation’);
                                $this->form_validation->set_rules(‘username’, ‘Username’, ‘callback_username_check’);
                                $this->form_validation->set_rules(‘password’, ‘Password’, ‘required’);
                                $this->form_validation->set_rules(‘passconf’, ‘Password Confirmation’, ‘required’);

if ($this->form_validation->run() == FALSE)

                                {
                                                $this->load->view(‘signup_form’);
                                }
                                else
                                {
                                                $this->load->view(‘members_area’);
                                }
                      }

Here username_check is the callback function. And the function may looks like :

public function username_check($str){
      $this->load->model(‘membership_model’);
       $q=$this->membership_model->username_exists($key);
    if($q==TRUE)
        {
            $this->form_validation->set_message(‘username_exists’,'%s already exist !!’);
            return FALSE;
        }
    }          }
First, load  membership_model (i.e.  $this->load->model(‘membership_model’ ) .So lets see the membership_model:
function username_exists($key){
    $this->db->where(‘user_name’,$key);
    $query = $this->db->get(‘employee’);
    if ($query->num_rows > 0)
        {
        return TRUE;
        }
    else{
        return FALSE;
    }
}
Here DB check the given username already exist or not. If given username already exist, model returns TRUE  to the controller and controller check if the model returns TRUE  then set error message $this->form_validation->set_message(‘username_exists’,'%s already exist !!’ ) by this function. Then this error message  has to display on the proper view (here we display error message on  signup_from)  that we have discussed earlier topic. Here sanjit_paul have already registered.
Error display

Error display

Hope it will help in your projects.Stay in touch with us to get  more topic on CI.