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

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

问题描述

在Codeigniter中:



这里是我用于验证的回调函数:

  public function has_match($ password,$ username){
if(0){
//用户存在
return true;
}
else {
$ this-> form_validation-> set_message('has_match','输入的用户名/密码无效'。$ password。
return false;
}
}

以下是验证规则:

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

任何人都可以告诉我我在调用回调函数时做错了什么无法获取用户名字段的值,并保持显示'username'(在回调中的变量$ username内)。

解决方案

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

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

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