blob: 3e16d69ed436931a590dcc9632ccfb2bd8170d1d [file] [log] [blame]
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnescccde962011-12-04 00:01:17 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnescccde962011-12-04 00:01:17 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Form Validation Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
38 */
39class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020040
Phil Sturgeon3837ae72011-05-09 21:12:26 +010041 protected $CI;
42 protected $_field_data = array();
43 protected $_config_rules = array();
44 protected $_error_array = array();
45 protected $_error_messages = array();
46 protected $_error_prefix = '<p>';
47 protected $_error_suffix = '</p>';
48 protected $error_string = '';
49 protected $_safe_form_data = FALSE;
JonoB099c4782012-03-04 14:37:30 +000050 protected $validation_data = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000051
Greg Akera9263282010-11-10 15:26:43 -060052 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +020053 {
Derek Allard2067d1a2008-11-13 22:59:24 +000054 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020055
Mike Funk326a5e72012-02-24 10:06:28 -050056 // applies delimiters set in config file.
Mike Funk7f42d062012-03-08 09:00:57 -050057 if (isset($rules['error_prefix']))
58 {
59 $this->_error_prefix = $rules['error_prefix'];
60 unset($rules['error_prefix']);
61 }
62 if (isset($rules['error_suffix']))
63 {
64 $this->_error_suffix = $rules['error_suffix'];
65 unset($rules['error_suffix']);
66 }
Mike Funk326a5e72012-02-24 10:06:28 -050067
Derek Allard2067d1a2008-11-13 22:59:24 +000068 // Validation rules can be stored in a config file.
69 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 // Automatically load the form helper
72 $this->CI->load->helper('form');
73
74 // Set the character encoding in MB.
75 if (function_exists('mb_internal_encoding'))
76 {
77 mb_internal_encoding($this->CI->config->item('charset'));
78 }
Barry Mienydd671972010-10-04 16:33:58 +020079
Derek Allard2067d1a2008-11-13 22:59:24 +000080 log_message('debug', "Form Validation Class Initialized");
81 }
Barry Mienydd671972010-10-04 16:33:58 +020082
Derek Allard2067d1a2008-11-13 22:59:24 +000083 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Allard2067d1a2008-11-13 22:59:24 +000085 /**
86 * Set Rules
87 *
88 * This function takes an array of field names and validation
89 * rules as input, validates the info, and stores it
90 *
Derek Allard2067d1a2008-11-13 22:59:24 +000091 * @param mixed
92 * @param string
93 * @return void
94 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010095 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
97 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +000098 // or a validation array has not been specified
99 if (count($_POST) === 0 && count($this->validation_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Greg Aker9f9af602010-11-10 15:41:51 -0600101 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 // If an array was passed via the first parameter instead of indidual string
105 // values we cycle through it and recursively call this function.
106 if (is_array($field))
107 {
108 foreach ($field as $row)
109 {
110 // Houston, we have a problem...
111 if ( ! isset($row['field']) OR ! isset($row['rules']))
112 {
113 continue;
114 }
115
116 // If the field label wasn't passed we use the field name
117 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
118
119 // Here we go!
120 $this->set_rules($row['field'], $label, $row['rules']);
121 }
Greg Aker9f9af602010-11-10 15:41:51 -0600122 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200126 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
Greg Aker9f9af602010-11-10 15:41:51 -0600128 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130
131 // If the field label wasn't passed we use the field name
132 $label = ($label == '') ? $field : $label;
133
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200134 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200135 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200136 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200137 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 // Note: Due to a bug in current() that affects some versions
139 // of PHP we can not pass function call directly into it
140 $x = explode('[', $field);
141 $indexes[] = current($x);
142
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200143 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200145 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200147 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
149 }
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 $is_array = TRUE;
152 }
153 else
154 {
Barry Mienydd671972010-10-04 16:33:58 +0200155 $indexes = array();
156 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
159 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000161 'field' => $field,
162 'label' => $label,
163 'rules' => $rules,
164 'is_array' => $is_array,
165 'keys' => $indexes,
166 'postdata' => NULL,
167 'error' => ''
168 );
Greg Aker9f9af602010-11-10 15:41:51 -0600169
170 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
172
173 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 /**
JonoB099c4782012-03-04 14:37:30 +0000176 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200177 *
JonoB099c4782012-03-04 14:37:30 +0000178 * If an array is set through this method, then this array will
179 * be used instead of the $_POST array
JonoB883f80f2012-03-05 09:51:27 +0000180 *
181 * Note that if you are validating multiple arrays, then the
182 * reset_validation() function should be called after validating
183 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200184 *
185 * @param array $data
186 * @return void
JonoB099c4782012-03-04 14:37:30 +0000187 */
188 public function set_data($data = '')
189 {
190 if ( ! empty($data) && is_array($data))
191 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200192 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000193 }
194 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200195
JonoB099c4782012-03-04 14:37:30 +0000196 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000197
198 /**
199 * Set Error Message
200 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500201 * Lets users set their own error messages on the fly. Note: The key
JonoB099c4782012-03-04 14:37:30 +0000202 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * @param string
205 * @param string
206 * @return string
207 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100208 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 if ( ! is_array($lang))
211 {
212 $lang = array($lang => $val);
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 $this->_error_messages = array_merge($this->_error_messages, $lang);
Phil Sturgeonc3828712011-01-19 12:31:47 +0000216
Greg Aker9f9af602010-11-10 15:41:51 -0600217 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 /**
223 * Set The Error Delimiter
224 *
225 * Permits a prefix/suffix to be added to each error message
226 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 * @param string
228 * @param string
229 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200230 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100231 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
233 $this->_error_prefix = $prefix;
234 $this->_error_suffix = $suffix;
Phil Sturgeonc3828712011-01-19 12:31:47 +0000235
Greg Aker9f9af602010-11-10 15:41:51 -0600236 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 }
238
239 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 /**
242 * Get Error Message
243 *
244 * Gets the error message associated with a particular field
245 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * @param string the field name
247 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200248 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100249 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200250 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
252 {
253 return '';
254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 if ($prefix == '')
257 {
258 $prefix = $this->_error_prefix;
259 }
260
261 if ($suffix == '')
262 {
263 $suffix = $this->_error_suffix;
264 }
265
266 return $prefix.$this->_field_data[$field]['error'].$suffix;
267 }
268
269 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100272 * Get Array of Error Messages
273 *
274 * Returns the error messages as an array
275 *
276 * @return array
277 */
278 public function error_array()
279 {
280 return $this->_error_array;
281 }
282
283 // --------------------------------------------------------------------
284
285 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * Error String
287 *
288 * Returns the error messages as a string, wrapped in the error delimiters
289 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 * @param string
291 * @param string
292 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200293 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100294 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
296 // No errrors, validation passes!
297 if (count($this->_error_array) === 0)
298 {
299 return '';
300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 if ($prefix == '')
303 {
304 $prefix = $this->_error_prefix;
305 }
306
307 if ($suffix == '')
308 {
309 $suffix = $this->_error_suffix;
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // Generate the error string
313 $str = '';
314 foreach ($this->_error_array as $val)
315 {
316 if ($val != '')
317 {
318 $str .= $prefix.$val.$suffix."\n";
319 }
320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 return $str;
323 }
324
325 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 /**
328 * Run the Validator
329 *
330 * This function does all the work.
331 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200333 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100334 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500336 // Do we even have any data to process? Mm?
JonoB099c4782012-03-04 14:37:30 +0000337 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
338 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
340 return FALSE;
341 }
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // Does the _field_data array containing the validation rules exist?
344 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200345 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500347 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200348 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 return FALSE;
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // Is there a validation rule for the particular URI being accessed?
354 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 if ($uri != '' AND isset($this->_config_rules[$uri]))
357 {
358 $this->set_rules($this->_config_rules[$uri]);
359 }
360 else
361 {
362 $this->set_rules($this->_config_rules);
363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // We're we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200366 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
368 log_message('debug', "Unable to find validation rules");
369 return FALSE;
370 }
371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 // Load the language file containing error messages
374 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200375
376 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // corresponding $_POST item and test for errors
378 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200379 {
JonoB099c4782012-03-04 14:37:30 +0000380 // Fetch the data from the corresponding $_POST or validation array and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 // Depending on whether the field name is an array or a string will determine where we get it from.
Barry Mienydd671972010-10-04 16:33:58 +0200382
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200383 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
JonoB099c4782012-03-04 14:37:30 +0000385 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
387 else
388 {
JonoB099c4782012-03-04 14:37:30 +0000389 if (isset($validation_array[$field]) AND $validation_array[$field] != "")
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
JonoB099c4782012-03-04 14:37:30 +0000391 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
395 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397
398 // Did we end up with any errors?
399 $total_errors = count($this->_error_array);
400
401 if ($total_errors > 0)
402 {
403 $this->_safe_form_data = TRUE;
404 }
405
406 // Now we need to re-set the POST data with the new, processed data
407 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200408
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200409 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
411
412 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 /**
415 * Traverse a multidimensional $_POST array index until the data is found
416 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 * @param array
418 * @param array
419 * @param integer
420 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200421 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100422 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200424 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200426 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 return $array;
430 }
431
432 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 /**
435 * Re-populate the _POST array with our finalized and processed data
436 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200438 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100439 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 foreach ($this->_field_data as $field => $row)
442 {
443 if ( ! is_null($row['postdata']))
444 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200445 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
447 if (isset($_POST[$row['field']]))
448 {
449 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
450 }
451 }
452 else
453 {
Derek Jones63eeae32009-02-10 19:08:56 +0000454 // start with a reference
455 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Jones63eeae32009-02-10 19:08:56 +0000457 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200458 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Derek Jones63eeae32009-02-10 19:08:56 +0000460 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462 else
463 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 foreach ($row['keys'] as $val)
465 {
Derek Jones63eeae32009-02-10 19:08:56 +0000466 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468 }
Derek Jones63eeae32009-02-10 19:08:56 +0000469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000471 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 $array = array();
473 foreach ($row['postdata'] as $k => $v)
474 {
475 $array[$k] = $this->prep_for_form($v);
476 }
Derek Jones63eeae32009-02-10 19:08:56 +0000477
478 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480 else
Derek Jones63eeae32009-02-10 19:08:56 +0000481 {
482 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485 }
486 }
487 }
488
489 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 /**
492 * Executes the Validation routines
493 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @param array
495 * @param array
496 * @param mixed
497 * @param integer
498 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200499 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100500 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
502 // If the $_POST data is an array we will run a recursive call
503 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200504 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 foreach ($postdata as $key => $val)
506 {
507 $this->_execute($row, $rules, $val, $cycles);
508 $cycles++;
509 }
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 return;
512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 // --------------------------------------------------------------------
515
516 // If the field is blank, but NOT required, no further tests are necessary
517 $callback = FALSE;
518 if ( ! in_array('required', $rules) AND is_null($postdata))
519 {
520 // Before we bail out, does the rule contain a callback?
Marcos Coelhoc28b2852011-07-05 12:59:41 -0700521 if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
523 $callback = TRUE;
524 $rules = (array('1' => $match[1]));
525 }
526 else
527 {
528 return;
529 }
530 }
531
532 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200535 if (is_null($postdata) AND $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
537 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
538 {
539 // Set the message type
540 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 if ( ! isset($this->_error_messages[$type]))
543 {
544 if (FALSE === ($line = $this->CI->lang->line($type)))
545 {
546 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200547 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
549 else
550 {
551 $line = $this->_error_messages[$type];
552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 // Build the error message
555 $message = sprintf($line, $this->_translate_fieldname($row['label']));
556
557 // Save the error message
558 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 if ( ! isset($this->_error_array[$row['field']]))
561 {
562 $this->_error_array[$row['field']] = $message;
563 }
564 }
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 return;
567 }
568
569 // --------------------------------------------------------------------
570
571 // Cycle through each rule and run it
572 foreach ($rules As $rule)
573 {
574 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 // We set the $postdata variable with the current data in our master array so that
577 // each cycle of the loop is dealing with the processed data from the last cycle
578 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
579 {
580 // We shouldn't need this safety, but just in case there isn't an array index
581 // associated with this cycle we'll bail out
582 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
583 {
584 continue;
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
588 $_in_array = TRUE;
589 }
590 else
591 {
592 $postdata = $this->_field_data[$row['field']]['postdata'];
593 }
594
595 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200596
597 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 $callback = FALSE;
599 if (substr($rule, 0, 9) == 'callback_')
600 {
601 $rule = substr($rule, 9);
602 $callback = TRUE;
603 }
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 // Strip the parameter (if exists) from the rule
606 // Rules can contain a parameter: max_length[5]
607 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500608 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 $rule = $match[1];
611 $param = $match[2];
612 }
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 // Call the function that corresponds to the rule
615 if ($callback === TRUE)
616 {
617 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200618 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 continue;
620 }
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 // Run the function and grab the result
623 $result = $this->CI->$rule($postdata, $param);
624
625 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200626 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
628 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
629 }
630 else
631 {
632 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
633 }
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 // If the field isn't required and we just processed a callback we'll move on...
636 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
637 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000638 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 }
640 }
641 else
Barry Mienydd671972010-10-04 16:33:58 +0200642 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 if ( ! method_exists($this, $rule))
644 {
Barry Mienydd671972010-10-04 16:33:58 +0200645 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 // Users can use any native PHP function call that has one param.
647 if (function_exists($rule))
648 {
649 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200650
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200651 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 {
653 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
654 }
655 else
656 {
657 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
658 }
659 }
patwork02404a12011-04-08 15:45:46 +0200660 else
661 {
662 log_message('debug', "Unable to find validation rule: ".$rule);
663 }
Barry Mienydd671972010-10-04 16:33:58 +0200664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 continue;
666 }
667
668 $result = $this->$rule($postdata, $param);
669
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200670 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
672 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
673 }
674 else
675 {
676 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
677 }
678 }
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Jones37f4b9c2011-07-01 17:56:50 -0500680 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200682 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 if ( ! isset($this->_error_messages[$rule]))
684 {
685 if (FALSE === ($line = $this->CI->lang->line($rule)))
686 {
687 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200688 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
690 else
691 {
692 $line = $this->_error_messages[$rule];
693 }
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 // Is the parameter we are inserting into the error message the name
Derek Jones37f4b9c2011-07-01 17:56:50 -0500696 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200697 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
Pascal Krietec1895832009-10-13 12:56:43 +0000699 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 }
Barry Mienydd671972010-10-04 16:33:58 +0200701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 // Build the error message
703 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
704
705 // Save the error message
706 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 if ( ! isset($this->_error_array[$row['field']]))
709 {
710 $this->_error_array[$row['field']] = $message;
711 }
Barry Mienydd671972010-10-04 16:33:58 +0200712
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 return;
714 }
715 }
716 }
717
718 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 /**
721 * Translate a field name
722 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 * @param string the field name
724 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200725 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100726 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 {
728 // Do we need to translate the field name?
729 // We look for the prefix lang: to determine this
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200730 if (substr($fieldname, 0, 5) === 'lang:')
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
732 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200733 $line = substr($fieldname, 5);
734
Derek Jones37f4b9c2011-07-01 17:56:50 -0500735 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
737 {
738 return $line;
739 }
740 }
741
742 return $fieldname;
743 }
744
745 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200746
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 /**
748 * Get the value from a form
749 *
750 * Permits you to repopulate a form field with the value it was submitted
751 * with, or, if that value doesn't exist, with the default
752 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 * @param string the field name
754 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200755 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200756 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100757 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200759 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
761 return $default;
762 }
Barry Mienydd671972010-10-04 16:33:58 +0200763
Phil Sturgeon5c561802011-01-05 16:31:59 +0000764 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600765 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000766 if (is_array($this->_field_data[$field]['postdata']))
767 {
768 return array_shift($this->_field_data[$field]['postdata']);
769 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000770
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 return $this->_field_data[$field]['postdata'];
772 }
Barry Mienydd671972010-10-04 16:33:58 +0200773
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 /**
777 * Set Select
778 *
779 * Enables pull-down lists to be set to the value the user
780 * selected in the event of an error
781 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 * @param string
783 * @param string
784 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200785 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100786 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200787 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200788 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 {
Andrey Andreev0adff1b2012-02-28 14:36:40 +0200790 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 }
Barry Mienydd671972010-10-04 16:33:58 +0200792
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 if (is_array($field))
796 {
797 if ( ! in_array($value, $field))
798 {
799 return '';
800 }
801 }
Andrey Andreev46ac8812012-02-28 14:32:54 +0200802 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200804 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 return ' selected="selected"';
808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 /**
813 * Set Radio
814 *
815 * Enables radio buttons to be set to the value the user
816 * selected in the event of an error
817 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 * @param string
819 * @param string
820 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200821 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100822 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200824 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200826 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 }
Barry Mienydd671972010-10-04 16:33:58 +0200828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 if (is_array($field))
832 {
833 if ( ! in_array($value, $field))
834 {
835 return '';
836 }
837 }
838 else
839 {
840 if (($field == '' OR $value == '') OR ($field != $value))
841 {
842 return '';
843 }
844 }
Barry Mienydd671972010-10-04 16:33:58 +0200845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 return ' checked="checked"';
847 }
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 /**
852 * Set Checkbox
853 *
854 * Enables checkboxes to be set to the value the user
855 * selected in the event of an error
856 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 * @param string
858 * @param string
859 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200860 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100861 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200863 // Logic is exactly the same as for radio fields
864 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 /**
870 * Required
871 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 * @param string
873 * @return bool
874 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100875 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200877 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 }
Barry Mienydd671972010-10-04 16:33:58 +0200879
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200881
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500883 * Performs a Regular Expression match test.
884 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500885 * @param string
886 * @param regex
887 * @return bool
888 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100889 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500890 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200891 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500892 }
893
894 // --------------------------------------------------------------------
895
896 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 * Match one field to another
898 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 * @param string
900 * @param field
901 * @return bool
902 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100903 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200905 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
JonoB099c4782012-03-04 14:37:30 +0000906 if ( ! isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
Barry Mienydd671972010-10-04 16:33:58 +0200908 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
JonoB099c4782012-03-04 14:37:30 +0000911 return ($str === $validation_array[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 }
Eric Barnescccde962011-12-04 00:01:17 -0500913
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100914 // --------------------------------------------------------------------
915
916 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200917 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100918 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200919 * Check if the input value doesn't already exist
920 * in the specified database field.
921 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100922 * @param string
923 * @param field
924 * @return bool
925 */
926 public function is_unique($str, $field)
927 {
Eric Barnescccde962011-12-04 00:01:17 -0500928 list($table, $field) = explode('.', $field);
929 if (isset($this->CI->db))
930 {
931 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
932 return $query->num_rows() === 0;
933 }
934 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 /**
940 * Minimum Length
941 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 * @param string
943 * @param value
944 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200945 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100946 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200948 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
950 return FALSE;
951 }
952
953 if (function_exists('mb_strlen'))
954 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200955 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
Barry Mienydd671972010-10-04 16:33:58 +0200957
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200958 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 /**
964 * Max Length
965 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 * @param string
967 * @param value
968 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200969 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100970 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200972 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
974 return FALSE;
975 }
976
977 if (function_exists('mb_strlen'))
978 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200979 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
Barry Mienydd671972010-10-04 16:33:58 +0200981
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200982 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 /**
988 * Exact Length
989 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 * @param string
991 * @param value
992 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200993 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100994 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200996 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 {
998 return FALSE;
999 }
1000
1001 if (function_exists('mb_strlen'))
1002 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001003 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001006 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 /**
1012 * Valid Email
1013 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 * @param string
1015 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001016 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001017 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001019 return (bool) preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 }
1021
1022 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001023
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 /**
1025 * Valid Emails
1026 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 * @param string
1028 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001029 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001030 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
1032 if (strpos($str, ',') === FALSE)
1033 {
1034 return $this->valid_email(trim($str));
1035 }
Barry Mienydd671972010-10-04 16:33:58 +02001036
Pascal Kriete14287f32011-02-14 13:39:34 -05001037 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001039 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 {
1041 return FALSE;
1042 }
1043 }
Barry Mienydd671972010-10-04 16:33:58 +02001044
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 return TRUE;
1046 }
1047
1048 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 /**
1051 * Validate IP Address
1052 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001054 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001056 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 {
1058 return $this->CI->input->valid_ip($ip);
1059 }
1060
1061 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 /**
1064 * Alpha
1065 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 * @param string
1067 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001068 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001069 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001071 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 }
Barry Mienydd671972010-10-04 16:33:58 +02001073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001075
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 /**
1077 * Alpha-numeric
1078 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 * @param string
1080 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001081 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001082 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001084 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 }
Barry Mienydd671972010-10-04 16:33:58 +02001086
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 /**
1090 * Alpha-numeric with underscores and dashes
1091 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 * @param string
1093 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001094 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001095 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001097 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 }
Barry Mienydd671972010-10-04 16:33:58 +02001099
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001101
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 /**
1103 * Numeric
1104 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 * @param string
1106 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001107 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001108 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001110 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001111
1112 }
1113
1114 // --------------------------------------------------------------------
1115
Barry Mienydd671972010-10-04 16:33:58 +02001116 /**
1117 * Is Numeric
1118 *
Barry Mienydd671972010-10-04 16:33:58 +02001119 * @param string
1120 * @return bool
1121 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001122 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001123 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001124 return is_numeric($str);
Barry Mienydd671972010-10-04 16:33:58 +02001125 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001126
1127 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001128
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 /**
1130 * Integer
1131 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 * @param string
1133 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001134 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001135 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001137 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1138 }
1139
1140 // --------------------------------------------------------------------
1141
1142 /**
1143 * Decimal number
1144 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001145 * @param string
1146 * @return bool
1147 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001148 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001149 {
1150 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1151 }
1152
1153 // --------------------------------------------------------------------
1154
1155 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001156 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001157 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001158 * @param string
1159 * @return bool
1160 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001161 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001162 {
1163 if ( ! is_numeric($str))
1164 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001165 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001166 }
1167 return $str > $min;
1168 }
1169
1170 // --------------------------------------------------------------------
Nick Busey98c347d2012-02-02 11:07:03 -07001171
1172 /**
1173 * Equal to or Greater than
1174 *
1175 * @access public
1176 * @param string
1177 * @return bool
1178 */
Nick Buseyc1931662012-02-06 17:55:58 -07001179 function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001180 {
1181 if ( ! is_numeric($str))
1182 {
1183 return FALSE;
1184 }
1185 return $str >= $min;
1186 }
1187
1188 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001189
1190 /**
1191 * Less than
1192 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001193 * @param string
1194 * @return bool
1195 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001196 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001197 {
1198 if ( ! is_numeric($str))
1199 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001200 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001201 }
1202 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001204
1205 // --------------------------------------------------------------------
1206
Barry Mienydd671972010-10-04 16:33:58 +02001207 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001208 * Equal to or Less than
1209 *
1210 * @access public
1211 * @param string
1212 * @return bool
1213 */
Nick Buseyc1931662012-02-06 17:55:58 -07001214 function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001215 {
1216 if ( ! is_numeric($str))
1217 {
1218 return FALSE;
1219 }
1220 return $str <= $max;
1221 }
1222
1223 // --------------------------------------------------------------------
1224
1225 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001226 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001227 *
Barry Mienydd671972010-10-04 16:33:58 +02001228 * @param string
1229 * @return bool
1230 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001231 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001232 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001233 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001234 }
1235
1236 // --------------------------------------------------------------------
1237
1238 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001239 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001240 *
Barry Mienydd671972010-10-04 16:33:58 +02001241 * @param string
1242 * @return bool
1243 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001244 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001245 {
Andrey Andreev46ac8812012-02-28 14:32:54 +02001246 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001247 }
1248
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001250
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 /**
1252 * Valid Base64
1253 *
1254 * Tests a string for characters outside of the Base64 alphabet
1255 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1256 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 * @param string
1258 * @return bool
1259 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001260 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 {
1262 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1263 }
Barry Mienydd671972010-10-04 16:33:58 +02001264
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 /**
1268 * Prep data for form
1269 *
1270 * This function allows HTML to be safely shown in a form.
1271 * Special characters are converted.
1272 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 * @param string
1274 * @return string
1275 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001276 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 {
1278 if (is_array($data))
1279 {
1280 foreach ($data as $key => $val)
1281 {
1282 $data[$key] = $this->prep_for_form($val);
1283 }
Barry Mienydd671972010-10-04 16:33:58 +02001284
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 return $data;
1286 }
Barry Mienydd671972010-10-04 16:33:58 +02001287
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 if ($this->_safe_form_data == FALSE OR $data === '')
1289 {
1290 return $data;
1291 }
1292
Andrey Andreev46ac8812012-02-28 14:32:54 +02001293 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 }
Barry Mienydd671972010-10-04 16:33:58 +02001295
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001297
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 /**
1299 * Prep URL
1300 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 * @param string
1302 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001303 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001304 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 {
1306 if ($str == 'http://' OR $str == '')
1307 {
1308 return '';
1309 }
Barry Mienydd671972010-10-04 16:33:58 +02001310
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001311 if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://')
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 {
1313 $str = 'http://'.$str;
1314 }
Barry Mienydd671972010-10-04 16:33:58 +02001315
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 return $str;
1317 }
Barry Mienydd671972010-10-04 16:33:58 +02001318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001320
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 /**
1322 * Strip Image Tags
1323 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 * @param string
1325 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001326 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001327 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 {
1329 return $this->CI->input->strip_image_tags($str);
1330 }
Barry Mienydd671972010-10-04 16:33:58 +02001331
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001333
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 /**
1335 * XSS Clean
1336 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 * @param string
1338 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001339 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001340 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 {
Derek Jones5640a712010-04-23 11:22:40 -05001342 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 }
Barry Mienydd671972010-10-04 16:33:58 +02001344
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001346
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 /**
1348 * Convert PHP tags to entities
1349 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 * @param string
1351 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001352 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001353 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001355 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 }
1357
JonoB099c4782012-03-04 14:37:30 +00001358 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001359
1360 /**
1361 * Reset validation vars
1362 *
1363 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001364 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001365 *
1366 * @return void
1367 */
JonoB883f80f2012-03-05 09:51:27 +00001368 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001369 {
JonoB099c4782012-03-04 14:37:30 +00001370 $this->_field_data = array();
1371 $this->_config_rules = array();
1372 $this->_error_array = array();
1373 $this->_error_messages = array();
1374 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001375 }
1376
Derek Allard2067d1a2008-11-13 22:59:24 +00001377}
Derek Allard2067d1a2008-11-13 22:59:24 +00001378
1379/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001380/* Location: ./system/libraries/Form_validation.php */