我必須在用戶提交表單後在多個表中輸入數據。我已經爲此創建了窗體和控制器。我的問題是我如何處理錯誤。假設有一些錯誤,如某個表中的數據因爲某些原因未被插入。因此,現在我想提醒用戶特定的錯誤消息,如(「有些東西是wrong.try再次」等)。如果發生錯誤,我也必須保留表格值。Codeigniter最好的方式來處理錯誤,其實不是驗證錯誤
我有嘗試以下操作:
1)添加SET_VALUE在表單字段保留表單值
2)加入會話閃光消息(不過,以顯示它,我必須刷新不保留會話頁面。值)
這裏是我的代碼:
Controller文件:
public function add_new()
{
$this->load->helper('form');
$this->load->library('form_validation');
if($this->input->post())
{
if($this->input->post('add') !== false)
{
$post_val = $this->input->post();
//Set validation rules
$this->form_validation->set_rules('field1', 'field1 name', 'required');
.
.
.
if($this->form_validation->run() == TRUE)
{
//Add Data into first table
$this->load->model("my_model");
$input_data['f1']=$post_val['filed1'];
$input_data['f2']=$post_val['filed2'];
$insert_fid =$this->my_model->insert($input_data);
//check whether data inserted into 1st table or not.
if($insert_fid)
{
//successfully inserted
//write into file(I have to write into file(for some requirement))
$this->load->helper('file');
$data = "\r\n".$input_data['f1'].".";
if (! write_file(WRITE_FILE, $data,'a+')) //WRITE_FILE is constant
{
//suppose file not writen then I have to alert user(Only to inform)
$this->session->set_flashdata('error',"Data no written.");
}
//Insert Into second table
$input_data=array();
$input_data['f1']=$post_val['field3'];
$input_data['f2']=$post_val['field4'];
$input_data['f3']=insert_fid;
$this->load->model("my_model_two");
$insert_sid =$this->my_model_two->insert($input_data);
//same procedure for checking
if($insert_sid)
{
//Insert Into Third table
$input_data=array();
$input_data['f1']=$post_val['field5'];
$input_data['f2']=$post_val['field6'];
$input_data['f3']=insert_sid;
$this->load->model("my_model_three");
$insert_tid =$this->my_model_three->insert($input_data);
//Insert Into Fourth table
$input_data=array();
$input_data['f1']=$post_val['field7'];
$input_data['f2']=insert_sid;
$this->load->model("my_model_four");
$insert_aid =$this->my_model_four->insert($input_data);
if(!$insert_tid)
{
$this->session->set_flashdata('error',"Error mesage 1");
//redirect(base_url(uri_string()));
}
elseif(!$insert_aid)
{
$this->session->set_flashdata('error',"Error mesage 2");
//redirect(base_url(uri_string()));
}
else
{
//ok all done successfully now I can redirect user to list page
$this->session->set_flashdata('message',"success message");
redirect(base_url().'list/');
}
}
else
{
$this->session->set_flashdata('error',"Error Message");
//redirect(base_url(uri_string()));
}
}
else
{
$this->session->set_flashdata('error',"Error Message");
//redirect(base_url(uri_string()));
}
//redirect(base_url(uri_string()));
}
}
}
$page_data["title"]="Add Data";
$this->load->view("myview",$page_data);
}
我的視圖文件
<div class="page-content">
<?php
$success = $this->session->flashdata('message');
if(isset($success) && trim($success) != "")
{
?>
<div class="alert alert-block alert-success" id="success-alert">
<button type="button" class="close" data-dismiss="alert">
<i class="ace-icon fa fa-times"></i>
</button>
<p>
<?= $success ?>
</p>
</div>
<?php
}
$error = $this->session->flashdata('error');
$validate_error = validation_errors();
if((isset($error) && trim($error) != "") || trim($validate_error) != "")
{
?>
<div class="alert alert-block alert-danger">
<button type="button" class="close" data-dismiss="alert">
<i class="ace-icon fa fa-times"></i>
</button>
<p>
<?= $error ?>
<?= $validate_error ?>
</p>
</div>
<?php
}
?>
<div class="page-header">
<h1 class=""><?=$title?>
</h1>
</div>
<form>
//My form Which sucessfully created
</form>
</div> <!-- page-contnet -->
我想顯示錯誤(預期驗證錯誤),也希望從值保留。
是@ cssBlaster21895。我也在考慮將相同的解決方案添加到$ page_data ['errors']中。但爲此,我不得不再次考慮顯示錯誤。所以我想如果有更好的解決方案。感謝您的答覆。 –
不客氣。 – cssBlaster21895