在CodeIgniter中重定向以验证错误消息

时间:2016-01-13 04:13:41

标签: php codeigniter

我有一个包含表单的视图,点击保存按钮后,我希望它返回到视图页面,以便在需要字段但用户不提供时显示错误消息。

我应该怎么做呢? 表单页面的网址如下所示'backendProduct/edit_product?item_id=11');

控制器(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);
        }
    }

查看(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>

1 个答案:

答案 0 :(得分:0)

您需要使用validation_errors funciton来处理打印错误,并且需要使用数据变量传递视图。例如 if(validation_errors()){

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