2012-02-03 58 views
2

我有一個表單供用戶輸入一些反饋,並且此表單必須位於產品詳細信息頁面中。我需要在詳細信息頁面上打印出一些錯誤驗證,而不是將表單重定向到帶有驗證消息的反饋表單頁面。Codeigniter窗體顯示另一個視圖中視圖的驗證錯誤

產品詳細信息頁面位於'index.php/product/view/1',反饋表單位於'index.php/product/add_feedback'。

如何打印出錯誤表單驗證消息,以便它顯示在產品詳細信息頁面上,而不是重定向到add_feedback。謝謝。

我的控制器:

class Product extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('mproduct'); 
     $this->load->model('mfeedback'); 
    } 

    public function index() 
    { 
     //get product details 
     $data['content'] = $this->mproduct->get_details(); 
     $this->load->view('listing', $data); 
    } 

    public function add_feedback() 
    { 
     // feedback form 

     $this->form_validation->set_rules('name', 'Name', 'required|xss_clean|max_length[200]'); 
     $this->form_validation->set_rules('feedback', 'Feedback', 'required|xss_clean|max_length[200]'); 

     if ($this->form_validation->run() == FALSE) 
     { 
      $this->load->view('feedback'); 
     } 
     else 
     { 
      $pid = $this->input->post('pid'); 
      $name = $this->input->post('name'); 
      $feedback = $this->input->post('feedback'); 

      $this->MFeedback->add($pid, $name, $feedback); 
      redirect('product/view/'.$pid); 
     } 
    } 
} 

型號:

class MFeedback extends CI_Model { 
    function add_feedback($name, $pid, $feedback) 
    { 
     $data = array(
      'name' => $name, 
      'feedback' => $feedback, 
      'pid' => $pid, 
     ); 
     $this->db->insert('feedback', $data); 
    } 
} 

視圖 - feedback.php

<h1>Add Feedback</h1> 

<?php echo validation_errors(); ?> 

<?php echo form_open('product/add_feedback'); ?> 

<p>Name</p> 
<input type="text" name="name" size="50" value="<?php echo set_value('name'); ?>" /> 

<p>Feedback</p> 
<textarea type="text" name="feedback"><?php echo set_value('feedback'); ?></textarea> 

<?php echo form_hidden('pid', $this->uri->segment(3, 0)); ?> 

<div><input type="submit" value="Add Feedback" /></div> 

</form> 
+0

你有設置'$ this-> load-> library('form_validation');'在'product/view'中嗎? – 2012-02-03 09:14:17

+0

我把它設置在自動加載,所以它顯示,只是我需要錯誤驗證消息出現在產品的詳細信息頁面,而不是形式被重定向到index.php /產品/ add_feedback – 2012-02-03 09:58:08

回答

2

簡單!只需驗證添加到Product/index - 方法,像這樣:

class Product extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('mproduct'); 
     $this->load->model('mfeedback'); 
    } 

    public function index() 
    { 
     // feedback form validation 
     $this->form_validation->set_rules('name', 'Name', 'required|xss_clean|max_length[200]'); 
     $this->form_validation->set_rules('feedback', 'Feedback', 'required|xss_clean|max_length[200]'); 

     if ($this->form_validation->run() == TRUE) 
     { 
      // the validation passed, lets use the form data! 
      $pid = $this->input->post('pid'); 
      $name = $this->input->post('name'); 
      $feedback = $this->input->post('feedback'); 

      $this->MFeedback->add($pid, $name, $feedback); 
      redirect('form/success'); // redirect to a page, where the user gets a "thanks" message - or redirect to the product page, and show a thanks there (but be sure to use redirect and nocht $this->load->view(..), because then the form data would be still in the header and a reload of the page would send another mail :) 
     } 

     // form did not pass the validation, lets get and show the product details 
     $data['content'] = $this->mproduct->get_details(); 
     $this->load->view('listing', $data); 
    } 
} 

和文件中feedback.php你必須到窗體目標更改爲類似這樣:

<?php echo form_open('product/'.$this->uri->segment(3, 0)); ?> 

...甚至更好:

<?php echo form_open('product/'.$content->id); ?> 

...取決於您的產品視圖。

+0

非常感謝爲您的建議。 – 2012-02-03 14:34:53

相關問題