Welcome Guest, Not a member yet? Register   Sign In
is_unique extended
#1

[eluser]osci[/eluser]
I had a look at is_unique in Form_validaton and while I liked it it lacked the support for validating on edit/update. So I copied it and extended it.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {
  
   public function is_unique($str, $field)
   {
      if (substr_count($field, '.')==3)
      {
         list($table,$field,$id_field,$id_val) = explode('.', $field);
         $query = $this->CI->db->limit(1)->where($field,$str)->where($id_field.' != ',$id_val)->get($table);
      } else {
         list($table, $field)=explode('.', $field);
         $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
      }
      
      return $query->num_rows() === 0;
    }
  
}
// END MY Form Validation Class

/* End of file MY_Form_validation.php */
/* Location: ./application/libraries/MY_Form_validation.php */

If you want to validate on create you do as before.
Code:
$this->form_validation->set_rules('username', 'User', 'is_unique[t_users.username]');

for update you need to pass your unique field name and current value
for example for table t_users, username for search, id unique key, $id current unique key value.
Code:
$this->form_validation->set_rules('username', 'User', 'is_unique[t_users.username.id.'.$id.']');

Hope it helps.


Messages In This Thread
is_unique extended - by El Forum - 06-23-2011, 04:20 PM
is_unique extended - by El Forum - 03-08-2012, 06:50 AM
is_unique extended - by El Forum - 03-08-2012, 09:35 AM
is_unique extended - by El Forum - 03-10-2012, 03:53 PM
is_unique extended - by El Forum - 03-13-2012, 10:30 PM



Theme © iAndrew 2016 - Forum software by © MyBB