0
votes

I have to enter data in multiple table after User submit the form. I created form and controller for this already. My question is how can I handle error. Suppose there is some error like in one table data not inserted because of some reason. SO now I want to alert user with specific error message like ("Something is wrong.try again" etc.)Also I have to retain form value if error occurs.

I have try following:

1) Add set_value in form fields to retain form values

2) Add session flash message.(But to display it I have to refresh page which does not retain session value)

Here is my code:

Controller file:

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);
}

My view file

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

I want to display errors(expect validation error) And also want to retain from value.

1

1 Answers

1
votes

set_value() uses values validated by form validation library, so use the rules where you can. It also searches $_POST if there are no validation rules, so anyway it should work out of the box. And the first option is to set value by yourself set_value('my_field', $this->input->post('my_field'), true); Third parameter is for html escaping, true by default. You can set it to false, it's useful when you have rich html from wyswig textarea.

Speaking about error, you don't have to use flash data and redirect, you can put array to your $page_data array and it will appear inside view. Something like $page_data['errors'] = []; $page_data['errors']['first_field'] = 'We have a bad situation here'; should do the trick