库中的CodeIgniter验证不接受回调 [英] CodeIgniter Validation in Library does not accept callback

查看:157
本文介绍了库中的CodeIgniter验证不接受回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是以下:我正在写一个登录库。
此库有一个函数_validation(),它使用验证库来验证数据。
使用正常的验证方法,它工作正常,但使用回调函数不工作。






$ b

我叫这样。

$ this-> CI-> form_validation-> set_rules('user','Username','required | callback__check_user');

函数名称为_check_user,它使用用户名_check_user($ user)。
函数本身工作正常,我也可以在类($ this - > _ check_user('username'))中调用它的工作结果。



我猜,可能有一个问题,因为我不是在控制器中工作,所以我有一个CI实例$ this-> CI而不是原始的实例$



有人提前感谢。

解决方案

嘿,我想出了一种适合我的方式。通过在MY_Form_validation.php中扩展Form_validation库,您可以创建自定义验证方法。我认为这是一个干净的方式,它的作品完全正常。我构建以下验证方法来检查现有的用户名和密码。 $ value是类似table_name.fieldname。
我没有设置消息,因此它将使用来自lang文件的_exist消息。

  / ** 
* Exist
*
*检查数据库中是否存在条目
*返回布尔值
*
* @access private
* @ param string
* @param field
* @return boolean
* /
function _exist($ str,$ value)
{
list ,$ column)= explode('。',$ value,2);
$ query = $ this-> CI-> db-> query(SELECT COUNT(*)AS count FROM $ table WHERE $ column ='$ str');
$ row = $ query-> row();
return($ row-> count> 0)?真假;
}

感谢您的帮助。


my problem is the following: I am writing a login library. This library has a function _validation() and this uses the validation library to validate the data. With using normal validation methods it works just fine, but using a callback function just does not work. It is not called.

I call it like this.

$this->CI->form_validation->set_rules('user', 'Username', 'required|callback__check_user');

The functions name is _check_user and it uses the username _check_user($user). The function itself works fine and I can also call it in the class ($this->_check_user('username')) with a working result.

I am guessing, there might be a problem because I am not workin in a controller so I have a CI instance $this->CI instead of just the original instance $this->

Does anyone have a clue how to fix this?

Thanks in advance.

解决方案

Hey, I figured out a way that works for me. By just extending the Form_validation library in MY_Form_validation.php you can create custom validation methods. I think it is a clean way and it works perfectly fine. I build the below validation method to check for existing usernames and passwords. $value is something like table_name.fieldname. I have not message set so that it will use the _exist messages from the lang files.

/**
 * Exist
 *
 * checks if the entry exists in the database
 * returns a boolean
 *
 * @access  private
 * @param   string
 * @param   field
 * @return  boolean
 */
function _exist($str, $value)
{       
    list($table, $column) = explode('.', $value, 2);    
    $query = $this->CI->db->query("SELECT COUNT(*) AS count FROM $table WHERE $column = '$str'");
    $row = $query->row();
    return ($row->count > 0) ? TRUE : FALSE;
}

Thanks for your help though.

这篇关于库中的CodeIgniter验证不接受回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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