2012-02-17 65 views
0

錯誤這是我的控制器:無法顯示在我的形式笨

public function Index() 
{ 
    $this->load->model('InsertData'); 
     $this->InsertData-> validateField(); 
    $this->InsertData->DispalyForm(); 

} 

這是我的模型:

 class InsertData extends CI_Model 
{ 
    function __construct() { 
     parent::__construct(); 
     $this->load->helper('form'); 
    } 

    function DispalyForm() 
    { 
      $dataI = array(
       'name'  => 'topic', 
       'id'   => 'url', 
       'value'  => set_value('username'), 
       'maxlength' => '100', 
       'size'  => '50', 
       'style'  => 'yellow', 
       ); 

      $dataS=array(
       'name' => 'submit', 
       'value'=>'create topic' 
      ); 

      $data['submit'] = form_submit($dataS); 
      $data['textbox']= form_input($dataI); 
      $this->load->view('admin',$data); 
    } 

    function validateField() 
    { 
     $this->load->library('form_validation'); 
     $rules['topic']='required'; 
     $this->form_validation->set_rules($rules); 
     $this->form_validation->run(); 


    } 
} 

的問題是每當我提交表單和字段爲空什麼都不顯示。

我在我的視圖頂部的一行:

 <?php echo validation_errors(); ?> 

哪裏是我的代碼wrong..why whenver我提交表單顯示沒有錯誤!?

回答

0

使用數組設置驗證規則時,數組必須是多維的。

規則數組,其中每個規則是一個標準數組。

請參閱this manual page RE此問題。

$rules = array(
    array(
     'field' => 'topic', 
     'label' => 'Topic', 
     'rules' => 'required' 
    ) 
); 

$this->form_validation->set_rules($rules); 
+0

謝謝..幫助 – BlackFire27 2012-02-17 19:00:51

+0

沒問題。不要忘記[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work):) – 2012-02-17 21:49:53