1

This is a simple login validation script in codeigniter. I can't understand the problem. I have gone through the user guide but the callback just doesn't work.

public function form_validation()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', 'Username', 'required|trim        |alpha_numeric');
    $this->form_validation->set_rules('password', 'Password', `enter code here` 'required|trim |xss_clean');
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|xss_clean|callback_validate');
    if ($this->form_validation->run()) {
        echo "validated but not logged";
    } else {
        $this->load->view('errors/formerror');

    }
}

public function validate()
{
    $this->load->model('model_users');
    if ($this->model_users->can_login()) {
        echo "Logged";
    } else {
        $this->form_validation->set_message('validate', "Incorrect username/password");
    }

}

2 Answers 2

1
public function form_validation()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_numeric');
    $this->form_validation->set_rules('password', 'Password', 'required|trim |xss_clean');
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|xss_clean|callback_email_check');
    if ($this->form_validation->run()) {
        echo "validated but not logged";
    } else {
        $this->load->view('errors/formerror');
    }
}

public function email_check($email)
{
    $this->load->model('model_users');
    if ($this->model_users->can_login($email)) {
        echo "Logged";
        return true;
    } else {
        $this->form_validation->set_message('email_check', "Incorrect username/password");
        return false;
    }
}

Add _check suffix like callback_email_check and callback method email_check and check. Find more here CodeIgniter Callbacks

Sign up to request clarification or add additional context in comments.

5 Comments

You forgot to add return true and return false in email_check method
And need remove echo :)
@Winston, that is as it was in question. I think the questioner will consider. may be he want to see whether the method is running...
the callback is working...but somehow I can't invoke the model and it's showing.... validated but not logged i.e. going straight to the form_validation_run part.....
public function can_login() { $sitekey="My Key"; $this->db->where('email',$this->input->post('email')); $this->db->where('password_hash',md5($this->input->post('password'))); $this->db->where('password_salt',md5($this->input->post('password').$sitekey)); $login_query =$this->db->get('users'); if($login_query->num_rows == 1) {return true;} else return false; }
0

I got myself into a mess like this one and almost drowned for a long day. Here was the problem. I was working with a model and it turns out that the function needed for callback was not working while in a model. To fix this:

Ensure that the function you are using for callback is in the controller and not in the model.

It worked like magic. I can't figure why but it fixed my callback case.

Adding check suffix doesn't make that much difference as the manual said nothing about suffixes in particular. The prefix callback is what I know should never be left out. I have several callbacks that have worked without the _check suffix.

Here is what the manual says:

To invoke a callback just put the function name in a rule, with "callback_" as the rule prefix. If you need to receive an extra parameter in your callback function, just add it normally after the function name between square brackets, as in: "callback_foo[bar]", then it will be passed as the second argument of your callback function

What I did notice was the example in the manual had the callback function in the controller and not in the model.

Comments

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.