is_unique for codeigniter form validation

2019-01-17 21:46发布

问题:

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.

回答1:

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.

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


回答2:

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

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


回答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){

}


回答4:

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

  1. 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
  2. Delete these two lines from __construct():

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

  3. Delete all the functions except __construct() and unique().

  4. At the end of the __construct() method of your controller add this line:

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

  5. 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...



回答5:

Extend Form_validation.php library create class inside of application/libraries file name 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;
                    }


            }
}  

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.



回答6:

we must have to add table name for is_unique

for Exp.

 is_unique[users.email]


回答7:

Simple Way

Just Change isset to is_object in system/libraries/form_validation.php

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


回答8:

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:

$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.

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.