2012-10-09 36 views
7

可能重複:
Codeigniter 2 forms on one page, validation_errors problem多個表單驗證笨衝突

我有我的第2種形式。我需要一次驗證1個,但我認爲存在衝突。下面一起來看看:

enter image description here

當我提交表單的任一,他們都表現出同樣的錯誤消息

我用validation_errors()顯示的郵件。我怎樣才能一次驗證表單1?

下面是代碼

public function update_user_info(){ 
    $this->form_validation->set_rules("firstname","First Name","required");  
    $this->form_validation->set_rules("lastname","Last Name","required"); 
    $this->form_validation->set_rules("middlename","Middle Name","required"); 
    if($this->form_validation->run()===false){ 
     //wrong 
    } 
    else { //correct } 
} 
+0

發佈您的代碼... –

+2

'http:// stackoverflow。com/questions/5802729/codeigniter-2-forms-on-page-validation-errors-problem' –

+0

public function update_user_info(){ \t \t $ this-> form_validation-> set_rules(「firstname」,「First Name 「,「需要」); \t \t $ this-> form_validation-> set_rules(「lastname」,「Last Name」,「required」); \t \t $ this-> form_validation-> set_rules(「middlename」,「Middle Name」,「required」); \t \t如果($這個 - > form_validation->的run()===假){ \t \t \t //錯誤 \t \t}其他{ \t \t \t //正確 \t \t} \t} –

回答

7

我絕對t遇到了這個問題。 我的解決辦法是:

1.首先設置第一個提交按鈕的名稱= 'update_info'

2.Secondly設置第二個提交按鈕的名稱= 'change_password'

3.Last更改update_user_info( )。

public function update_user_info(){ 
    if (isset ($_POST['update_info'])) { 
     $this->form_validation->set_rules("firstname","First Name","required");  
     $this->form_validation->set_rules("lastname","Last Name","required"); 
     $this->form_validation->set_rules("middlename","Middle Name","required"); 
     if($this->form_validation->run()===false){ 
      //wrong 
     } 
     else { //correct }    
    } 
    else if (isset ($_POST['change_password'])){ 
     form_validation of your change password 
    } 

我認爲這是解決您的問題最簡單的方法。

祝你好運。

+0

謝謝! :)它的作品就像一個魅力! –

+0

我的榮幸。 :) – lijinma

2

如果你有不同的驗證錯誤爲每個表單可以從validation_errors檢查輸出。

據我所見validation_errors只允許您更改錯誤的分隔符而不是其他。但是,你可以嘗試出個人形式的錯誤,像這樣:<?php echo form_error('username'); ?>

5

你可以把一個隱藏的輸入每種形式

First Form: 
<input type="hidden" name="form" value="form1" /> 

Second Form: 
<input type="hidden" name="form" value="form2" /> 

在你的控制器,你可以設置的規則陣列爲每個表單

$config['form1'] = array(
       array(
        'field' => 'username', 
        'label' => 'Username', 
        'rules' => 'required' 
       ), 
       array(
        'field' => 'password', 
        'label' => 'Password', 
        'rules' => 'required' 
       ), 
      ); 

$config['form2'] = array(
       array(
        'field' => 'email', 
        'label' => 'Email', 
        'rules' => 'required' 
       ), 
      ); 

Now check which hidden field posted 

$form = $this->input->post('form') 


Now you can set rules as below 

$this->form_validation->set_rules($config[$form]); 

if ($this->form_validation->run()): 

    // process form 

else: 
     $data[$form.'_errors'] = validation_errors(); 
endif; 

現在,在您的視圖文件

if (isset($form1_errors)) echo $form1_errors; 
if (isset($form2_errors)) echo $form2_errors;