is_unique for codeigniter form validation

前端 未结 9 1071
走了就别回头了
走了就别回头了 2020-12-03 07:19

I\'m trying to figure out how I can use the is_unique rule from the Codeigniter form validation library in the following situation.

I\'m trying to submi

9条回答
  •  失恋的感觉
    2020-12-03 07:56

    This code helpful for unique validation to create and update function...

    In controller

    Add this form validation code in both create and update function

    $this->form_validation->set_rules('order_no', 'Order no', 'required|callback_check_order_no');
    

    Add this call back function in controller

    function check_order_no($order_no) {        
            if($this->input->post('id'))
                $id = $this->input->post('id');
            else
                $id = '';
            $result = $this->Data_model->check_unique_order_no($id, $order_no);
            if($result == 0)
                $response = true;
            else {
                $this->form_validation->set_message('check_order_no', 'Order no already exist');
                $response = false;
            }
            return $response;
        }
    

    In model

    function check_unique_order_no($id = '', $order_no) {
            $this->db->where('order_no', $order_no);
            $this->db->where('status', "A");
    
            if($id) {
                $this->db->where_not_in('id', $id);
            }
            return $this->db->get('delivery_order')->num_rows();
        }
    

提交回复
热议问题