且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在表单验证期间使用多个参数的Codeigniter中的回调函数

更新时间:2023-11-19 18:00:58

p>您的代码正常工作。您基本上总是使用字符串 用户名作为参数调用回调方法has_match。我认为您期望这将转换为:

  callback_has_match [$ username] 

因此,当您访问has_match()方法时,您可以访问 $ username 的值。然而,这不是回调方法工作的方式。您在其中设置的参数是一个字符串,它是硬编码的,就像你添加一个规则min_length [10]时 - 这不是PHP变量的值。一个简单的修复,我没有测试,但怀疑工程是:

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

但是上面的代码并不干净, b
$ b

现在我们已经发现了代码的问题,我知道这是在问题的范围之外,但我想指出 - 我发现这更多是一个设计问题。为什么要检查密码字段的回调中的用户名/密码对?



请记住,形式,你不应该与模型工作混合。表单不关心如果提供的用户名/密码组合是否正确,它应该只是看看这两个字段是否已正确提供,如果是,它应该做一些事情。



我将你的代码改为:

  $ 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){
//用户名/密码组合存在于数据库中,保存在会话或任何内容中。
} else {
//用户名/密码组合不存在于DB中,显示错误。
}
} else {
//表单字段未正确提供时的代码。
}


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?

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.
}