blob: 225325d6f59cf2c4aa0cba7419942f161103395b [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
Andrey Andreevb998f3e2012-01-24 15:31:26 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Form Validation Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
36 */
37class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020038
Timothy Warren0688ac92012-04-20 10:25:04 -040039 /**
40 * Reference to the CodeIgniter instance
41 *
42 * @var object
43 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010044 protected $CI;
Andrey Andreev56454792012-05-17 14:32:19 +030045
Timothy Warren0688ac92012-04-20 10:25:04 -040046 /**
47 * Validation data for the current form submission
48 *
49 * @var array
50 */
Andrey Andreev56454792012-05-17 14:32:19 +030051 protected $_field_data = array();
52
Timothy Warren0688ac92012-04-20 10:25:04 -040053 /**
54 * Validation rules for the current form
55 *
56 * @var array
57 */
Andrey Andreev56454792012-05-17 14:32:19 +030058 protected $_config_rules = array();
59
Timothy Warren0688ac92012-04-20 10:25:04 -040060 /**
61 * Array of validation errors
62 *
63 * @var array
64 */
Andrey Andreev78f55772012-04-03 19:59:08 +030065 protected $_error_array = array();
Andrey Andreev56454792012-05-17 14:32:19 +030066
Timothy Warren0688ac92012-04-20 10:25:04 -040067 /**
68 * Array of custom error messages
69 *
70 * @var array
71 */
Andrey Andreev78f55772012-04-03 19:59:08 +030072 protected $_error_messages = array();
Andrey Andreev56454792012-05-17 14:32:19 +030073
Timothy Warren0688ac92012-04-20 10:25:04 -040074 /**
75 * Start tag for error wrapping
76 *
77 * @var string
78 */
Andrey Andreev78f55772012-04-03 19:59:08 +030079 protected $_error_prefix = '<p>';
Andrey Andreev56454792012-05-17 14:32:19 +030080
Timothy Warren0688ac92012-04-20 10:25:04 -040081 /**
82 * End tag for error wrapping
Andrey Andreev56454792012-05-17 14:32:19 +030083 *
Timothy Warren0688ac92012-04-20 10:25:04 -040084 * @var string
85 */
Andrey Andreev78f55772012-04-03 19:59:08 +030086 protected $_error_suffix = '</p>';
Andrey Andreev56454792012-05-17 14:32:19 +030087
Timothy Warren0688ac92012-04-20 10:25:04 -040088 /**
89 * Custom error message
90 *
91 * @var string
92 */
Andrey Andreev78f55772012-04-03 19:59:08 +030093 protected $error_string = '';
Andrey Andreev56454792012-05-17 14:32:19 +030094
Timothy Warren0688ac92012-04-20 10:25:04 -040095 /**
96 * Whether the form data has been validated as safe
97 *
98 * @var bool
99 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300100 protected $_safe_form_data = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300101
Timothy Warren0688ac92012-04-20 10:25:04 -0400102 /**
103 * Custom data to validate
104 *
105 * @var array
106 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300107 protected $validation_data = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
Timothy Warren0688ac92012-04-20 10:25:04 -0400109 /**
110 * Initialize Form_Validation class
111 *
Andrey Andreev56454792012-05-17 14:32:19 +0300112 * @param array $rules
113 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -0400114 */
Greg Akera9263282010-11-10 15:26:43 -0600115 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +0200116 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200118
Mike Funk326a5e72012-02-24 10:06:28 -0500119 // applies delimiters set in config file.
Mike Funk7f42d062012-03-08 09:00:57 -0500120 if (isset($rules['error_prefix']))
121 {
122 $this->_error_prefix = $rules['error_prefix'];
123 unset($rules['error_prefix']);
124 }
125 if (isset($rules['error_suffix']))
126 {
127 $this->_error_suffix = $rules['error_suffix'];
128 unset($rules['error_suffix']);
129 }
Andrey Andreev31cf46e2012-03-20 15:48:00 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 // Validation rules can be stored in a config file.
132 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // Automatically load the form helper
135 $this->CI->load->helper('form');
136
137 // Set the character encoding in MB.
tiyowan5b9fd2d2012-03-12 20:26:59 +0400138 if (MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
140 mb_internal_encoding($this->CI->config->item('charset'));
141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Andrey Andreev901573c2012-01-11 01:40:48 +0200143 log_message('debug', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 /**
149 * Set Rules
150 *
151 * This function takes an array of field names and validation
152 * rules as input, validates the info, and stores it
153 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400154 * @param mixed $field
155 * @param string $label
156 * @param mixed $rules
Andrey Andreev78f55772012-04-03 19:59:08 +0300157 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100159 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
161 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +0000162 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200163 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Greg Aker9f9af602010-11-10 15:41:51 -0600165 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
tiyowanc2acb232012-03-14 21:24:00 +0400168 // If an array was passed via the first parameter instead of individual string
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // values we cycle through it and recursively call this function.
170 if (is_array($field))
171 {
172 foreach ($field as $row)
173 {
174 // Houston, we have a problem...
Andrey Andreev78f55772012-04-03 19:59:08 +0300175 if ( ! isset($row['field'], $row['rules']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
177 continue;
178 }
179
180 // If the field label wasn't passed we use the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300181 $label = isset($row['label']) ? $row['label'] : $row['field'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000182
183 // Here we go!
184 $this->set_rules($row['field'], $label, $row['rules']);
185 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300186
Greg Aker9f9af602010-11-10 15:41:51 -0600187 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // No fields? Nothing to do...
Alex Bilbied261b1e2012-06-02 11:12:16 +0100191 if ( ! is_string($field) OR ! is_string($rules) OR $field === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Greg Aker9f9af602010-11-10 15:41:51 -0600193 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 }
195
196 // If the field label wasn't passed we use the field name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100197 $label = ($label === '') ? $field : $label;
Derek Allard2067d1a2008-11-13 22:59:24 +0000198
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200199 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200200 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200201 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200202 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 // Note: Due to a bug in current() that affects some versions
204 // of PHP we can not pass function call directly into it
205 $x = explode('[', $field);
206 $indexes[] = current($x);
207
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200208 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100210 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200212 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 }
214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 $is_array = TRUE;
217 }
218 else
219 {
Barry Mienydd671972010-10-04 16:33:58 +0200220 $indexes = array();
221 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
224 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300226 'field' => $field,
227 'label' => $label,
228 'rules' => $rules,
229 'is_array' => $is_array,
230 'keys' => $indexes,
231 'postdata' => NULL,
232 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000233 );
Greg Aker9f9af602010-11-10 15:41:51 -0600234
235 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
237
238 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 /**
JonoB099c4782012-03-04 14:37:30 +0000241 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200242 *
JonoB099c4782012-03-04 14:37:30 +0000243 * If an array is set through this method, then this array will
244 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200245 *
246 * Note that if you are validating multiple arrays, then the
247 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000248 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200249 *
250 * @param array $data
251 * @return void
JonoB099c4782012-03-04 14:37:30 +0000252 */
253 public function set_data($data = '')
254 {
255 if ( ! empty($data) && is_array($data))
256 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200257 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000258 }
259 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200260
JonoB099c4782012-03-04 14:37:30 +0000261 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000262
263 /**
264 * Set Error Message
265 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300266 * Lets users set their own error messages on the fly. Note:
267 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300269 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300271 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100273 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
275 if ( ! is_array($lang))
276 {
277 $lang = array($lang => $val);
278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600281 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 /**
287 * Set The Error Delimiter
288 *
289 * Permits a prefix/suffix to be added to each error message
290 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * @param string
292 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300293 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200294 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100295 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
297 $this->_error_prefix = $prefix;
298 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600299 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
301
302 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 /**
305 * Get Error Message
306 *
307 * Gets the error message associated with a particular field
308 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * @param string the field name
Timothy Warren0688ac92012-04-20 10:25:04 -0400310 * @param string the html start tag
311 * @param strign the html end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300312 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200313 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100314 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200315 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300316 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
318 return '';
319 }
Barry Mienydd671972010-10-04 16:33:58 +0200320
Alex Bilbied261b1e2012-06-02 11:12:16 +0100321 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 $prefix = $this->_error_prefix;
324 }
325
Alex Bilbied261b1e2012-06-02 11:12:16 +0100326 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 $suffix = $this->_error_suffix;
329 }
330
331 return $prefix.$this->_field_data[$field]['error'].$suffix;
332 }
333
334 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100337 * Get Array of Error Messages
338 *
339 * Returns the error messages as an array
340 *
341 * @return array
342 */
343 public function error_array()
344 {
345 return $this->_error_array;
346 }
347
348 // --------------------------------------------------------------------
349
350 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 * Error String
352 *
353 * Returns the error messages as a string, wrapped in the error delimiters
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @param string
356 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300357 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200358 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100359 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
361 // No errrors, validation passes!
362 if (count($this->_error_array) === 0)
363 {
364 return '';
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Alex Bilbied261b1e2012-06-02 11:12:16 +0100367 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
369 $prefix = $this->_error_prefix;
370 }
371
Alex Bilbied261b1e2012-06-02 11:12:16 +0100372 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 $suffix = $this->_error_suffix;
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // Generate the error string
378 $str = '';
379 foreach ($this->_error_array as $val)
380 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100381 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 $str .= $prefix.$val.$suffix."\n";
384 }
385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 return $str;
388 }
389
390 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 /**
393 * Run the Validator
394 *
395 * This function does all the work.
396 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400397 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200399 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100400 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500402 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300403 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000404 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
406 return FALSE;
407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 // Does the _field_data array containing the validation rules exist?
410 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200411 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500413 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200414 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
416 return FALSE;
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // Is there a validation rule for the particular URI being accessed?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100420 $uri = ($group === '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200421
Alex Bilbied261b1e2012-06-02 11:12:16 +0100422 if ($uri !== '' && isset($this->_config_rules[$uri]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
424 $this->set_rules($this->_config_rules[$uri]);
425 }
426 else
427 {
428 $this->set_rules($this->_config_rules);
429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Andrey Andreev901573c2012-01-11 01:40:48 +0200431 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200432 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200434 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 return FALSE;
436 }
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // Load the language file containing error messages
440 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200441
442 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 // corresponding $_POST item and test for errors
444 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200445 {
JonoB099c4782012-03-04 14:37:30 +0000446 // 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 +0000447 // Depending on whether the field name is an array or a string will determine where we get it from.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200448 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
JonoB099c4782012-03-04 14:37:30 +0000450 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
Andrey Andreevfff6c2a2012-05-13 22:15:23 +0300452 elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300454 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
457 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
459
460 // Did we end up with any errors?
461 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 if ($total_errors > 0)
463 {
464 $this->_safe_form_data = TRUE;
465 }
466
467 // Now we need to re-set the POST data with the new, processed data
468 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200469
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200470 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
472
473 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 /**
476 * Traverse a multidimensional $_POST array index until the data is found
477 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 * @param array
479 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300480 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200482 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100483 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200485 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200487 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 return $array;
491 }
492
493 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 /**
496 * Re-populate the _POST array with our finalized and processed data
497 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200498 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200499 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100500 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
502 foreach ($this->_field_data as $field => $row)
503 {
504 if ( ! is_null($row['postdata']))
505 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200506 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 if (isset($_POST[$row['field']]))
509 {
510 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
511 }
512 }
513 else
514 {
Derek Jones63eeae32009-02-10 19:08:56 +0000515 // start with a reference
516 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Jones63eeae32009-02-10 19:08:56 +0000518 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200519 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
Derek Jones63eeae32009-02-10 19:08:56 +0000521 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
523 else
524 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 foreach ($row['keys'] as $val)
526 {
Derek Jones63eeae32009-02-10 19:08:56 +0000527 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529 }
Derek Jones63eeae32009-02-10 19:08:56 +0000530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000532 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 $array = array();
534 foreach ($row['postdata'] as $k => $v)
535 {
536 $array[$k] = $this->prep_for_form($v);
537 }
Derek Jones63eeae32009-02-10 19:08:56 +0000538
539 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
541 else
Derek Jones63eeae32009-02-10 19:08:56 +0000542 {
543 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
546 }
547 }
548 }
549
550 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 /**
553 * Executes the Validation routines
554 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 * @param array
556 * @param array
557 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300558 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200560 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100561 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
563 // If the $_POST data is an array we will run a recursive call
564 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200565 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 foreach ($postdata as $key => $val)
567 {
568 $this->_execute($row, $rules, $val, $cycles);
569 $cycles++;
570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 return;
573 }
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 // If the field is blank, but NOT required, no further tests are necessary
576 $callback = FALSE;
Andrey Andreev6de924c2012-01-20 13:18:18 +0200577 if ( ! in_array('required', $rules) && is_null($postdata))
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
579 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200580 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
582 $callback = TRUE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300583 $rules = array(1 => $match[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 }
585 else
586 {
587 return;
588 }
589 }
590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev6de924c2012-01-20 13:18:18 +0200592 if (is_null($postdata) && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
594 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
595 {
596 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300597 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200598
Andrey Andreev56454792012-05-17 14:32:19 +0300599 if (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
601 $line = $this->_error_messages[$type];
602 }
Andrey Andreev56454792012-05-17 14:32:19 +0300603 elseif (FALSE === ($line = $this->CI->lang->line($type)))
604 {
605 $line = 'The field was not set';
606 }
Barry Mienydd671972010-10-04 16:33:58 +0200607
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 // Build the error message
609 $message = sprintf($line, $this->_translate_fieldname($row['label']));
610
611 // Save the error message
612 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 if ( ! isset($this->_error_array[$row['field']]))
615 {
616 $this->_error_array[$row['field']] = $message;
617 }
618 }
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 return;
621 }
622
623 // --------------------------------------------------------------------
624
625 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300626 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
628 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200629
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 // We set the $postdata variable with the current data in our master array so that
631 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100632 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
634 // We shouldn't need this safety, but just in case there isn't an array index
635 // associated with this cycle we'll bail out
636 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
637 {
638 continue;
639 }
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
642 $_in_array = TRUE;
643 }
644 else
645 {
646 $postdata = $this->_field_data[$row['field']]['postdata'];
647 }
648
Barry Mienydd671972010-10-04 16:33:58 +0200649 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200651 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 {
653 $rule = substr($rule, 9);
654 $callback = TRUE;
655 }
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 // Strip the parameter (if exists) from the rule
658 // Rules can contain a parameter: max_length[5]
659 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200660 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 {
662 $rule = $match[1];
663 $param = $match[2];
664 }
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 // Call the function that corresponds to the rule
667 if ($callback === TRUE)
668 {
669 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200670 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200671 log_message('debug', 'Unable to find callback validation rule: '.$rule);
672 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200674 else
675 {
676 // Run the function and grab the result
677 $result = $this->CI->$rule($postdata, $param);
678 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000679
680 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200681 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300683 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 }
685 else
686 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300687 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 }
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200691 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000693 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 }
695 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300696 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200697 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300698 // If our own wrapper function doesn't exist we see if a native PHP function does.
699 // Users can use any native PHP function call that has one param.
700 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
Andrey Andreev320d37c2012-04-03 20:21:39 +0300702 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200703
Andrey Andreev78f55772012-04-03 19:59:08 +0300704 if ($_in_array === TRUE)
705 {
706 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 }
patwork02404a12011-04-08 15:45:46 +0200708 else
709 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300710 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200711 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200713 else
714 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300715 log_message('debug', 'Unable to find validation rule: '.$rule);
716 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200717 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300718 }
719 else
720 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 $result = $this->$rule($postdata, $param);
722
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200723 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300725 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 }
727 else
728 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300729 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 }
731 }
Barry Mienydd671972010-10-04 16:33:58 +0200732
Andrey Andreev901573c2012-01-11 01:40:48 +0200733 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200735 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 if ( ! isset($this->_error_messages[$rule]))
737 {
738 if (FALSE === ($line = $this->CI->lang->line($rule)))
739 {
740 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200741 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
743 else
744 {
745 $line = $this->_error_messages[$rule];
746 }
Barry Mienydd671972010-10-04 16:33:58 +0200747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200749 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200750 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 {
Pascal Krietec1895832009-10-13 12:56:43 +0000752 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 }
Barry Mienydd671972010-10-04 16:33:58 +0200754
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 // Build the error message
756 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
757
758 // Save the error message
759 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200760
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 if ( ! isset($this->_error_array[$row['field']]))
762 {
763 $this->_error_array[$row['field']] = $message;
764 }
Barry Mienydd671972010-10-04 16:33:58 +0200765
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 return;
767 }
768 }
769 }
770
771 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 /**
774 * Translate a field name
775 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 * @param string the field name
777 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200778 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100779 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 {
781 // Do we need to translate the field name?
782 // We look for the prefix lang: to determine this
Andrey Andreev901573c2012-01-11 01:40:48 +0200783 if (strpos($fieldname, 'lang:') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 {
785 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200786 $line = substr($fieldname, 5);
787
Derek Jones37f4b9c2011-07-01 17:56:50 -0500788 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
790 {
791 return $line;
792 }
793 }
794
795 return $fieldname;
796 }
797
798 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200799
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 /**
801 * Get the value from a form
802 *
803 * Permits you to repopulate a form field with the value it was submitted
804 * with, or, if that value doesn't exist, with the default
805 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 * @param string the field name
807 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200808 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200809 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100810 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200812 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
814 return $default;
815 }
Barry Mienydd671972010-10-04 16:33:58 +0200816
Phil Sturgeon5c561802011-01-05 16:31:59 +0000817 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600818 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000819 if (is_array($this->_field_data[$field]['postdata']))
820 {
821 return array_shift($this->_field_data[$field]['postdata']);
822 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 return $this->_field_data[$field]['postdata'];
825 }
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 /**
830 * Set Select
831 *
832 * Enables pull-down lists to be set to the value the user
833 * selected in the event of an error
834 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 * @param string
836 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400837 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200839 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100840 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200841 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200842 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200844 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 }
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 if (is_array($field))
849 {
850 if ( ! in_array($value, $field))
851 {
852 return '';
853 }
854 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100855 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200857 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 }
Barry Mienydd671972010-10-04 16:33:58 +0200859
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 return ' selected="selected"';
861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 /**
866 * Set Radio
867 *
868 * Enables radio buttons to be set to the value the user
869 * selected in the event of an error
870 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 * @param string
872 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400873 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200875 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100876 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200878 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200880 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 }
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 if (is_array($field))
885 {
886 if ( ! in_array($value, $field))
887 {
888 return '';
889 }
890 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100891 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200893 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 }
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 return ' checked="checked"';
897 }
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 /**
902 * Set Checkbox
903 *
904 * Enables checkboxes to be set to the value the user
905 * selected in the event of an error
906 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 * @param string
908 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400909 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200911 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100912 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200914 // Logic is exactly the same as for radio fields
915 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 }
Barry Mienydd671972010-10-04 16:33:58 +0200917
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 /**
921 * Required
922 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 * @param string
924 * @return bool
925 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100926 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300928 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 }
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500934 * Performs a Regular Expression match test.
935 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500936 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300937 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500938 * @return bool
939 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100940 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500941 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200942 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500943 }
944
945 // --------------------------------------------------------------------
946
947 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 * Match one field to another
949 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300951 * @param string field
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 * @return bool
953 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100954 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300956 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
Barry Mienydd671972010-10-04 16:33:58 +0200957
Andrey Andreev56454792012-05-17 14:32:19 +0300958 return isset($validation_array[$field]) ? ($str === $validation_array[$field]) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
Eric Barnescccde962011-12-04 00:01:17 -0500960
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100961 // --------------------------------------------------------------------
962
963 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200964 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100965 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200966 * Check if the input value doesn't already exist
967 * in the specified database field.
968 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100969 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300970 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100971 * @return bool
972 */
973 public function is_unique($str, $field)
974 {
Eric Barnescccde962011-12-04 00:01:17 -0500975 list($table, $field) = explode('.', $field);
976 if (isset($this->CI->db))
977 {
978 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
979 return $query->num_rows() === 0;
980 }
981 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600982 }
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 /**
987 * Minimum Length
988 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300990 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200992 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100993 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200995 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
997 return FALSE;
998 }
999
Andrey Andreev78f55772012-04-03 19:59:08 +03001000 return (MB_ENABLED === TRUE)
1001 ? ($val <= mb_strlen($str))
George Petsagourakis2d51c082012-05-02 20:29:04 +03001002 : ($val <= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001006
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 /**
1008 * Max Length
1009 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001011 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001013 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001014 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001016 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 {
1018 return FALSE;
1019 }
1020
Andrey Andreev78f55772012-04-03 19:59:08 +03001021 return (MB_ENABLED === TRUE)
1022 ? ($val >= mb_strlen($str))
1023 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 }
Barry Mienydd671972010-10-04 16:33:58 +02001025
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001027
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 /**
1029 * Exact Length
1030 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001032 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001034 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001035 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001037 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 {
1039 return FALSE;
1040 }
1041
Andrey Andreev78f55772012-04-03 19:59:08 +03001042 return (MB_ENABLED === TRUE)
Alex Bilbied261b1e2012-06-02 11:12:16 +01001043 ? (mb_strlen($str) === $val)
1044 : (strlen($str) === $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 }
Barry Mienydd671972010-10-04 16:33:58 +02001046
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 /**
1050 * Valid Email
1051 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 * @param string
1053 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001054 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001055 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001057 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 +00001058 }
1059
1060 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001061
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 /**
1063 * Valid Emails
1064 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 * @param string
1066 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001067 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001068 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 {
1070 if (strpos($str, ',') === FALSE)
1071 {
1072 return $this->valid_email(trim($str));
1073 }
Barry Mienydd671972010-10-04 16:33:58 +02001074
Pascal Kriete14287f32011-02-14 13:39:34 -05001075 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001077 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 {
1079 return FALSE;
1080 }
1081 }
Barry Mienydd671972010-10-04 16:33:58 +02001082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 return TRUE;
1084 }
1085
1086 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001087
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 /**
1089 * Validate IP Address
1090 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001092 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001094 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 {
1096 return $this->CI->input->valid_ip($ip);
1097 }
1098
1099 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001100
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 /**
1102 * Alpha
1103 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 * @param string
1105 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001106 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001107 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001109 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 }
Barry Mienydd671972010-10-04 16:33:58 +02001111
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001113
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 /**
1115 * Alpha-numeric
1116 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 * @param string
1118 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001119 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001120 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001122 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 }
Barry Mienydd671972010-10-04 16:33:58 +02001124
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001126
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 /**
1128 * Alpha-numeric with underscores and dashes
1129 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 * @param string
1131 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001132 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001133 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001135 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 }
Barry Mienydd671972010-10-04 16:33:58 +02001137
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001139
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 /**
1141 * Numeric
1142 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 * @param string
1144 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001145 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001146 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001148 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001149
1150 }
1151
1152 // --------------------------------------------------------------------
1153
Barry Mienydd671972010-10-04 16:33:58 +02001154 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 * Integer
1156 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 * @param string
1158 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001159 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001160 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001162 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1163 }
1164
1165 // --------------------------------------------------------------------
1166
1167 /**
1168 * Decimal number
1169 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001170 * @param string
1171 * @return bool
1172 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001173 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001174 {
1175 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1176 }
1177
1178 // --------------------------------------------------------------------
1179
1180 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001181 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001182 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001183 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001184 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001185 * @return bool
1186 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001187 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001188 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001189 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001190 }
1191
1192 // --------------------------------------------------------------------
1193
1194 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001195 * Equal to or Greater than
1196 *
Nick Busey98c347d2012-02-02 11:07:03 -07001197 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001198 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001199 * @return bool
1200 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001201 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001202 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001203 return is_numeric($str) ? ($str >= $min) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001204 }
1205
1206 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001207
1208 /**
1209 * Less than
1210 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001211 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001212 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001213 * @return bool
1214 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001215 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001216 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001217 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001219
1220 // --------------------------------------------------------------------
1221
Barry Mienydd671972010-10-04 16:33:58 +02001222 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001223 * Equal to or Less than
1224 *
Nick Busey98c347d2012-02-02 11:07:03 -07001225 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001226 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001227 * @return bool
1228 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001229 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001230 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001231 return is_numeric($str) ? ($str <= $max) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001232 }
1233
1234 // --------------------------------------------------------------------
1235
1236 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001237 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001238 *
Barry Mienydd671972010-10-04 16:33:58 +02001239 * @param string
1240 * @return bool
1241 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001242 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001243 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001244 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001245 }
1246
1247 // --------------------------------------------------------------------
1248
1249 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001250 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001251 *
Barry Mienydd671972010-10-04 16:33:58 +02001252 * @param string
1253 * @return bool
1254 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001255 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001256 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001257 return ($str !== 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001258 }
1259
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001261
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 /**
1263 * Valid Base64
1264 *
1265 * Tests a string for characters outside of the Base64 alphabet
1266 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1267 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 * @param string
1269 * @return bool
1270 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001271 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 {
Andrey Andreev56454792012-05-17 14:32:19 +03001273 return ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 }
Barry Mienydd671972010-10-04 16:33:58 +02001275
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001277
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 /**
1279 * Prep data for form
1280 *
1281 * This function allows HTML to be safely shown in a form.
1282 * Special characters are converted.
1283 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 * @param string
1285 * @return string
1286 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001287 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 {
1289 if (is_array($data))
1290 {
1291 foreach ($data as $key => $val)
1292 {
1293 $data[$key] = $this->prep_for_form($val);
1294 }
Barry Mienydd671972010-10-04 16:33:58 +02001295
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 return $data;
1297 }
Barry Mienydd671972010-10-04 16:33:58 +02001298
Alex Bilbied261b1e2012-06-02 11:12:16 +01001299 if ($this->_safe_form_data === FALSE OR $data === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 {
1301 return $data;
1302 }
1303
Andrey Andreev901573c2012-01-11 01:40:48 +02001304 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 }
Barry Mienydd671972010-10-04 16:33:58 +02001306
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001308
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 /**
1310 * Prep URL
1311 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 * @param string
1313 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001314 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001315 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001317 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 {
1319 return '';
1320 }
Barry Mienydd671972010-10-04 16:33:58 +02001321
Andrey Andreev901573c2012-01-11 01:40:48 +02001322 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001324 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 }
Barry Mienydd671972010-10-04 16:33:58 +02001326
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 return $str;
1328 }
Barry Mienydd671972010-10-04 16:33:58 +02001329
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001331
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 /**
1333 * Strip Image Tags
1334 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 * @param string
1336 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001337 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001338 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 {
1340 return $this->CI->input->strip_image_tags($str);
1341 }
Barry Mienydd671972010-10-04 16:33:58 +02001342
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001344
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 /**
1346 * XSS Clean
1347 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 * @param string
1349 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001350 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001351 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 {
Derek Jones5640a712010-04-23 11:22:40 -05001353 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 }
Barry Mienydd671972010-10-04 16:33:58 +02001355
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001357
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 /**
1359 * Convert PHP tags to entities
1360 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 * @param string
1362 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001363 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001364 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001366 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 }
1368
JonoB099c4782012-03-04 14:37:30 +00001369 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001370
1371 /**
1372 * Reset validation vars
1373 *
1374 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001375 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001376 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001377 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001378 */
JonoB883f80f2012-03-05 09:51:27 +00001379 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001380 {
JonoB099c4782012-03-04 14:37:30 +00001381 $this->_field_data = array();
1382 $this->_config_rules = array();
1383 $this->_error_array = array();
1384 $this->_error_messages = array();
1385 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001386 }
1387
Derek Allard2067d1a2008-11-13 22:59:24 +00001388}
Derek Allard2067d1a2008-11-13 22:59:24 +00001389
1390/* End of file Form_validation.php */
Andrey Andreev31cf46e2012-03-20 15:48:00 +02001391/* Location: ./system/libraries/Form_validation.php */