donglu1913 2010-11-06 22:29
浏览 19
已采纳

Codeigniter回调失败

Can anyone see what I'm missing?

I'm using Codeigniter v1.72.

In the doc:

http://codeigniter.com/user_guide/libraries/form_validation.html

It states:

$this->form_validation->set_rules('username', 'Username', 'callback_username_check');

function username_check($str)
    {
        if ($str == 'test')
        {
            $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

In my class User extends Controller

I have in function register:

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check('.$username.')');

I have also tried 

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check');

And

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check['.$username.']');


    function username_check($str)
    {
            $this->load->model('User_model', '', TRUE);
            $taken = $this->User_model->countUsername($str);

            if($taken)
            {
                $this->form_validation->set_message('username_check', 'That username is already taken');
                return FALSE;
            }
            else
                return TRUE;
    }

There are no errors at all, none of my approaches work, the code behaves like it's not there.

  • 写回答

1条回答 默认 最新

  • dqv84329 2010-11-06 22:59
    关注

    First of all, I'm assuming the rest of your code is correct. It might help to show the whole User class.

    You might want to check if CodeIgniter lets you invoke callback functions AND prepping/validator functions in the same rule. If it doesn't allow that, you could call trim, require, and xss_clean in your callback function.

    I will say, though, that if it is allowed, then this is definitely the RIGHT form:

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check');

    This is wrong:

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check['.$username.']');

    And this is wrong too:

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean', 'callback_username_check('.$username.')');

    See, in that line, you shouldn't actually be CALLING the function. Rather, you're passing a string to the set_rules() function that it will parse and figure out what function you want to use as a callback.

    As the documentation states, whatever the value of username is, will be passed as the argument to your callback function.

    EDIT:

    I was going to say try this:

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');

    But I don't think it will work. Instead, this should work:

    function username_check($str)
        {
            $this->load->model('User_model', '', TRUE);
            $taken = $this->User_model->countUsername($str);
    
            if($taken)
            {
                $this->form_validation->set_message('username_check', 'That username is already taken');
                return FALSE;
            }
            else if(!$str) {
                // This is functioning as the required rule
                return FALSE;
            }
            else {
                $str = trim($str);
                $str = $this->input->xss_clean($str);
                return $str;
            }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应