Codeigniter处理错误的最佳方法,这些错误实际上不是验证错误

时间:2017-02-01 09:47:43

标签: php codeigniter session error-handling

用户提交表单后,我必须在多个表格中输入数据。我已经为此创建了表单和控制器。我的问题是如何处理错误。假设由于某种原因,在一个表数据中没有插入一些错误。所以现在我想用特定的错误消息警告用户(“Something is wrong.try again”等)。如果发生错误,我还必须保留表单值。

我试过以下:

1)在表单字段中添加set_value以保留表单值

2)添加会话flash消息。(但要显示它我必须刷新不保留会话值的页面)

这是我的代码:

控制器文件:

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 -->

我想显示错误(期望验证错误)并且还希望保留价值。

1 个答案:

答案 0 :(得分:1)

set_value()使用表单验证库验证的值,因此请尽可能使用规则。如果没有验证规则,它也会搜索$ _POST,所以无论如何它应该开箱即用。第一个选项是自己设置值set_value('my_field', $this->input->post('my_field'), true);第三个参数用于html转义,默认情况下为true。您可以将其设置为false,当您从wyswig textarea获得丰富的html时,它非常有用。

说到错误,您不必使用Flash数据和重定向,您可以将数组放入$ page_data数组,它将显示在视图中。像$page_data['errors'] = []; $page_data['errors']['first_field'] = 'We have a bad situation here';之类的东西应该可以做到这一点