0

我有一个关于使用 Codeigniter 和 Flash Data 进行表单验证的问题。我的表单位于 URL 字符串的深处,因此我认为在页面上显示错误消息的最佳方式是使用 CI 闪存数据。我很难弄清楚如何实际显示错误消息。我不确定我哪里出错了。我哪里错了?感谢大家。

   public function form_validate_cars()
   {
     $this->load->library('form_validation');  

         $this->form_validation->set_rules("value", "<b>Value</b>", "trim|required|is_numeric|xss_clean");
         $this->form_validation->set_rules("exterior_color", "<b>Exterior Color</b>", "trim|required|xss_clean");
         $this->form_validation->set_rules("interior_color", "<b>Interior Color</b>", "trim|required|xss_clean");
         $this->form_validation->set_rules("mileage","<b>Mileage</b>","trim|required|is_numeric|xss_clean");

         if ($this->form_validation->run() == FALSE)
          {   
             // if person does not fill in correct info, they get resubmitted back to the form.
             // redirect('site/item/2332');
             // echo 'Hello World!';
             $msg = "Please fill out proper data";
             $this->session->set_flashdata('test', $msg);
             // echo 'Please fill out needed ifnormation!';
          } 
          else 
          {

            $this->load->model('model_data');
            $this->model_data->add_new_value();
            redirect('/site/thanks');

          }

   }

我的观点

   echo form_open('site/form_validate_cars');

   print_r($this->session->flashdata('test'));

   echo validation_errors();

   my form continues below ....... 
4

2 回答 2

1

$this->session->set_flashdata('test', $msg);制作redirect到所需的网址之后。

视野中

if ($this->session->flashdata('result') != ''): 
    echo $this->session->flashdata('result'); 
endif;

没有会话的更好方法

public function form_validate_cars()
   {
     $this->load->library('form_validation');  

         $this->form_validation->set_rules("value", "<b>Value</b>", "trim|required|is_numeric|xss_clean");
         $this->form_validation->set_rules("exterior_color", "<b>Exterior Color</b>", "trim|required|xss_clean");
         $this->form_validation->set_rules("interior_color", "<b>Interior Color</b>", "trim|required|xss_clean");
         $this->form_validation->set_rules("mileage","<b>Mileage</b>","trim|required|is_numeric|xss_clean");

       if ($this->form_validation->run() == FALSE)
          {   
             // if person does not fill in correct info, they get resubmitted back to the form.
             // redirect('site/item/2332');
             // echo 'Hello World!';
             $data['error'] = "Please fill out proper data";
             $this->load->view('desired_view', $data);
          } else {
            $this->load->model('model_data');
            $this->model_data->add_new_value();
            redirect('/site/thanks');

          }

   }

并且在视野中

echo (isset($error)) ? $error : "";
于 2013-06-17T21:27:50.957 回答
0

分配 flashdata 后,使用 redirect() 重定向回表单。然后这将获取闪存数据,但请确保检查它是否已填充。

于 2013-06-17T23:28:04.560 回答