/halogy/application/libraries/MY_Form_validation.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 93 lines · 51 code · 19 blank · 23 comment · 6 complexity · befaceb241531076ba3b9d6e0cd081cc MD5 · raw file

  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 1.7.0
  13. */
  14. class MY_Form_validation extends CI_Form_validation {
  15. function My_Form_validation()
  16. {
  17. parent::CI_Form_validation();
  18. // set password error
  19. $this->set_message('matches', 'The passwords do not match.');
  20. }
  21. // --------------------------------------------------------------------
  22. /**
  23. * Unique
  24. *
  25. * @access public
  26. * @param string
  27. * @param field
  28. * @return bool
  29. */
  30. function unique($str, $field)
  31. {
  32. $CI =& get_instance();
  33. list($table, $column) = preg_split("/\./", $field, 2);
  34. // for shop
  35. $fields = $CI->db->list_fields($table);
  36. if (in_array('siteID', $fields) && $table != 'sites')
  37. {
  38. $CI->db->where('siteID', $CI->site->config['siteID']);
  39. }
  40. if (in_array('deleted', $fields))
  41. {
  42. $CI->db->where('deleted', 0);
  43. }
  44. $CI->form_validation->set_message('unique', 'The %s that you requested is already taken, please try another.');
  45. $CI->db->select('COUNT(*) dupe');
  46. $query = $CI->db->get_where($table, array($column => $str));
  47. $row = $query->row();
  48. return ($row->dupe > 0) ? FALSE : TRUE;
  49. }
  50. function really_unique($str, $field)
  51. {
  52. $CI =& get_instance();
  53. list($table, $column) = preg_split("/\./", $field, 2);
  54. $CI->form_validation->set_message('really_unique', 'The %s that you requested is already taken, please try another.');
  55. $CI->db->select('COUNT(*) dupe');
  56. $query = $CI->db->get_where($table, array($column => $str));
  57. $row = $query->row();
  58. return ($row->dupe > 0) ? FALSE : TRUE;
  59. }
  60. function set_error($error = '')
  61. {
  62. if (empty($error))
  63. {
  64. return FALSE;
  65. }
  66. else
  67. {
  68. $CI =& get_instance();
  69. $CI->form_validation->_error_array['custom_error'] = $error;
  70. return TRUE;
  71. }
  72. }
  73. }
  74. ?>