Advertisement
Guest User

Extending CodeIgniter’s Validation To Check For Unique

a guest
May 7th, 2011
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * MY_Form_validation Class
  4.  *
  5.  * Extends Form_Validation library
  6.  *
  7.  * Adds one validation rule, "unique" and accepts a
  8.  * parameter, the name of the table and column that
  9.  * you are checking, specified in the forum table.column
  10.  *
  11.  * Note that this update should be used with the
  12.  * form_validation library introduced in CI 2.0.2
  13.  */
  14.  
  15. class MY_Form_validation extends CI_Form_validation {
  16.  
  17.     function __construct($config)
  18.     {
  19.         parent::__construct($config);
  20.     }
  21.  
  22.     // --------------------------------------------------------------------
  23.  
  24.     /**
  25.      * Unique
  26.      *
  27.      * @access  public
  28.      * @param   string
  29.      * @param   field
  30.      * @return  bool
  31.      */
  32. function unique($str,$field)
  33.     {
  34.         $CI =& get_instance();
  35.         $CI->load->database();        
  36.         list($table, $column) = split("\.", $field, 2);
  37.        
  38.         $CI->form_validation->set_message('unique', 'The %s that you requested is unavailable.');
  39.                
  40.         $query = $CI->db->query("SELECT $column FROM $table WHERE $column = '$str' limit 1");
  41.         return ($query->num_rows() > 0) ? FALSE : TRUE;
  42.        
  43. /**
  44.  *      $query = $CI->db->query("SELECT COUNT(*) AS dupe FROM $table WHERE $column = '$str'");
  45.  *      $row = $query->row();
  46.  *      return ($row->dupe > 0) ? FALSE : TRUE;
  47.  */
  48.     }  
  49. }
  50. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement