开发者

Form validation with custom callback function

I created a "callback" function to check if the username exists in the DB. I have multiple rules for the "username" field, but the only thing that work is my callback function. It refuses to check against the other rules. I tried leaving the field empty, and the "required" rule never kicked in.

Controller:

account.php

function register() {
    $this->load->library('validation');

    $fields['username'] = "trim|required|callback_username_check";
    etc ...
    etc ...

    $this->validation->set_rules($fields);

    if ($this->validation->run()) {

        $records = array();
        $records['username'] = $this->validation->username;
        etc ...
        etc ...

        $data = $this->account_mode开发者_StackOverflow中文版l->registerNewAccount($records);     
    }
    $this->load->view('register_view');
}

function username_check($username) {
    $m = new Mongo();
    $collection = $m->selectDB( DBNAME )->selectCollection( TABLE );

    $data = $collection->count(array("username" => $username) );

    if($data == 1) {
        $this->validation->set_message('username_check', '%s is already taken!');
        return false;
    } else {
        return true;
    }       
}


Try using the new form_validation class here:

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

I believe there was a bug about it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜