简体   繁体   中英

Codeigniter's is_unique doesn't work with multiple fields

I'm making registration form, and I want fields nick and e-mail to be unique. I use form validation in controller:

  $this->load->library('form_validation'); $this->form_validation->set_rules('nick', '<b>Nazwa użytkownika</b>', 'required|is_unique[users.nick]'); $this->form_validation->set_rules('email', '<b>Adres e-mail</b>', 'required|is_unique[users.mail]'); $this->form_validation->set_rules('password', '<b>Hasło</b>', 'required'); 
    $this->form_validation->set_message('is_unique', '%s jest już w bazie.');

    if ($this->form_validation->run()) {
        $data['submit'] = $this->users_model->CreateUser($this->input->post()); //submits data
        $this->load->view('contribute/emptyPage', $data); //loads view
    } else {
        $data['title'] = 'Załóż konto';
        $this->layout->view('account/create',$data);
    }

It works when I fill only one field, eq. nick or mail . When I fill the whole form, it processes without a problem. Whan can I do?

edit.

I found out that problem occures only when one or more of the fields have only numbers in it.

您需要创建自己的自定义验证回调函数

I have created an is_unique function here

You can use it in 2 ways:

//is_unique[table_name.field_to_check.id_field_name.id_field_value] <-- unique 1 field only
//is_unique[table_name.field_to_check.id_field_name != 'id_field_value' and anotherIdName='theValue'] <-- custom where 

hope it could be usefull for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM