1
votes

Hello all i was using form_validation Library in codeigniter inside my application. I am making a password retrieving function. I have made a submit form of the email . Now, on the email input field i want to apply these validations.

  1. required
  2. valid_email
  3. check email exist or not.

For the 3rd one i am using call back function to check the database and it worked fine. But with the call back function valid_email is not functioning. This is my controller functions.

     public function password_retrieve()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') 
    {

        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback__email_exists');
        if ($this->form_validation->run() == false) {
            $this->load->view('login_header');
            $this->load->view('password_retrieve');
            $this->load->view('login_footer');
        } else {

        }
    } else {
        $this->load->view('login_header');
        $this->load->view('password_retrieve');
        $this->load->view('login_footer');
    }
    }

    function _email_exists($email)
    {
    $this->load->model('users_model');

    $result = $result = $this->users_model->check_email_is_exist($email);
    if (!$result) {
        $this->form_validation->set_message(__FUNCTION__, 'This %s address does not exist!');
        return false;
    } else {
        return true;
    }
    }

It should checked the valid_email rather than the going to the callback function.

In other mean i want to know what is the order of the rules. Is callback rule runs before the valid_email?

2
valid_email is a native rule so no reason it shouldn't work... What email are you trying? - Callombert
i tried using test and it directly says this doesnot exist. ignoring the valid_email rule - tech_geek
why there is an underscore _ in function name. just remove it and try. just email_exists($email) - Yadhu Babu
tried already still not succeeded. - tech_geek

2 Answers

0
votes

Try to remove "trim" and for check if email exist don't use another function. But use "is_unique[table_name.email]".

$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[table_name.email]');
0
votes

By Searching the official documentation and git repository i have found out that there is not a particular order in which a function will run in codeigniter. This means $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback__email_exists'); In this case valid_email will run after the callback__email_exists. There is not a order in which first trim, then required and then valid_email will run.

So what i have done is to make a new function in my callback function which runs after the required but before the check_email.

Answering this question so that in future people can get the benefit from it. Cheers!