2016-06-10 115 views
0

我有一個分隔文件位於applications/config/form_validation.php用於驗證。創建新用戶的兩個字段必須是唯一的,他們的身份證和電子郵件。Codeigniter驗證編輯表單時唯一字段的問題

'user' => array(
    array(
     'field' => 'email', 
     'label' => 'E-MAIL', 
     'rules' => 'required|valid_email|is_unique[usuario.email]' 
    ), 
    array(
     'field' => 'cpf_cnpj', 
     'label' => 'CPF', 
     'rules' => 'required|_validate_cpf|is_unique[usuario.cpf_cnpj]' 
    ), 
    ... 
) 

在開始的時候,我曾使用同一組驗證的編輯形式,但是我已經和這兩個領域的問題,他們總是被指責不是唯一的。

因此,我在表單中添加了一個hidden字段,用於存儲這些字段的當前值並添加另一個值,也存儲該值,但在視圖中顯示的值將是唯一要編輯的值,並且然後,在edit方法,條件會繼續檢查,如果hidden場是到正規的不同領域發生之前:

// HTML 
<input type="email" name="novoemail" class="email <?php echo form_error('novoemail') ? 'campo_aviso' : ''; ?>" value="<?php echo $u['email']; ?>"/> 
<input type="hidden" name="email" value="<?php echo $u['email']; ?>"/> 

// In `edit` 
if ($this->input->post('email') !== $this->input->post('novoemail')) { 
    $this->form_validation->set_rules('email', 'E-MAIL', 'is_unique[usuario.email]'); 
} 

if ($this->form_validation->run('edit_user')) { ... } 

如果發現差異,添加規則以使其唯一。現在的問題是,即使輸入的電子郵件不存在,它的任何區別都會失敗,並且指責它們不是唯一的。

我需要找到一種方法來編輯唯一的字段,並仍然保證唯一性。

回答

1

我不同意這種方法。如果is_unique規則失敗,則應該找出原因,而不是試圖解決它。

無論如何,你會需要在驗證規則上運行某種類型的連接回調,我想。我發現驗證數組很笨重,所以我不使用它們。請隨時適應這樣的:

$this->form_validation->set_rules('test', 'Test Post', 'trim|less_than[99]'.($this->input->post('test') === $this->input->post('test2' ? 'required' : ''))); 

有可能是一個更好的答案,也許某種方式使用內置的「匹配」的驗證規則。我把它扔在一個CI項目上,並沒有發現任何錯誤,但我也沒有在合法數據上進行測試。

+0

它失敗了,因爲在'form_validations.php'中,該版本還檢查它是否是唯一的。這是CI問題。無論如何,你的解決方案幫助了我。我編輯了一下,重用了我製作和使用的方法。非常感激。 – mfgabriel92

+0

這很棒,我很高興你能夠做出一些有用的東西。 – Joe

1

我同意這個方法。使用隱藏字段的想法可以工作,對於確定與form_validation->run()一起使用的正確規則集很有用。

這個答案堅持使用規則的配置文件,並避免直接使用set_rule()

您可以輕鬆地操作config/form_validation.php中的數組來返回您需要的數組,並且同時沒有大量的重複代碼。

考慮一下這個版本的config/form_validation.php

$check_email = array(
    'field' => 'novo_email', 
    'label' => 'E-MAIL', 
    'rules' => 'trim|required|valid_email|is_unique[usuario.email]' 
); 

$check_card = array(
    'field' => 'novo_cpf_cnpj', 
    'label' => 'CPF', 
    'rules' => 'trim|required|_validate_cpf|is_unique[usuario.cpf_cnpj]' 
); 

$config = array(
    'edit_email' => array($check_email), 
    'edit_cpf_cnpj' => array($check_card), 
    'new_user' => array($check_email, $check_card) 
); 

上面創建的規則,三套獨立,而不是重新創建的代碼結構。

只有兩種情況,您甚至需要執行字段驗證。

  1. 新用戶
  2. 一個或兩個領域已經改變

如果對於一個新用戶,你總是隱藏字段的值設置爲空字符串(或NULL)的「新用戶「狀態很容易確定。在這種情況下,您需要配置文件中的「new_user」規則。

如果隱藏的字段不是空的,您需要找出哪些更改,並根據找到的內容選擇驗證規則。

下面介紹如何在edit函數中實現該邏輯。注意:novo_*字段是用戶編輯的字段。

public function edit() 
{ 
    //instead of multiple calls to $this->input->post, 
    //capture all the inputs in one handy array 
    $posted = $this->input->post(); 

    //check for new user (true when the hidden field 'email' is blank 
    //if 'email' is blank then 'novo_cpf_cnpj' should be too 
    if(empty($posted['email'])) 
    { 
    $rules = 'new_user'; 
    } 
    else //not a new user. What has changed? 
    { 
    //note the use of trim - in case user added spaces to an otherwise unchanged field 
    $changed_email = $posted['email'] !== trim($posted['novo_email']); 
    $changed_cpf_cnpj = $posted['cpf_cnpj'] !== trim($posted['novo_cpf_cnpj']); 
    if($changed_email && $changed_cpf_cnpj) 
    { 
     //both have changed, treat like a new user 
     $rules = 'new_user'; 
    } 
    elseif($changed_email) 
    { 
     $rules = 'edit_email'; 
    } 
    elseif($changed_cpf_cnpj) 
    { 
     //only possibility left - cpf_cnpj is changed 
     $rules = 'edit_cpf_cnpj'; 
    } 
    } 

    if(! isset($rules)) 
    { 
    //Not a new user and nothing changed so validation is not needed. 
    //Behave as if successful validation occurred and redirect to successful save page 
    redirect('controller/save_success'); 
    //redirect() does not return because it always calls the PHP 
    //function exit thereby ending script execution. 
    //So, there is no need for an else to if(!isset($rules)) 
    } 

    if($this->form_validation->run($rules)) 
    { 
    //save to database then 
    redirect('controller/save_success'); 
    } 
    //whatever happens when validation fails 
} 
+0

謝謝,非常好。 – mfgabriel92