Как создать проверку is_unique в Codeigniter с помощью form_validation-> set_rules (), которая будет работать при обновлении? - PullRequest
1 голос
/ 08 апреля 2020

Как создать правило проверки в CI, которое может работать в режиме обновления на основе уникального значения? Я создал функцию в моем контроллере для добавления некоторого значения в таблицу.

/**
     * Function to create and update a smart pixel 
     * @return  json 
     */
    public function create()
    {
        $this->form_validation->set_rules('pixel_name', 'pixel name', 'required');
        $this->form_validation->set_rules('pixel_id', 'pixel id', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $temp = array();
            $temp['header'] = $this->lang->line('smart_pixel_create_validation_error_heading');
            $temp['content'] = $this->lang->line('smart_pixel_create_validation_error_message') . validation_errors();
            $this->return = array();
            $this->return['responseCode'] = 0;
            $this->return['responseHTML'] = $this->load->view('shared/error', array('data'=>$temp), TRUE);
            die(json_encode($this->return));
        }

        /** Checking for unique pixel name while adding a pixel */
        if($this->input->post('field_id') == '' && $this->input->post('pixel_name') != ''){
            $err = [];
            $err['header']  = $this->lang->line('smart_pixel_create_validation_error_heading');
            $err['content'] = $this->lang->line('smart_pixel_create_pixel_name_validation_error_message') . validation_errors(); 
            $check_pixel_name = $this->MPixels->check_pixel_name();
            if($check_pixel_name){
            $this->return = array();
            $this->return['responseCode'] = 0;
            $this->return['responseHTML'] = $this->load->view('shared/error', array('data'=>$err), TRUE);
            die(json_encode($this->return));
            }
        }

        /** Checking for unique pixel name while update and updating the pixel information */
        if($this->input->post('field_id') != ''){
            $check_for_update = $this->MPixels->check_pixel_name_update($this->input->post('field_id'));
            if($check_for_update){
                $update_response = $this->MPixels->update();

                $success = [];
                $success['header']  = $this->lang->line('smart_pixel_create_success_heading');
                $success['content'] = $this->lang->line('smart_pixel_update_success_message'); 
                $this->return = array();
                $this->return['responseCode'] = 1;
                $this->return['responseHTML'] = $this->load->view('shared/success', array('data' => $success), TRUE);
                die(json_encode($this->return));
            }else{
                $err = [];
                $err['header']  = $this->lang->line('smart_pixel_create_validation_error_heading');
                $err['content'] = $this->lang->line('smart_pixel_create_pixel_name_validation_error_message') . validation_errors(); 
                $this->return = array();
                $this->return['responseCode'] = 0;
                $this->return['responseHTML'] = $this->load->view('shared/error', array('data'=>$err), TRUE);
                die(json_encode($this->return));
            }
        }

        /** Function to insert the smart pixel in the pixels table */
        $pixel_id = $this->MPixels->insert();
        if($pixel_id){
        $success = [];
        $success['header']  = $this->lang->line('smart_pixel_create_success_heading');
        $success['content'] = $this->lang->line('smart_pixel_create_success_message'); 
        $this->return = array();
        $this->return['responseCode'] = 1;
        $this->return['responseHTML'] = $this->load->view('shared/success', array('data' => $success), TRUE);
        die(json_encode($this->return));
        }
    }


Теперь в моем Модале я сделал эти функции для проверки, я сделал функцию


/**
     * Check the pixel name exist in table or not while create
     * @return boolean 
     */
    public function check_pixel_name()
    {
        $this->db->where('user_id', $this->session->userdata('user_id'));
        $this->db->where('user_pixel_name', $this->input->post('pixel_name'));
        $q = $this->db->get('pixels');

        if($q->num_rows() > 0){
            return TRUE;
        }else{
            return FALSE;
        }
    }

    /**
     * Check the pixel name exist in table or not while update
     * @param id
     * @return boolean 
     */
    public function check_pixel_name_update($id = ''){
        $this->db->where('id', $id);    
        $this->db->where('user_id', $this->session->userdata('user_id'));
        $this->db->where('user_pixel_name', $this->input->post('pixel_name'));
        $q = $this->db->get('pixels');
        if($q->num_rows() > 0){
            return TRUE;
        }else{
            /** check for unique pixel name  */
            $this->db->where('user_id', $this->session->userdata('user_id'));
            $this->db->where('user_pixel_name', $this->input->post('pixel_name'));
            $q = $this->db->get('pixels');

            if($q->num_rows() > 0){
                return FALSE;
            }else{
                return TRUE;
            }
        }
    }

Все эти функции работают, но Как сделать это с помощью функций проверки формы CI.

Спасибо, что ответили на вопрос. Надеюсь, у тебя все хорошо в это время зайцев.

...