Codeigniter中字符串的回调函数

时间:2014-04-24 10:26:46

标签: php codeigniter

任何人都知道如何在Codeigniter中为字符串设计回调函数?如果对int使用此函数,它可以工作。对于字符串,验证不起作用。我的模型是:

> function check_user_name($User_Name='')
>     {
>         $this -> db -> select('User_Name');
>         $this -> db -> from('login');
>         $this -> db -> where('User_Name',$User_Name);
>         $this -> db -> limit(1);
>         $query = $this -> db -> get();
>             
>         if($query -> num_rows() == 1)
>         {
>             return $query->result();
>         }
>         else
>         {
>             return false;
>         }
>     }

这是我的控制器:

表单验证

  

$this->form_validation->set_rules('User_Name','User_Name','trim|required|xss_clean|callback_check_User_Name');

> function check_User_Name($User_Name)
>     {
>         //Field validation succeeded.  Validate against database
> 
>         $row = $this->UserRegistration->check_user_name($User_Name);//query the
> database
> 
>         if($row)
>         {
>             return TRUE;
>         }
>         else 
>         {
>             $this->form_validation->set_message('check_User_Name', 'The User_Name already exist.Please use other User_Name.Thank you.');
>             return FALSE;
>         }
>     }

1 个答案:

答案 0 :(得分:0)

将验证脚本放入功能中。

function check_User_Name($ User_Name)          {              //字段验证成功。验证数据库     如果($这 - > form_validation-set_rules( '的User_Name', '的User_Name', '修剪|所需| xss_clean | callback_check_User_Name'))     {

         $row = $this->UserRegistration->check_user_name($User_Name);//query the
 database

         if($row)
        {
             return TRUE;
         }
        else 
         {
             $this->form_validation->set_message('check_User_Name', 'The User_Name already exist.Please use other User_Name.Thank you.');
            return FALSE;
         }
       }
     }