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.
推荐答案
以您的代码为例,is_unique
验证规则通过查找名为 user_name
的字段起作用在您的 users
数据库表中.如果存在具有相同值的字段,则验证为 false.
Using your code as an example, the is_unique
validation rule works by looking for a field called user_name
in your users
database table. If the field with the same value exists it validates as false.
为了确保它仅在用户提交新值时运行,您可以根据您拉取的值检查发布的值 $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屋!