0

由于 AngularJS 中的错误,我比以往任何时候都需要让 CodeIgniter 验证我的注册表单。

我已经尽我所能来解决这个问题。也许它只是需要一双不同的眼睛?或者也许有人以前处理过这个问题?

    public function register_validation() {

        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username',
            'required|trim|is_unique[users.username]|max_length[32]');
        $this->form_validation->set_rules('password', 'Password',
            'required|trim|min_length[6]');
        $this->form_validation->set_rules('email', 'Email',
            'required|trim|valid_email|is_unique[users.email]');

        if($this->form_validation->run()) {
            echo "Pass!";
        } else {
            echo "NOPE!";
            $this->load->view('home');
        }

    }
4

3 回答 3

0

This line:

$this->form_validation->set_rules('username', 'Password',
            'required|trim|min_length[6]');

Should be:

$this->form_validation->set_rules('password', 'Password',
            'required|trim|min_length[6]');

You used username twice. You should change your run() check to:

if($this->form_validation->run() == FALSE))
{
    echo "NOPE!";
    $this->load->view('home');
}
else
{
    echo "Pass!";
}
于 2013-09-17T04:20:21.200 回答
0

this line is correct

$this->form_validation->set_rules('username', 'Username',
        'required|trim|is_unique[users.username]|max_length');

first username is your field name and second one is which show in the error.

but this line

this->form_validation->set_rules('username', 'Password',
        'required|trim|min_length[6]');

here you used username name which is already validating instead of username you used password

there are many ways for applying conditions

first one is

if($this->form_validation->run() == FALSE)) {
    $this->load->view('home');
} else {
    echo "Pass!";
}

second one is

if($this->form_validation->run() != FALSE) {
     echo "Pass!";
} else {
     $this->load->view('home');
}
于 2013-09-17T04:24:14.417 回答
0

改变

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

    if($this->form_validation->run($this)) {
于 2013-09-17T06:36:37.103 回答