Codeigniter's is_unique doesn't work with multiple fields

廉价感情. 提交于 2019-12-25 05:29:11

问题


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.


回答1:


You need to create your own custom validation callback function




回答2:


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



来源:https://stackoverflow.com/questions/11328926/codeigniters-is-unique-doesnt-work-with-multiple-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!