表单验证期间带有多个参数的 Codeigniter 中的回调函数 [英] Callback function in Codeigniter with multiple parameters during form validation

查看:30
本文介绍了表单验证期间带有多个参数的 Codeigniter 中的回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Codeigniter 中:

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

以下是验证规则:

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

任何人都可以告诉我我在调用回调函数时做错了什么,因为我无法获取用户名字段的值并且它一直显示用户名"(在回调中的变量 $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?

推荐答案

您的代码按预期工作.您基本上总是使用 string username 作为参数调用回调方法 has_match.我认为您希望这会转化为:

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]

因此,当您访问 has_match() 方法时,您将有权访问 $username 的值.然而,这不是回调方法的工作方式.您在其中设置的参数是一个硬编码的字符串,与您为 min_length[10] 添加规则时所做的完全一样 - 它不是 PHP 变量的值.一个简单的修复方法,我还没有测试过,但怀疑可行:

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

这篇关于表单验证期间带有多个参数的 Codeigniter 中的回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆