2011-08-18 155 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