首页 > 解决方案 > 如何使用 form_validation->set_rules() 在 Codeigniter 中创建 is_unique 验证,这将在更新时起作用?

问题描述

如何在 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 表单验证功能来实现。

感谢您回答这个问题。希望你在这个野兔时期做得很好。

标签: phpcodeignitercontinuous-integration

解决方案


推荐阅读