Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - Callback function in Codeigniter with multiple parameters during form validation

In Codeigniter:

Here is the callback function that I am using for validation:

public function has_match($password, $username){
    if (0) {
        // user exists
        return true;
    }
    else {
        $this->form_validation->set_message('has_match', 'Invalid Username/password entered ' . $password . ' ' . $username);
        return false;
    }
}

Below are the validation rules:

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|callback_has_match[username]');

Can any one please tell me what I am doing wrong here in calling the callback function, as I am unable to get the value of the username field and it keeps showing 'username' (inside the variable $username in callback) instead?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code is working as expected. You're basically always calling the callback method has_match with the string username as a parameter. I think that you expect that this translates into:

callback_has_match[$username]

Therefore, when you access the has_match() method, you would have access to the value of $username. This is however, not the way callback methods work. The parameter that you set in there is a string, which is hardcoded, exactly like you do when you add a rule for min_length[10] - it's not the value of a PHP variable. An easy fix, which I haven't tested but suspect works is to do:

$this->form_validation->set_rules('password', 'Password', 'required|callback_has_match[' . $username . ']');

However the code above is not clean, and seems bad design in my opinion.

Now that we've found the problem with the code, I know it's outside the scope of the question, but I would like to point it out - I find it's more of a design issue here. Why do you want to check for the username/password pair inside of a callback to the password field?

Remember, you're validating a form, you shouldn't mix it up with model work. The form doesn't care if the provided username/password combo is correct, it should just look at whether both the fields have been provided correctly, and if so, it should do something about it.

I would adapt your code above to:

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|callback_has_match[username]');

if ($this->form_validation->run() != FALSE) {
    $validLogin = $this->muser->checkLogin($username, $password);

    if ($validLogin) {
        //Username/password combo exists in DB, save in session or whatever.
    } else {
        //Username/password combo does not exist in DB, show an error.
    }
} else {
    //Code for when form fields have not been provided correctly.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...