PageRenderTime 193ms CodeModel.GetById 90ms app.highlight 4ms RepoModel.GetById 97ms app.codeStats 0ms

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