is_unique用于codeigniter表单验证 [英] is_unique for codeigniter form validation
问题描述
我想知道如何在下面情况下使用Codeigniter表单验证库中的 is_unique
规则。
I'm trying to figure out how I can use the is_unique
rule from the Codeigniter form validation library in the following situation.
我试图提交编辑用户表单,并且具有以下规则:
I'm trying to submit a edit user form and have the rule:
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|is_unique[users.user_name]');
如果表单中的其他值正在更改,但此值保持不变,该怎么办?
What if other values in the form are being changed but this value stays the same. The form is going to see that this value already exists so how would I protect it from editing if this value isn't changed.
推荐答案
为了确保它只在用户提交新值时运行,可以检查发布值 $ this-> input-> post('user_name')
与您从数据库中提取的值填充表单。如果它们相同,则不验证is_unique;
To make sure it runs only when the user submits a new value, you could check the posted value $this->input->post('user_name')
against the value you pulled from the database to populate your form with. If they are the same, don't validate is_unique;
if($this->input->post('user_name') != $original_value) {
$is_unique = '|is_unique[users.user_name]'
} else {
$is_unique = ''
}
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean'.$is_unique);
这篇关于is_unique用于codeigniter表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!