blob: 1c0089d855927246db3d4899368c949e3be6cc17 [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();
Andrey Andreevc8da4fe2012-03-04 19:20:33 +020051
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
Derek Allard2067d1a2008-11-13 22:59:24 +000056 // Validation rules can be stored in a config file.
57 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Allard2067d1a2008-11-13 22:59:24 +000059 // Automatically load the form helper
60 $this->CI->load->helper('form');
61
62 // Set the character encoding in MB.
63 if (function_exists('mb_internal_encoding'))
64 {
65 mb_internal_encoding($this->CI->config->item('charset'));
66 }
Barry Mienydd671972010-10-04 16:33:58 +020067
Derek Allard2067d1a2008-11-13 22:59:24 +000068 log_message('debug', "Form Validation Class Initialized");
69 }
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 /**
74 * Set Rules
75 *
76 * This function takes an array of field names and validation
77 * rules as input, validates the info, and stores it
78 *
Derek Allard2067d1a2008-11-13 22:59:24 +000079 * @param mixed
80 * @param string
81 * @return void
82 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010083 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +020085 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +000086 // or a validation array has not been specified
87 if (count($_POST) === 0 && count($this->validation_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Greg Aker9f9af602010-11-10 15:41:51 -060089 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +000090 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 // If an array was passed via the first parameter instead of indidual string
93 // values we cycle through it and recursively call this function.
94 if (is_array($field))
95 {
96 foreach ($field as $row)
97 {
98 // Houston, we have a problem...
99 if ( ! isset($row['field']) OR ! isset($row['rules']))
100 {
101 continue;
102 }
103
104 // If the field label wasn't passed we use the field name
105 $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
106
107 // Here we go!
108 $this->set_rules($row['field'], $label, $row['rules']);
109 }
Greg Aker9f9af602010-11-10 15:41:51 -0600110 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 // No fields? Nothing to do...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200114 if ( ! is_string($field) OR ! is_string($rules) OR $field == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Greg Aker9f9af602010-11-10 15:41:51 -0600116 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
118
119 // If the field label wasn't passed we use the field name
120 $label = ($label == '') ? $field : $label;
121
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200122 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200123 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200124 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200125 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // Note: Due to a bug in current() that affects some versions
127 // of PHP we can not pass function call directly into it
128 $x = explode('[', $field);
129 $indexes[] = current($x);
130
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200131 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200133 if ($matches[1][$i] != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200135 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 $is_array = TRUE;
140 }
141 else
142 {
Barry Mienydd671972010-10-04 16:33:58 +0200143 $indexes = array();
144 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
147 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 $this->_field_data[$field] = array(
Phil Sturgeonef112c02011-02-07 13:01:47 +0000149 'field' => $field,
150 'label' => $label,
151 'rules' => $rules,
152 'is_array' => $is_array,
153 'keys' => $indexes,
154 'postdata' => NULL,
155 'error' => ''
156 );
Greg Aker9f9af602010-11-10 15:41:51 -0600157
158 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
160
161 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200162
JonoB099c4782012-03-04 14:37:30 +0000163 /**
164 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200165 *
JonoB099c4782012-03-04 14:37:30 +0000166 * If an array is set through this method, then this array will
167 * be used instead of the $_POST array
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200168 *
169 * @param array $data
170 * @return void
JonoB099c4782012-03-04 14:37:30 +0000171 */
172 public function set_data($data = '')
173 {
174 if ( ! empty($data) && is_array($data))
175 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200176 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000177 }
178 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200179
JonoB099c4782012-03-04 14:37:30 +0000180 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 /**
183 * Set Error Message
184 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500185 * Lets users set their own error messages on the fly. Note: The key
JonoB099c4782012-03-04 14:37:30 +0000186 * name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * @param string
189 * @param string
190 * @return string
191 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100192 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
194 if ( ! is_array($lang))
195 {
196 $lang = array($lang => $val);
197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 $this->_error_messages = array_merge($this->_error_messages, $lang);
Phil Sturgeonc3828712011-01-19 12:31:47 +0000200
Greg Aker9f9af602010-11-10 15:41:51 -0600201 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 /**
207 * Set The Error Delimiter
208 *
209 * Permits a prefix/suffix to be added to each error message
210 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 * @param string
212 * @param string
213 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200214 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100215 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
217 $this->_error_prefix = $prefix;
218 $this->_error_suffix = $suffix;
Phil Sturgeonc3828712011-01-19 12:31:47 +0000219
Greg Aker9f9af602010-11-10 15:41:51 -0600220 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222
223 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 /**
226 * Get Error Message
227 *
228 * Gets the error message associated with a particular field
229 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 * @param string the field name
231 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200232 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100233 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200234 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 if ( ! isset($this->_field_data[$field]['error']) OR $this->_field_data[$field]['error'] == '')
236 {
237 return '';
238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 if ($prefix == '')
241 {
242 $prefix = $this->_error_prefix;
243 }
244
245 if ($suffix == '')
246 {
247 $suffix = $this->_error_suffix;
248 }
249
250 return $prefix.$this->_field_data[$field]['error'].$suffix;
251 }
252
253 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100256 * Get Array of Error Messages
257 *
258 * Returns the error messages as an array
259 *
260 * @return array
261 */
262 public function error_array()
263 {
264 return $this->_error_array;
265 }
266
267 // --------------------------------------------------------------------
268
269 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * Error String
271 *
272 * Returns the error messages as a string, wrapped in the error delimiters
273 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @param string
275 * @param string
276 * @return str
Barry Mienydd671972010-10-04 16:33:58 +0200277 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100278 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 // No errrors, validation passes!
281 if (count($this->_error_array) === 0)
282 {
283 return '';
284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if ($prefix == '')
287 {
288 $prefix = $this->_error_prefix;
289 }
290
291 if ($suffix == '')
292 {
293 $suffix = $this->_error_suffix;
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 // Generate the error string
297 $str = '';
298 foreach ($this->_error_array as $val)
299 {
300 if ($val != '')
301 {
302 $str .= $prefix.$val.$suffix."\n";
303 }
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 return $str;
307 }
308
309 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 /**
312 * Run the Validator
313 *
314 * This function does all the work.
315 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200317 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100318 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500320 // Do we even have any data to process? Mm?
JonoB099c4782012-03-04 14:37:30 +0000321 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
322 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
324 return FALSE;
325 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200326
JonoB099c4782012-03-04 14:37:30 +0000327 // Clear any previous validation data
328 $this->_reset_validation();
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // Does the _field_data array containing the validation rules exist?
331 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200332 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500334 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200335 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 return FALSE;
338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // Is there a validation rule for the particular URI being accessed?
341 $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 if ($uri != '' AND isset($this->_config_rules[$uri]))
344 {
345 $this->set_rules($this->_config_rules[$uri]);
346 }
347 else
348 {
349 $this->set_rules($this->_config_rules);
350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // We're we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200353 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 log_message('debug', "Unable to find validation rules");
356 return FALSE;
357 }
358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // Load the language file containing error messages
361 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200362
363 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // corresponding $_POST item and test for errors
365 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200366 {
JonoB099c4782012-03-04 14:37:30 +0000367 // 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 +0000368 // 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 +0200369
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200370 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
JonoB099c4782012-03-04 14:37:30 +0000372 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
374 else
375 {
JonoB099c4782012-03-04 14:37:30 +0000376 if (isset($validation_array[$field]) AND $validation_array[$field] != "")
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
JonoB099c4782012-03-04 14:37:30 +0000378 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
380 }
Barry Mienydd671972010-10-04 16:33:58 +0200381
382 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
384
385 // Did we end up with any errors?
386 $total_errors = count($this->_error_array);
387
388 if ($total_errors > 0)
389 {
390 $this->_safe_form_data = TRUE;
391 }
392
393 // Now we need to re-set the POST data with the new, processed data
394 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200395
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200396 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
398
399 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 /**
402 * Traverse a multidimensional $_POST array index until the data is found
403 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * @param array
405 * @param array
406 * @param integer
407 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200408 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100409 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200411 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200413 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 return $array;
417 }
418
419 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 /**
422 * Re-populate the _POST array with our finalized and processed data
423 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200425 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100426 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
428 foreach ($this->_field_data as $field => $row)
429 {
430 if ( ! is_null($row['postdata']))
431 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200432 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 if (isset($_POST[$row['field']]))
435 {
436 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
437 }
438 }
439 else
440 {
Derek Jones63eeae32009-02-10 19:08:56 +0000441 // start with a reference
442 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Jones63eeae32009-02-10 19:08:56 +0000444 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200445 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
Derek Jones63eeae32009-02-10 19:08:56 +0000447 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449 else
450 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 foreach ($row['keys'] as $val)
452 {
Derek Jones63eeae32009-02-10 19:08:56 +0000453 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455 }
Derek Jones63eeae32009-02-10 19:08:56 +0000456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000458 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 $array = array();
460 foreach ($row['postdata'] as $k => $v)
461 {
462 $array[$k] = $this->prep_for_form($v);
463 }
Derek Jones63eeae32009-02-10 19:08:56 +0000464
465 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
467 else
Derek Jones63eeae32009-02-10 19:08:56 +0000468 {
469 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
472 }
473 }
474 }
475
476 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 /**
479 * Executes the Validation routines
480 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * @param array
482 * @param array
483 * @param mixed
484 * @param integer
485 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200486 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100487 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
489 // If the $_POST data is an array we will run a recursive call
490 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200491 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 foreach ($postdata as $key => $val)
493 {
494 $this->_execute($row, $rules, $val, $cycles);
495 $cycles++;
496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 return;
499 }
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 // --------------------------------------------------------------------
502
503 // If the field is blank, but NOT required, no further tests are necessary
504 $callback = FALSE;
505 if ( ! in_array('required', $rules) AND is_null($postdata))
506 {
507 // Before we bail out, does the rule contain a callback?
Marcos Coelhoc28b2852011-07-05 12:59:41 -0700508 if (preg_match("/(callback_\w+(\[.*?\])?)/", implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
510 $callback = TRUE;
511 $rules = (array('1' => $match[1]));
512 }
513 else
514 {
515 return;
516 }
517 }
518
519 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200522 if (is_null($postdata) AND $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
525 {
526 // Set the message type
527 $type = (in_array('required', $rules)) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 if ( ! isset($this->_error_messages[$type]))
530 {
531 if (FALSE === ($line = $this->CI->lang->line($type)))
532 {
533 $line = 'The field was not set';
Barry Mienydd671972010-10-04 16:33:58 +0200534 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536 else
537 {
538 $line = $this->_error_messages[$type];
539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 // Build the error message
542 $message = sprintf($line, $this->_translate_fieldname($row['label']));
543
544 // Save the error message
545 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 if ( ! isset($this->_error_array[$row['field']]))
548 {
549 $this->_error_array[$row['field']] = $message;
550 }
551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 return;
554 }
555
556 // --------------------------------------------------------------------
557
558 // Cycle through each rule and run it
559 foreach ($rules As $rule)
560 {
561 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 // We set the $postdata variable with the current data in our master array so that
564 // each cycle of the loop is dealing with the processed data from the last cycle
565 if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
566 {
567 // We shouldn't need this safety, but just in case there isn't an array index
568 // associated with this cycle we'll bail out
569 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
570 {
571 continue;
572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
575 $_in_array = TRUE;
576 }
577 else
578 {
579 $postdata = $this->_field_data[$row['field']]['postdata'];
580 }
581
582 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200583
584 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 $callback = FALSE;
586 if (substr($rule, 0, 9) == 'callback_')
587 {
588 $rule = substr($rule, 9);
589 $callback = TRUE;
590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 // Strip the parameter (if exists) from the rule
593 // Rules can contain a parameter: max_length[5]
594 $param = FALSE;
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500595 if (preg_match("/(.*?)\[(.*)\]/", $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
597 $rule = $match[1];
598 $param = $match[2];
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 // Call the function that corresponds to the rule
602 if ($callback === TRUE)
603 {
604 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200605 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 continue;
607 }
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 // Run the function and grab the result
610 $result = $this->CI->$rule($postdata, $param);
611
612 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200613 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
615 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
616 }
617 else
618 {
619 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
620 }
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 // If the field isn't required and we just processed a callback we'll move on...
623 if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
624 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000625 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 }
627 }
628 else
Barry Mienydd671972010-10-04 16:33:58 +0200629 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 if ( ! method_exists($this, $rule))
631 {
Barry Mienydd671972010-10-04 16:33:58 +0200632 // If our own wrapper function doesn't exist we see if a native PHP function does.
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 // Users can use any native PHP function call that has one param.
634 if (function_exists($rule))
635 {
636 $result = $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200637
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200638 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
640 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
641 }
642 else
643 {
644 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
645 }
646 }
patwork02404a12011-04-08 15:45:46 +0200647 else
648 {
649 log_message('debug', "Unable to find validation rule: ".$rule);
650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 continue;
653 }
654
655 $result = $this->$rule($postdata, $param);
656
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200657 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
659 $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
660 }
661 else
662 {
663 $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
664 }
665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Jones37f4b9c2011-07-01 17:56:50 -0500667 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200669 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 if ( ! isset($this->_error_messages[$rule]))
671 {
672 if (FALSE === ($line = $this->CI->lang->line($rule)))
673 {
674 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200675 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 }
677 else
678 {
679 $line = $this->_error_messages[$rule];
680 }
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 // Is the parameter we are inserting into the error message the name
Derek Jones37f4b9c2011-07-01 17:56:50 -0500683 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200684 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
Pascal Krietec1895832009-10-13 12:56:43 +0000686 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // Build the error message
690 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
691
692 // Save the error message
693 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 if ( ! isset($this->_error_array[$row['field']]))
696 {
697 $this->_error_array[$row['field']] = $message;
698 }
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 return;
701 }
702 }
703 }
704
705 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 /**
708 * Translate a field name
709 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 * @param string the field name
711 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200712 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100713 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 {
715 // Do we need to translate the field name?
716 // We look for the prefix lang: to determine this
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200717 if (substr($fieldname, 0, 5) === 'lang:')
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
719 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200720 $line = substr($fieldname, 5);
721
Derek Jones37f4b9c2011-07-01 17:56:50 -0500722 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
724 {
725 return $line;
726 }
727 }
728
729 return $fieldname;
730 }
731
732 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 /**
735 * Get the value from a form
736 *
737 * Permits you to repopulate a form field with the value it was submitted
738 * with, or, if that value doesn't exist, with the default
739 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 * @param string the field name
741 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200742 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200743 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100744 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200746 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 {
748 return $default;
749 }
Barry Mienydd671972010-10-04 16:33:58 +0200750
Phil Sturgeon5c561802011-01-05 16:31:59 +0000751 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600752 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000753 if (is_array($this->_field_data[$field]['postdata']))
754 {
755 return array_shift($this->_field_data[$field]['postdata']);
756 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 return $this->_field_data[$field]['postdata'];
759 }
Barry Mienydd671972010-10-04 16:33:58 +0200760
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 /**
764 * Set Select
765 *
766 * Enables pull-down lists to be set to the value the user
767 * selected in the event of an error
768 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 * @param string
770 * @param string
771 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200772 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100773 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200774 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200775 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 {
Andrey Andreev0adff1b2012-02-28 14:36:40 +0200777 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 }
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 if (is_array($field))
783 {
784 if ( ! in_array($value, $field))
785 {
786 return '';
787 }
788 }
Andrey Andreev46ac8812012-02-28 14:32:54 +0200789 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200791 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 }
Barry Mienydd671972010-10-04 16:33:58 +0200793
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 return ' selected="selected"';
795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 /**
800 * Set Radio
801 *
802 * Enables radio buttons to be set to the value the user
803 * selected in the event of an error
804 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 * @param string
806 * @param string
807 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200808 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100809 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200811 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200813 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 }
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 $field = $this->_field_data[$field]['postdata'];
Barry Mienydd671972010-10-04 16:33:58 +0200817
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 if (is_array($field))
819 {
820 if ( ! in_array($value, $field))
821 {
822 return '';
823 }
824 }
825 else
826 {
827 if (($field == '' OR $value == '') OR ($field != $value))
828 {
829 return '';
830 }
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 return ' checked="checked"';
834 }
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 /**
839 * Set Checkbox
840 *
841 * Enables checkboxes to be set to the value the user
842 * selected in the event of an error
843 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 * @param string
845 * @param string
846 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200847 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100848 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200850 // Logic is exactly the same as for radio fields
851 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 }
Barry Mienydd671972010-10-04 16:33:58 +0200853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 /**
857 * Required
858 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 * @param string
860 * @return bool
861 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100862 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200864 return ( ! is_array($str)) ? (trim($str) !== '') : ( ! empty($str));
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 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500870 * Performs a Regular Expression match test.
871 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500872 * @param string
873 * @param regex
874 * @return bool
875 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100876 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500877 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200878 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500879 }
880
881 // --------------------------------------------------------------------
882
883 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * Match one field to another
885 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 * @param string
887 * @param field
888 * @return bool
889 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100890 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200892 $validation_array = ( ! empty($this->validation_data)) ? $this->validation_data : $_POST;
JonoB099c4782012-03-04 14:37:30 +0000893 if ( ! isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
Barry Mienydd671972010-10-04 16:33:58 +0200895 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
Barry Mienydd671972010-10-04 16:33:58 +0200897
JonoB099c4782012-03-04 14:37:30 +0000898 return ($str === $validation_array[$field]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 }
Eric Barnescccde962011-12-04 00:01:17 -0500900
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100901 // --------------------------------------------------------------------
902
903 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200904 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100905 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200906 * Check if the input value doesn't already exist
907 * in the specified database field.
908 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100909 * @param string
910 * @param field
911 * @return bool
912 */
913 public function is_unique($str, $field)
914 {
Eric Barnescccde962011-12-04 00:01:17 -0500915 list($table, $field) = explode('.', $field);
916 if (isset($this->CI->db))
917 {
918 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
919 return $query->num_rows() === 0;
920 }
921 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 /**
927 * Minimum Length
928 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 * @param string
930 * @param value
931 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200932 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100933 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200935 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
937 return FALSE;
938 }
939
940 if (function_exists('mb_strlen'))
941 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200942 return ! (mb_strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200945 return ! (strlen($str) < $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 /**
951 * Max Length
952 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 * @param string
954 * @param value
955 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200956 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100957 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200959 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 {
961 return FALSE;
962 }
963
964 if (function_exists('mb_strlen'))
965 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200966 return ! (mb_strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200969 return ! (strlen($str) > $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 }
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 /**
975 * Exact Length
976 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 * @param string
978 * @param value
979 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200980 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100981 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200983 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
985 return FALSE;
986 }
987
988 if (function_exists('mb_strlen'))
989 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200990 return (mb_strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 }
Barry Mienydd671972010-10-04 16:33:58 +0200992
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200993 return (strlen($str) == $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 }
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 /**
999 * Valid Email
1000 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 * @param string
1002 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001003 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001004 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001006 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 +00001007 }
1008
1009 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 /**
1012 * Valid Emails
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_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
1019 if (strpos($str, ',') === FALSE)
1020 {
1021 return $this->valid_email(trim($str));
1022 }
Barry Mienydd671972010-10-04 16:33:58 +02001023
Pascal Kriete14287f32011-02-14 13:39:34 -05001024 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001026 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
1028 return FALSE;
1029 }
1030 }
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 return TRUE;
1033 }
1034
1035 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001036
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 /**
1038 * Validate IP Address
1039 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001041 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001043 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 {
1045 return $this->CI->input->valid_ip($ip);
1046 }
1047
1048 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 /**
1051 * Alpha
1052 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 * @param string
1054 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001055 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001056 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001058 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 }
Barry Mienydd671972010-10-04 16:33:58 +02001060
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 /**
1064 * Alpha-numeric
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_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001071 return (bool) preg_match('/^[a-z0-9]+$/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 with underscores and dashes
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_dash($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 * Numeric
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 numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001097 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001098
1099 }
1100
1101 // --------------------------------------------------------------------
1102
Barry Mienydd671972010-10-04 16:33:58 +02001103 /**
1104 * Is Numeric
1105 *
Barry Mienydd671972010-10-04 16:33:58 +02001106 * @param string
1107 * @return bool
1108 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001109 public function is_numeric($str)
Barry Mienydd671972010-10-04 16:33:58 +02001110 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001111 return is_numeric($str);
Barry Mienydd671972010-10-04 16:33:58 +02001112 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001113
1114 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 /**
1117 * Integer
1118 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 * @param string
1120 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001121 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001122 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001124 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1125 }
1126
1127 // --------------------------------------------------------------------
1128
1129 /**
1130 * Decimal number
1131 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001132 * @param string
1133 * @return bool
1134 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001135 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001136 {
1137 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1138 }
1139
1140 // --------------------------------------------------------------------
1141
1142 /**
1143 * Greather than
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 greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001149 {
1150 if ( ! is_numeric($str))
1151 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001152 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001153 }
1154 return $str > $min;
1155 }
1156
1157 // --------------------------------------------------------------------
1158
1159 /**
1160 * Less than
1161 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001162 * @param string
1163 * @return bool
1164 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001165 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001166 {
1167 if ( ! is_numeric($str))
1168 {
Pascal Kriete8761ef52011-02-14 13:13:52 -05001169 return FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001170 }
1171 return $str < $max;
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001173
1174 // --------------------------------------------------------------------
1175
Barry Mienydd671972010-10-04 16:33:58 +02001176 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001177 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001178 *
Barry Mienydd671972010-10-04 16:33:58 +02001179 * @param string
1180 * @return bool
1181 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001182 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001183 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001184 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001185 }
1186
1187 // --------------------------------------------------------------------
1188
1189 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001190 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001191 *
Barry Mienydd671972010-10-04 16:33:58 +02001192 * @param string
1193 * @return bool
1194 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001195 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001196 {
Andrey Andreev46ac8812012-02-28 14:32:54 +02001197 return ($str != 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001198 }
1199
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001201
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 /**
1203 * Valid Base64
1204 *
1205 * Tests a string for characters outside of the Base64 alphabet
1206 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1207 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 * @param string
1209 * @return bool
1210 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001211 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 {
1213 return (bool) ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
1214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001217
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 /**
1219 * Prep data for form
1220 *
1221 * This function allows HTML to be safely shown in a form.
1222 * Special characters are converted.
1223 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 * @param string
1225 * @return string
1226 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001227 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 {
1229 if (is_array($data))
1230 {
1231 foreach ($data as $key => $val)
1232 {
1233 $data[$key] = $this->prep_for_form($val);
1234 }
Barry Mienydd671972010-10-04 16:33:58 +02001235
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 return $data;
1237 }
Barry Mienydd671972010-10-04 16:33:58 +02001238
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 if ($this->_safe_form_data == FALSE OR $data === '')
1240 {
1241 return $data;
1242 }
1243
Andrey Andreev46ac8812012-02-28 14:32:54 +02001244 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 }
Barry Mienydd671972010-10-04 16:33:58 +02001246
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001248
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 /**
1250 * Prep URL
1251 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 * @param string
1253 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001254 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001255 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 {
1257 if ($str == 'http://' OR $str == '')
1258 {
1259 return '';
1260 }
Barry Mienydd671972010-10-04 16:33:58 +02001261
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001262 if (substr($str, 0, 7) !== 'http://' && substr($str, 0, 8) !== 'https://')
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 {
1264 $str = 'http://'.$str;
1265 }
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 return $str;
1268 }
Barry Mienydd671972010-10-04 16:33:58 +02001269
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001271
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 /**
1273 * Strip Image Tags
1274 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 * @param string
1276 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001277 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001278 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
1280 return $this->CI->input->strip_image_tags($str);
1281 }
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001284
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 /**
1286 * XSS Clean
1287 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 * @param string
1289 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001290 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001291 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 {
Derek Jones5640a712010-04-23 11:22:40 -05001293 return $this->CI->security->xss_clean($str);
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 * Convert PHP tags to entities
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 encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001306 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001308
JonoB099c4782012-03-04 14:37:30 +00001309 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001310
1311 /**
1312 * Reset validation vars
1313 *
1314 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001315 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001316 *
1317 * @return void
1318 */
1319 protected function _reset_validation()
1320 {
JonoB099c4782012-03-04 14:37:30 +00001321 $this->_field_data = array();
1322 $this->_config_rules = array();
1323 $this->_error_array = array();
1324 $this->_error_messages = array();
1325 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001326 }
1327
Derek Allard2067d1a2008-11-13 22:59:24 +00001328}
Derek Allard2067d1a2008-11-13 22:59:24 +00001329
1330/* End of file Form_validation.php */
Marcos Coelhoc28b2852011-07-05 12:59:41 -07001331/* Location: ./system/libraries/Form_validation.php */