blob: 6db5bb495d8e2792fc000833bb155f3cc4940504 [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
Andrey Andreev901573c2012-01-11 01:40:48 +0200137 log_message('debug', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 /**
143 * Set Rules
144 *
145 * This function takes an array of field names and validation
146 * rules as input, validates the info, and stores it
147 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400148 * @param mixed $field
149 * @param string $label
150 * @param mixed $rules
Andrey Andreev78f55772012-04-03 19:59:08 +0300151 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100153 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
155 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +0000156 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200157 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Greg Aker9f9af602010-11-10 15:41:51 -0600159 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
tiyowanc2acb232012-03-14 21:24:00 +0400162 // If an array was passed via the first parameter instead of individual string
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // values we cycle through it and recursively call this function.
164 if (is_array($field))
165 {
166 foreach ($field as $row)
167 {
168 // Houston, we have a problem...
Andrey Andreev78f55772012-04-03 19:59:08 +0300169 if ( ! isset($row['field'], $row['rules']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 continue;
172 }
173
174 // If the field label wasn't passed we use the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300175 $label = isset($row['label']) ? $row['label'] : $row['field'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000176
177 // Here we go!
178 $this->set_rules($row['field'], $label, $row['rules']);
179 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300180
Greg Aker9f9af602010-11-10 15:41:51 -0600181 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Kevin Wood-Friend6a56d502012-06-12 09:54:01 -0400184 // Convert an array of rules to a string
185 if (is_array($rules))
186 {
187 $rules = implode('|', $rules);
188 }
189
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
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300457 // Don't try to validate if we have no rules set
458 if (empty($row['rules']))
459 {
460 continue;
461 }
462
Barry Mienydd671972010-10-04 16:33:58 +0200463 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
465
466 // Did we end up with any errors?
467 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 if ($total_errors > 0)
469 {
470 $this->_safe_form_data = TRUE;
471 }
472
473 // Now we need to re-set the POST data with the new, processed data
474 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200475
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200476 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
478
479 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 /**
482 * Traverse a multidimensional $_POST array index until the data is found
483 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @param array
485 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300486 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200488 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100489 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200491 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200493 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 return $array;
497 }
498
499 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 /**
502 * Re-populate the _POST array with our finalized and processed data
503 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200504 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200505 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100506 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 foreach ($this->_field_data as $field => $row)
509 {
510 if ( ! is_null($row['postdata']))
511 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200512 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
514 if (isset($_POST[$row['field']]))
515 {
516 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
517 }
518 }
519 else
520 {
Derek Jones63eeae32009-02-10 19:08:56 +0000521 // start with a reference
522 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Jones63eeae32009-02-10 19:08:56 +0000524 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200525 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
Derek Jones63eeae32009-02-10 19:08:56 +0000527 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529 else
530 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 foreach ($row['keys'] as $val)
532 {
Derek Jones63eeae32009-02-10 19:08:56 +0000533 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
535 }
Derek Jones63eeae32009-02-10 19:08:56 +0000536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000538 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 $array = array();
540 foreach ($row['postdata'] as $k => $v)
541 {
542 $array[$k] = $this->prep_for_form($v);
543 }
Derek Jones63eeae32009-02-10 19:08:56 +0000544
545 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 }
547 else
Derek Jones63eeae32009-02-10 19:08:56 +0000548 {
549 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
552 }
553 }
554 }
555
556 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 /**
559 * Executes the Validation routines
560 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @param array
562 * @param array
563 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300564 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200566 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100567 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
569 // If the $_POST data is an array we will run a recursive call
570 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200571 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 foreach ($postdata as $key => $val)
573 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300574 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 return;
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // If the field is blank, but NOT required, no further tests are necessary
581 $callback = FALSE;
Andrey Andreev6de924c2012-01-20 13:18:18 +0200582 if ( ! in_array('required', $rules) && is_null($postdata))
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
584 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200585 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 $callback = TRUE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300588 $rules = array(1 => $match[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 }
590 else
591 {
592 return;
593 }
594 }
595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev6de924c2012-01-20 13:18:18 +0200597 if (is_null($postdata) && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
600 {
601 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300602 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200603
Andrey Andreev56454792012-05-17 14:32:19 +0300604 if (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
606 $line = $this->_error_messages[$type];
607 }
Andrey Andreev56454792012-05-17 14:32:19 +0300608 elseif (FALSE === ($line = $this->CI->lang->line($type)))
609 {
610 $line = 'The field was not set';
611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // Build the error message
614 $message = sprintf($line, $this->_translate_fieldname($row['label']));
615
616 // Save the error message
617 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 if ( ! isset($this->_error_array[$row['field']]))
620 {
621 $this->_error_array[$row['field']] = $message;
622 }
623 }
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 return;
626 }
627
628 // --------------------------------------------------------------------
629
630 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300631 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
633 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 // We set the $postdata variable with the current data in our master array so that
636 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100637 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
639 // We shouldn't need this safety, but just in case there isn't an array index
640 // associated with this cycle we'll bail out
641 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
642 {
643 continue;
644 }
Barry Mienydd671972010-10-04 16:33:58 +0200645
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
647 $_in_array = TRUE;
648 }
649 else
650 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300651 // If we get an array field, but it's not expected - then it is most likely
652 // somebody messing with the form on the client side, so we'll just consider
653 // it an empty field
654 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
655 ? NULL
656 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
658
Barry Mienydd671972010-10-04 16:33:58 +0200659 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200661 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
663 $rule = substr($rule, 9);
664 $callback = TRUE;
665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // Strip the parameter (if exists) from the rule
668 // Rules can contain a parameter: max_length[5]
669 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200670 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
672 $rule = $match[1];
673 $param = $match[2];
674 }
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 // Call the function that corresponds to the rule
677 if ($callback === TRUE)
678 {
679 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200680 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200681 log_message('debug', 'Unable to find callback validation rule: '.$rule);
682 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200684 else
685 {
686 // Run the function and grab the result
687 $result = $this->CI->$rule($postdata, $param);
688 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000689
690 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200691 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300693 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 }
695 else
696 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300697 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 }
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200701 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000703 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
705 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300706 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200707 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300708 // If our own wrapper function doesn't exist we see if a native PHP function does.
709 // Users can use any native PHP function call that has one param.
710 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 {
Andrey Andreev320d37c2012-04-03 20:21:39 +0300712 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200713
Andrey Andreev78f55772012-04-03 19:59:08 +0300714 if ($_in_array === TRUE)
715 {
716 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 }
patwork02404a12011-04-08 15:45:46 +0200718 else
719 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300720 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200721 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200723 else
724 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300725 log_message('debug', 'Unable to find validation rule: '.$rule);
726 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200727 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300728 }
729 else
730 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 $result = $this->$rule($postdata, $param);
732
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200733 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300735 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
737 else
738 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300739 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 }
741 }
Barry Mienydd671972010-10-04 16:33:58 +0200742
Andrey Andreev901573c2012-01-11 01:40:48 +0200743 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200745 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 if ( ! isset($this->_error_messages[$rule]))
747 {
748 if (FALSE === ($line = $this->CI->lang->line($rule)))
749 {
750 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200751 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 }
753 else
754 {
755 $line = $this->_error_messages[$rule];
756 }
Barry Mienydd671972010-10-04 16:33:58 +0200757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200759 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200760 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 {
Pascal Krietec1895832009-10-13 12:56:43 +0000762 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 }
Barry Mienydd671972010-10-04 16:33:58 +0200764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 // Build the error message
766 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
767
768 // Save the error message
769 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200770
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 if ( ! isset($this->_error_array[$row['field']]))
772 {
773 $this->_error_array[$row['field']] = $message;
774 }
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 return;
777 }
778 }
779 }
780
781 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 /**
784 * Translate a field name
785 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 * @param string the field name
787 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200788 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100789 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 {
791 // Do we need to translate the field name?
792 // We look for the prefix lang: to determine this
Andrey Andreev901573c2012-01-11 01:40:48 +0200793 if (strpos($fieldname, 'lang:') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
795 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200796 $line = substr($fieldname, 5);
797
Derek Jones37f4b9c2011-07-01 17:56:50 -0500798 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
800 {
801 return $line;
802 }
803 }
804
805 return $fieldname;
806 }
807
808 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 /**
811 * Get the value from a form
812 *
813 * Permits you to repopulate a form field with the value it was submitted
814 * with, or, if that value doesn't exist, with the default
815 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 * @param string the field name
817 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200818 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200819 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100820 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200822 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 {
824 return $default;
825 }
Barry Mienydd671972010-10-04 16:33:58 +0200826
Phil Sturgeon5c561802011-01-05 16:31:59 +0000827 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600828 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000829 if (is_array($this->_field_data[$field]['postdata']))
830 {
831 return array_shift($this->_field_data[$field]['postdata']);
832 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 return $this->_field_data[$field]['postdata'];
835 }
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 /**
840 * Set Select
841 *
842 * Enables pull-down lists to be set to the value the user
843 * selected in the event of an error
844 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 * @param string
846 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400847 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200849 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100850 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200851 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200852 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200854 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 }
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 if (is_array($field))
859 {
860 if ( ! in_array($value, $field))
861 {
862 return '';
863 }
864 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100865 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200867 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 return ' selected="selected"';
871 }
Barry Mienydd671972010-10-04 16:33:58 +0200872
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200874
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 /**
876 * Set Radio
877 *
878 * Enables radio buttons to be set to the value the user
879 * selected in the event of an error
880 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 * @param string
882 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400883 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200885 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100886 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200888 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200890 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 }
Barry Mienydd671972010-10-04 16:33:58 +0200892
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 if (is_array($field))
895 {
896 if ( ! in_array($value, $field))
897 {
898 return '';
899 }
900 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100901 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200903 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 }
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 return ' checked="checked"';
907 }
Barry Mienydd671972010-10-04 16:33:58 +0200908
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 /**
912 * Set Checkbox
913 *
914 * Enables checkboxes to be set to the value the user
915 * selected in the event of an error
916 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 * @param string
918 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400919 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200921 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100922 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200924 // Logic is exactly the same as for radio fields
925 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 }
Barry Mienydd671972010-10-04 16:33:58 +0200927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 /**
931 * Required
932 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 * @param string
934 * @return bool
935 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100936 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300938 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 }
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200942
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500944 * Performs a Regular Expression match test.
945 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500946 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300947 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500948 * @return bool
949 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100950 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500951 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200952 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500953 }
954
955 // --------------------------------------------------------------------
956
957 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 * Match one field to another
959 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300961 * @param string field
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 * @return bool
963 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100964 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300966 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
Barry Mienydd671972010-10-04 16:33:58 +0200967
Andrey Andreev56454792012-05-17 14:32:19 +0300968 return isset($validation_array[$field]) ? ($str === $validation_array[$field]) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
Eric Barnescccde962011-12-04 00:01:17 -0500970
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100971 // --------------------------------------------------------------------
972
973 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -0300974 * Differs from another field
975 *
976 * @param string
977 * @param string field
978 * @return bool
979 */
980 public function differs($str, $field)
981 {
982 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
983 }
984
985 // --------------------------------------------------------------------
986 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200987 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100988 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200989 * Check if the input value doesn't already exist
990 * in the specified database field.
991 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100992 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300993 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100994 * @return bool
995 */
996 public function is_unique($str, $field)
997 {
Eric Barnescccde962011-12-04 00:01:17 -0500998 list($table, $field) = explode('.', $field);
999 if (isset($this->CI->db))
1000 {
1001 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
1002 return $query->num_rows() === 0;
1003 }
1004 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001005 }
Barry Mienydd671972010-10-04 16:33:58 +02001006
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 /**
1010 * Minimum Length
1011 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001013 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001015 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001016 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001018 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 {
1020 return FALSE;
1021 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001022 else
1023 {
1024 $val = (int) $val;
1025 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001026
Andrey Andreev78f55772012-04-03 19:59:08 +03001027 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001028 ? ($val <= mb_strlen($str))
1029 : ($val <= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 }
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 /**
1035 * Max Length
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001038 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001040 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001041 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001043 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 {
1045 return FALSE;
1046 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001047 else
1048 {
1049 $val = (int) $val;
1050 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001051
Andrey Andreev78f55772012-04-03 19:59:08 +03001052 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001053 ? ($val >= mb_strlen($str))
1054 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 }
Barry Mienydd671972010-10-04 16:33:58 +02001056
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001058
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 /**
1060 * Exact Length
1061 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001063 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001065 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001066 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001068 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 {
1070 return FALSE;
1071 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001072 else
1073 {
1074 $val = (int) $val;
1075 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001076
Andrey Andreev78f55772012-04-03 19:59:08 +03001077 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001078 ? (mb_strlen($str) === $val)
1079 : (strlen($str) === $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 }
Barry Mienydd671972010-10-04 16:33:58 +02001081
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001083
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 /**
1085 * Valid Email
1086 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 * @param string
1088 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001089 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001090 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001092 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 }
1094
1095 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001096
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 /**
1098 * Valid Emails
1099 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 * @param string
1101 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001102 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001103 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 {
1105 if (strpos($str, ',') === FALSE)
1106 {
1107 return $this->valid_email(trim($str));
1108 }
Barry Mienydd671972010-10-04 16:33:58 +02001109
Pascal Kriete14287f32011-02-14 13:39:34 -05001110 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001112 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 {
1114 return FALSE;
1115 }
1116 }
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 return TRUE;
1119 }
1120
1121 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001122
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 /**
1124 * Validate IP Address
1125 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001127 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001128 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001130 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001132 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 }
1134
1135 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001136
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 /**
1138 * Alpha
1139 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 * @param string
1141 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001142 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001143 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001145 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 }
Barry Mienydd671972010-10-04 16:33:58 +02001147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001149
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 /**
1151 * Alpha-numeric
1152 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 * @param string
1154 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001155 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001156 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001158 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 }
Barry Mienydd671972010-10-04 16:33:58 +02001160
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001162
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 /**
1164 * Alpha-numeric with underscores and dashes
1165 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 * @param string
1167 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001168 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001169 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001171 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 }
Barry Mienydd671972010-10-04 16:33:58 +02001173
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001175
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 /**
1177 * Numeric
1178 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 * @param string
1180 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001181 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001182 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001184 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001185
1186 }
1187
1188 // --------------------------------------------------------------------
1189
Barry Mienydd671972010-10-04 16:33:58 +02001190 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 * Integer
1192 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 * @param string
1194 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001195 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001196 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001198 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1199 }
1200
1201 // --------------------------------------------------------------------
1202
1203 /**
1204 * Decimal number
1205 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001206 * @param string
1207 * @return bool
1208 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001209 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001210 {
1211 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1212 }
1213
1214 // --------------------------------------------------------------------
1215
1216 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001217 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001218 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001219 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001220 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001221 * @return bool
1222 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001223 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001224 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001225 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001226 }
1227
1228 // --------------------------------------------------------------------
1229
1230 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001231 * Equal to or Greater than
1232 *
Nick Busey98c347d2012-02-02 11:07:03 -07001233 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001234 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001235 * @return bool
1236 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001237 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001238 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001239 return is_numeric($str) ? ($str >= $min) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001240 }
1241
1242 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001243
1244 /**
1245 * Less than
1246 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001247 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001248 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001249 * @return bool
1250 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001251 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001252 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001253 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001255
1256 // --------------------------------------------------------------------
1257
Barry Mienydd671972010-10-04 16:33:58 +02001258 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001259 * Equal to or Less than
1260 *
Nick Busey98c347d2012-02-02 11:07:03 -07001261 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001262 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001263 * @return bool
1264 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001265 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001266 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001267 return is_numeric($str) ? ($str <= $max) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001268 }
1269
1270 // --------------------------------------------------------------------
1271
1272 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001273 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001274 *
Barry Mienydd671972010-10-04 16:33:58 +02001275 * @param string
1276 * @return bool
1277 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001278 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001279 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001280 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001281 }
1282
1283 // --------------------------------------------------------------------
1284
1285 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001286 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001287 *
Barry Mienydd671972010-10-04 16:33:58 +02001288 * @param string
1289 * @return bool
1290 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001291 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001292 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001293 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001294 }
1295
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 * Valid Base64
1300 *
1301 * Tests a string for characters outside of the Base64 alphabet
1302 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1303 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 * @param string
1305 * @return bool
1306 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001307 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 {
Andrey Andreev56454792012-05-17 14:32:19 +03001309 return ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 }
Barry Mienydd671972010-10-04 16:33:58 +02001311
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001313
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 /**
1315 * Prep data for form
1316 *
1317 * This function allows HTML to be safely shown in a form.
1318 * Special characters are converted.
1319 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 * @param string
1321 * @return string
1322 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001323 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 {
1325 if (is_array($data))
1326 {
1327 foreach ($data as $key => $val)
1328 {
1329 $data[$key] = $this->prep_for_form($val);
1330 }
Barry Mienydd671972010-10-04 16:33:58 +02001331
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 return $data;
1333 }
Barry Mienydd671972010-10-04 16:33:58 +02001334
Alex Bilbied261b1e2012-06-02 11:12:16 +01001335 if ($this->_safe_form_data === FALSE OR $data === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 {
1337 return $data;
1338 }
1339
Andrey Andreev901573c2012-01-11 01:40:48 +02001340 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 }
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 * Prep URL
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 prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001353 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 {
1355 return '';
1356 }
Barry Mienydd671972010-10-04 16:33:58 +02001357
Andrey Andreev901573c2012-01-11 01:40:48 +02001358 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001360 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 }
Barry Mienydd671972010-10-04 16:33:58 +02001362
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 return $str;
1364 }
Barry Mienydd671972010-10-04 16:33:58 +02001365
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001367
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 /**
1369 * Strip Image Tags
1370 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 * @param string
1372 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001373 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001374 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001376 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 }
Barry Mienydd671972010-10-04 16:33:58 +02001378
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001380
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 /**
1382 * XSS Clean
1383 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 * @param string
1385 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001386 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001387 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 {
Derek Jones5640a712010-04-23 11:22:40 -05001389 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 }
Barry Mienydd671972010-10-04 16:33:58 +02001391
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001393
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 /**
1395 * Convert PHP tags to entities
1396 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 * @param string
1398 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001399 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001400 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 {
Andrey Andreeve446ad32012-06-15 15:23:41 +03001402 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 }
1404
JonoB099c4782012-03-04 14:37:30 +00001405 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001406
1407 /**
1408 * Reset validation vars
1409 *
1410 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001411 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001412 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001413 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001414 */
JonoB883f80f2012-03-05 09:51:27 +00001415 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001416 {
JonoB099c4782012-03-04 14:37:30 +00001417 $this->_field_data = array();
1418 $this->_config_rules = array();
1419 $this->_error_array = array();
1420 $this->_error_messages = array();
1421 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001422 }
1423
Derek Allard2067d1a2008-11-13 22:59:24 +00001424}
Derek Allard2067d1a2008-11-13 22:59:24 +00001425
1426/* End of file Form_validation.php */
Andrey Andreev31cf46e2012-03-20 15:48:00 +02001427/* Location: ./system/libraries/Form_validation.php */