且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

is_unique 用于 codeigniter 表单验证

更新时间:2023-12-03 18:15:58

以您的代码为例,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);