php is_unique 用于 codeigniter 表单验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13692473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is_unique for codeigniter form validation
提问by Kevin Smith
I'm trying to figure out how I can use the is_uniquerule from the Codeigniter form validation library in the following situation.
我试图弄清楚如何is_unique在以下情况下使用Codeigniter 表单验证库中的规则。
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.
如果表单中的其他值正在更改但此值保持不变,该怎么办。表单将看到此值已存在,因此如果此值未更改,我将如何保护它不被编辑。
回答by Jeemusu
Using your code as an example, the is_uniquevalidation rule works by looking for a field called user_namein your usersdatabase table. If the field with the same value exists it validates as false.
使用您的代码为例,该is_unique验证规则的工作原理是寻找一个叫做场user_name在你的users数据库表。如果存在具有相同值的字段,则验证为 false。
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;
为确保它仅在用户提交新值时运行,您可以$this->input->post('user_name')根据从数据库中提取的值来检查发布的值 以填充表单。如果它们相同,则不验证 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);
回答by Anthony Mutisya
There's a better way to go around it, I think, still using CodeIgniters' validation library... Use edit_unique where you pass an extra parameter which is the id of the row you're editing.. See below.. I use it and works pretty fine for me.. hope it helps
我认为有一个更好的方法来解决它,我认为,仍然使用 CodeIgniters 的验证库......使用 edit_unique 在那里你传递一个额外的参数,它是你正在编辑的行的 id..见下文..我使用它和对我来说很好用..希望它有帮助
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');
回答by user6580356
$something = $this->input->post('something');
$this->form->validation->set_rules('something','Something','xss_clean|is_unique['tbl'.users]');
if($this->form_validation->run()== FALSE){
}
回答by Nirav Rathod
Simple Way
简单的方法
Just Change isset to is_object in system/libraries/form_validation.php
只需将 system/libraries/form_validation.php 中的 isset 更改为 is_object
public function is_unique($str, $field)
{
sscanf($field, '%[^.].%[^.]', $table, $field);
return is_object($this->CI->db) //default isset
? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}
回答by xttrust
This question is very old but maybe some new people experience this problem and this is the solution for it. I bet your are using Modular Extensions (HMVC) and you have created a new library, MY_Form_validation. You did id for callbacks, so you have this line of code on your class in order to use callbacks:
这个问题很老了,但也许有些新人会遇到这个问题,这就是解决方案。我敢打赌您正在使用模块化扩展 (HMVC),并且您已经创建了一个新库 MY_Form_validation。你为回调做了 id,所以你在你的类中有这行代码以便使用回调:
$this->form_validation->CI =& $this;
Well, the solution to this is whenever you want to use "is_unique" you must delete this line of code "$this->form_validation->CI =& $this;" from the class. I have experienced this problem and i fix it this way, it works fine now.
嗯,解决这个问题的方法是每当你想使用“is_unique”时,你必须删除这行代码“$this->form_validation->CI =& $this;” 从班级。我遇到过这个问题,我是这样解决的,现在工作正常。
If you realy want to use callbacks "$this->form_validation->CI =& $this;", then do it only on required "methods" / "functions" where you don't want to use is_unique.
如果您真的想使用回调“$this->form_validation->CI =& $this;”,那么仅在您不想使用 is_unique 的所需“方法”/“函数”上执行此操作。
回答by Mani
This code helpful for unique validation to create and update function...
此代码有助于创建和更新功能的唯一验证...
In controller
在控制器中
Add this form validation code in both create and update function
在创建和更新功能中添加此表单验证代码
$this->form_validation->set_rules('order_no', 'Order no', 'required|callback_check_order_no');
Add this call back function in controller
在控制器中添加此回调函数
function check_order_no($order_no) {
if($this->input->post('id'))
$id = $this->input->post('id');
else
$id = '';
$result = $this->Data_model->check_unique_order_no($id, $order_no);
if($result == 0)
$response = true;
else {
$this->form_validation->set_message('check_order_no', 'Order no already exist');
$response = false;
}
return $response;
}
In model
在模型中
function check_unique_order_no($id = '', $order_no) {
$this->db->where('order_no', $order_no);
$this->db->where('status', "A");
if($id) {
$this->db->where_not_in('id', $id);
}
return $this->db->get('delivery_order')->num_rows();
}
回答by Elijah Lofgren
Here's an easy method that worked for me and uses well documented code (thanks to https://github.com/ivantcholakovfor sharing it!). I found it referenced at https://github.com/bcit-ci/CodeIgniter/issues/3109#issuecomment-46346280
这是一个对我有用的简单方法,并使用了记录良好的代码(感谢https://github.com/ivantcholakov分享它!)。我发现它在https://github.com/bcit-ci/CodeIgniter/issues/3109#issuecomment-46346280 中被引用
- Download https://github.com/ivantcholakov/starter-public-edition-3/blob/master/platform/application/libraries/MY_Form_validation.php(MIT licensed) and save it to your application at application\libraries\MY_Form_validation.php
Delete these two lines from __construct():
$this->CI->load->helper('checkbox'); $this->CI->load->helper('email');
Delete all the functions except __construct() and unique().
At the end of the __construct() method of your controller add this line:
$this->load->library('form_validation');
As per the documentation of the unique() method update your validation rule to add a "unique" rule like this (e.g. if you already have required and trim rules):
…|required|unique[tablename.fieldname,tablename.(primaryKey-used-for-updates)]|trim...
- 下载https://github.com/ivantcholakov/starter-public-edition-3/blob/master/platform/application/libraries/MY_Form_validation.php(MIT许可)并将其保存到您的应用程序中的 application\libraries\MY_Form_validation.php
从 __construct() 中删除这两行:
$this->CI->load->helper('checkbox'); $this->CI->load->helper('email');
删除除 __construct() 和 unique() 之外的所有函数。
在控制器的 __construct() 方法的末尾添加以下行:
$this->load->library('form_validation');
根据 unique() 方法的文档更新您的验证规则以添加这样的“唯一”规则(例如,如果您已经有 required 和 trim 规则):
...|必需|唯一[表名.字段名,表名.(primaryKey-used-for-updates)]|修剪...
回答by daulat
Extend Form_validation.php library create class inside of application/libraries file name MY_Form_validation.php
在应用程序/库文件名 MY_Form_validation.php 中扩展 Form_validation.php 库创建类
<?php
class MY_Form_validation extends CI_Form_validation{
protected $ci;
public function __construct($config = array()){
parent::__construct($config);
$this->ci =& get_instance();
}
public function is_unique_update($str, $field){
$explode=explode('@', $field);
$field_name=$explode['0'];
$field_id_key=$explode['1'];
$field_id_value=$explode['2'];
sscanf($field_name, '%[^.].%[^.]', $table, $field_name);
if(isset($this->ci->db)){
if($this->ci->db->limit(1)->get_where($table, array($field_name => $str,$field_id_key=>$field_id_value))->num_rows() === 0){
$this->ci->form_validation->set_message('is_unique_update', 'The {field} field must contain a unique value.');
return false;
}
return true;
}
}
}
Now in your controller
现在在你的控制器中
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|is_unique_update[users.user_name@id@'.$id.']');
"@" I used for explode the string
where id is primary key of users table
and $id is the value of id.
Now you can use this is_unique_update validation in any controller.
“@”我用于分解字符串
,其中 id 是用户表的主键,
$id 是 id 的值。现在您可以在任何控制器中使用此 is_unique_update 验证。
回答by sandeep kumar
we must have to add table name for is_unique
我们必须为is_unique添加表名
for Exp.
为Exp。
is_unique[users.email]

