简体   繁体   中英

Redirect in CodeIgniter for validation error message

I have a view containing a form and upon clicking on the save button, I want it to return to the view page to show the error message if a field is required but not provided by the user.

How should I go about doing it? The url of the form page look like this 'backendProduct/edit_product?item_id=11');

Controller ( product.php )

public function save_product(){

        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('information', 'Information', 'required');
        $this->form_validation->set_rules('item_id', 'Item_id', 'required');

        if($this->form_validation->run() === FALSE)
        {
            $this->load->view('layout/header');
            $data['item_id']= $this->input->post('item_id');
            $this->load->view('product/edit_product', $data);
        }
        else
        {
            $item_id = $this->input->post('item_id');
            $category_id = $this->input->post('category_id');
            header('Location: '.base_url().'edit_product?cate_code='.$category_id.'&item_id='.$item_id);
        }
    }

View ( product_form.php )

    <?php echo form_open('backendProduct/save_product'); ?>
        <div class="row"  ng-app="appcolor">
                <div class="col-md-8">
                    <div class="tabbable">
                       <ul class="nav nav-tabs">
                            <li class="active"><a href="#product_info" data-toggle="tab"><?php echo lang('details');?></a></li>
                            <?php //if there aren't any files uploaded don't offer the client the tab
                            if (count($file_list) > 0):?>
                            <li><a href="#product_downloads" data-toggle="tab"><?php echo lang('digital_content');?></a></li>
                            <?php endif;?>
                            <li><a href="#product_specification" data-toggle="tab">Specification</a></li>
                            <li><a href="#product_categories" data-toggle="tab"><?php echo lang('categories');?></a></li>
                            <li><a href="#ProductOptions" data-toggle="tab"><?php echo lang('options');?></a></li>
                            <li><a href="#product_related" data-toggle="tab"><?php echo lang('related_products');?></a></li>
                            <li><a href="#product_photos" data-toggle="tab"><?php echo lang('images');?></a></li>
                        </ul>
                    </div>
<button type="submit" class="btn btn-primary">Save</button>

        </div>

    </div>

You need to use validation_errors funciton for print error and need to pass view using data variable.see example if (validation_errors() ) {

            $data['error'] =  validation_errors() ;;
        } else {
                          $data['error'] = '';
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM