我有一个包含表单的视图,单击保存按钮后,如果需要字段但用户未提供,我希望它返回到视图页面以显示错误消息。
我该怎么做?表单页面的url看起来像这个'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>
您需要对打印错误使用validation_errors函数,并需要使用数据变量传递视图。参见示例if(validation_errors()){
$data['error'] = validation_errors() ;;
} else {
$data['error'] = '';
}