2011-08-18 77 views
0

CodeIgniter和Ocular的新手都很適合我。Codeigniter Ocular和表單驗證

我用運行表單驗證(其中指數()方法中包含的形式加載代碼)時,按以下方式進行編碼:

if ($this->form_validation->run() == FALSE) 
{ 
    $this->index(); 
} 
else 
{ 
    $this->load->view('view_name', $data); 
} 

不過我現在試圖用眼模板庫和上面的代碼不再工作見下面的例子:

if ($this->form_validation->run() == FALSE) 
{ 
    $this->index(); 
} 
else 
{ 
    $this->template->set($data); 
    $this->template->render(); 
} 

上面的代碼不通過索引方式運行,因爲它使用無眼,我想知道最好的做法是,或者即使我以前來糾正這個是什麼代碼是最佳實踐?

問候, Numan1617

回答

1

有時很難確定最佳實踐與笨,因爲它的慣例不太自然,因此,所有我真的可以告訴你的是我是如何找到最好的在我的經驗...

我認爲你的表單視圖是通過index()提供的,然後你將表單提交給這個(單獨的)控制器函數來驗證和處理表單數據,並且如果有錯誤的話,重新顯示錯誤的視圖問題...

我會清理這個由consoli約會所有到一個單一的功能...

public function form() 
{ 
    //if this is a post request 
    if ($_POST) 
    { 
     //check the validation 
     if ($this->form_validation->run()) 
     { 
      //process it 
      //and then redirect if you want to send to a "success" page 
      redirect('uri/to/success'); 
     } 
     else 
     { 
      //load up $data values to re-display form 
     } 
    } 

    //load up any $data values needed for standard view 
    $this->load->view('view', $data); 
    // or $this->template stuff... 

} 

它總是在我看來,這是一個壞的路線走下來呼叫控制器功能的內部,恩。 $this->index()

+0

表單驗證應在模型中進行,而不是控制器 – iaintunderstand

+0

@iaintunderstand這是一個相當流行的** **的意見,這些天。 – jondavidjohn