I'm trying to figure out how I can use the is_unique
rule from the Codeigniter form validation library in the following situation.
我想弄清楚如何在以下情况下使用Codeigniter表单验证库中的is_unique规则。
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.
如果表单中的其他值被更改但该值保持不变,该怎么办?表单将会看到此值已经存在,因此如果不更改此值,我将如何保护它不被编辑。
7 个解决方案
#1
32
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.
使用您的代码作为示例,is_unique验证规则通过在您的users数据库表中查找名为user_name的字段来工作。如果存在具有相同值的字段,则验证为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);
#2
7
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.']');
#3
1
$something = $this->input->post('something');
$this->form->validation->set_rules('something','Something','xss_clean|is_unique['tbl'.users]');
if($this->form_validation->run()== FALSE){
}
#4
0
Here's an easy method that worked for me and uses well documented code (thanks to https://github.com/ivantcholakov for 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
- 下载https://github.com/ivantcholakov/starter-public-edition-3/blob/master/platform/application/libraries/MY_Form_validation.php(MIT许可)并将其保存到application \ libraries \ MY_Form_validation.php的应用程序中
-
Delete these two lines from __construct():
从__construct()中删除这两行:
$this->CI->load->helper('checkbox'); $this->CI->load->helper('email');
$这个 - > CI->负载>帮手( '复选框'); $这个 - > CI->负载>帮手( '电子邮件');
-
Delete all the functions except __construct() and unique().
删除除__construct()和unique()之外的所有函数。
-
At the end of the __construct() method of your controller add this line:
在控制器的__construct()方法的末尾添加以下行:
$this->load->library('form_validation');
$这 - >负载>库( '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):
根据unique()方法的文档,更新验证规则以添加这样的“唯一”规则(例如,如果您已经有必需和修剪规则):
…|required|unique[tablename.fieldname,tablename.(primaryKey-used-for-updates)]|trim...
... |需要| [tablename.fieldname,表名(PrimaryKey的使用的换更新)]独特|修剪...
#5
0
Extend Form_validation.php library create class inside of application/libraries file name MY_Form_validation.php
在应用程序/库文件名MY_Form_validation.php中扩展Form_validation.php库create class
<?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是users表的主键,$ id是id的值。现在,您可以在任何控制器中使用此is_unique_update验证。
#6
0
we must have to add table name for is_unique
我们必须为is_unique添加表名
for Exp.
为Exp。
is_unique[users.email]
#7
0
Simple Way
简单的方法
Just Change isset to is_object in system/libraries/form_validation.php
只需将更改设置为system / libraries / form_validation.php中的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;
}
#1
32
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.
使用您的代码作为示例,is_unique验证规则通过在您的users数据库表中查找名为user_name的字段来工作。如果存在具有相同值的字段,则验证为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);
#2
7
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.']');
#3
1
$something = $this->input->post('something');
$this->form->validation->set_rules('something','Something','xss_clean|is_unique['tbl'.users]');
if($this->form_validation->run()== FALSE){
}
#4
0
Here's an easy method that worked for me and uses well documented code (thanks to https://github.com/ivantcholakov for 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
- 下载https://github.com/ivantcholakov/starter-public-edition-3/blob/master/platform/application/libraries/MY_Form_validation.php(MIT许可)并将其保存到application \ libraries \ MY_Form_validation.php的应用程序中
-
Delete these two lines from __construct():
从__construct()中删除这两行:
$this->CI->load->helper('checkbox'); $this->CI->load->helper('email');
$这个 - > CI->负载>帮手( '复选框'); $这个 - > CI->负载>帮手( '电子邮件');
-
Delete all the functions except __construct() and unique().
删除除__construct()和unique()之外的所有函数。
-
At the end of the __construct() method of your controller add this line:
在控制器的__construct()方法的末尾添加以下行:
$this->load->library('form_validation');
$这 - >负载>库( '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):
根据unique()方法的文档,更新验证规则以添加这样的“唯一”规则(例如,如果您已经有必需和修剪规则):
…|required|unique[tablename.fieldname,tablename.(primaryKey-used-for-updates)]|trim...
... |需要| [tablename.fieldname,表名(PrimaryKey的使用的换更新)]独特|修剪...
#5
0
Extend Form_validation.php library create class inside of application/libraries file name MY_Form_validation.php
在应用程序/库文件名MY_Form_validation.php中扩展Form_validation.php库create class
<?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是users表的主键,$ id是id的值。现在,您可以在任何控制器中使用此is_unique_update验证。
#6
0
we must have to add table name for is_unique
我们必须为is_unique添加表名
for Exp.
为Exp。
is_unique[users.email]
#7
0
Simple Way
简单的方法
Just Change isset to is_object in system/libraries/form_validation.php
只需将更改设置为system / libraries / form_validation.php中的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;
}