22

我试图弄清楚如何is_unique在以下情况下使用 Codeigniter 表单验证库中的规则。

我正在尝试提交编辑用户表单并具有以下规则:

$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|is_unique[users.user_name]');

如果表单中的其他值正在更改但该值保持不变怎么办。表单将看到这个值已经存在,所以如果这个值没有改变,我将如何保护它不被编辑。

4

10 回答 10

40

以您的代码为例,is_unique验证规则通过查找数据库表中调用user_name的字段来工作。users如果存在具有相同值的字段,则验证为 false。

为确保它仅在用户提交新值时运行,您可以将发布 $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);
于 2012-12-04T01:35:23.367 回答
9

我认为有一个更好的方法可以绕过它,仍然使用 CodeIgniters 的验证库...使用 edit_unique 传递一个额外参数,即您正在编辑的行的 id ..见下文..我使用它和对我来说效果很好..希望它有所帮助

$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');
于 2014-05-21T09:39:51.937 回答
3
$something = $this->input->post('something');

$this->form->validation->set_rules('something','Something','xss_clean|is_unique['tbl'.users]');

if($this->form_validation->run()== FALSE){

}
于 2016-07-12T15:54:19.337 回答
2

简单的方法

只需在 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;
}
于 2018-06-27T11:24:54.977 回答
0

这是一个对我有用的简单方法,它使用了有据可查的代码(感谢https://github.com/ivantcholakov的分享!)。我发现它在https://github.com/bcit-ci/CodeIgniter/issues/3109#issuecomment-46346280

  1. 下载https://github.com/ivantcholakov/starter-public-edition-3/blob/master/platform/application/libraries/MY_Form_validation.php(MIT许可)并将其保存到您的应用程序 application\libraries\MY_Form_validation.php
  2. 从 __construct() 中删除这两行:

    $this->CI->load->helper('checkbox'); $this->CI->load->helper('email');

  3. 删除除 __construct() 和 unique() 之外的所有函数。

  4. 在控制器的 __construct() 方法的末尾添加以下行:

    $this->load->library('form_validation');

  5. 根据 unique() 方法的文档,更新您的验证规则以添加这样的“唯一”规则(例如,如果您已经有 required 和 trim 规则):

    …|必需|唯一[tablename.fieldname,tablename.(primaryKey-used-for-updates)]|trim...

于 2015-09-01T21:06:58.537 回答
0

扩展 Form_validation.php 库在应用程序/库文件名 MY_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;
                    }


            }
}  

现在在你的控制器中

$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|is_unique_update[users.user_name@id@'.$id.']');  

"@" 我用来分解字符串
,其中 id 是用户表的主键,
$id 是 id 的值。现在您可以在任何控制器中使用这个 is_unique_update 验证。

于 2016-05-02T07:01:13.653 回答
0

这个问题很老,但也许一些新人会遇到这个问题,这就是它的解决方案。我敢打赌您正在使用模块化扩展 (HMVC),并且您已经创建了一个新库 MY_Form_validation。你为回调做了 id ,所以你的类上有这行代码以便使用回调:

$this->form_validation->CI =& $this;

好吧,解决这个问题的方法是,每当你想使用“is_unique”时,你必须删除这行代码“$this->form_validation->CI =& $this;” 从课堂上。我遇到过这个问题,我用这种方式修复它,现在它工作正常。

如果您真的想使用回调“$this->form_validation->CI =& $this;”,那么只在您不想使用 is_unique 的所需“方法”/“函数”上执行此操作。

于 2018-09-20T09:53:36.777 回答
0

此代码有助于创建和更新函数的唯一验证...

在控制器中

在创建和更新函数中添加此表单验证代码

$this->form_validation->set_rules('order_no', 'Order no', 'required|callback_check_order_no');

在控制器中添加此回调函数

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;
    }

在模型中

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();
    }
于 2019-05-13T06:55:10.667 回答
0

我正在使用 codeigniter3,当我在更新值时检查用户名时它显示错误,is_unique 不适用于更新场景,因此使用@Anthony Mutisya 的答案,这是完整的解决方案

在您的控制器中,在使用数据库验证当前用户的用户名时添加此行

$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');

你可以$id从你提交的表格中得到它。

现在,将以下函数添加/system/libraries/Form_Validation.php到此文件中。System文件夹位于 CodeIgniter3 文件夹的根目录中。

/**
* edit_unique // for check on update value
*
* Check if the input value doesn't already exist
* in the specified database field.
*
* @param    string  $str
* @param    string  $field
* @return   bool
*/
function edit_unique($value, $params)  {
    $CI =& get_instance();
    $CI->load->database();
    $CI->form_validation->set_message('edit_unique', "Sorry, that %s is already being used.");
    
    list($table, $field, $current_id) = explode(".", $params);
    
    $query = $CI->db->select()->from($table)->where($field, $value)->limit(1)->get();
    
    if ($query->row() && $query->row()->id != $current_id)
    {
        return FALSE;
    } else {
        return TRUE;
    }
}

在我的情况下它工作得很好

于 2021-06-18T06:17:59.920 回答
-2

我们必须为is_unique添加表名

为 Exp。

 is_unique[users.email]
于 2016-08-17T11:56:46.020 回答