2016-06-10 114 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