Two field as one unique field in Grocery Crud

主宰稳场 提交于 2019-12-06 09:39:38

Apart from your table design login, you could create a custom callback function to check the existance of these values in your database. The validation will take place in one of your 2 fields, for example site_code:

public function _unique_code_no($site_code)
{
    $CI =& get_instance();
    $CI->form_validation->set_message('_unique_code_no', 'the values must be unique');
    $site_no=$CI->input->post('id');
    $query=$CI->db->query("select 1 from mytable where site_no=$site_no and site_code=$site_code");
    if ($query->num_rows==0){
        return TRUE;
    }else{
        return FALSE;
    }
}

And the validation rule:

$this->form_validation->set_rules('site_code', 'Site code', 'required|_unique_code_no');

It sounds like you should look over the constraints on your table.

Is there a good reason for using an id column in this case? If there is not you should remove it and then set both site_code and site_no as your primary key.

With (site_code,site_no) as your primary key yo will not be able to insert duplicates of those two values.

Christopher

I think you should use callback_column( string $column , mixed $callback ) method. Check this link CallBack Column

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