blob: e50eee4f26e6473acf2a383dd6d6226b0b37fb9f [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Form Validation Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
37 */
38class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020039
Timothy Warren0688ac92012-04-20 10:25:04 -040040 /**
41 * Reference to the CodeIgniter instance
42 *
43 * @var object
44 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010045 protected $CI;
Andrey Andreev56454792012-05-17 14:32:19 +030046
Timothy Warren0688ac92012-04-20 10:25:04 -040047 /**
48 * Validation data for the current form submission
49 *
50 * @var array
51 */
Andrey Andreev56454792012-05-17 14:32:19 +030052 protected $_field_data = array();
53
Timothy Warren0688ac92012-04-20 10:25:04 -040054 /**
55 * Validation rules for the current form
56 *
57 * @var array
58 */
Andrey Andreev56454792012-05-17 14:32:19 +030059 protected $_config_rules = array();
60
Timothy Warren0688ac92012-04-20 10:25:04 -040061 /**
62 * Array of validation errors
63 *
64 * @var array
65 */
Andrey Andreev78f55772012-04-03 19:59:08 +030066 protected $_error_array = array();
Andrey Andreev56454792012-05-17 14:32:19 +030067
Timothy Warren0688ac92012-04-20 10:25:04 -040068 /**
69 * Array of custom error messages
70 *
71 * @var array
72 */
Andrey Andreev78f55772012-04-03 19:59:08 +030073 protected $_error_messages = array();
Andrey Andreev56454792012-05-17 14:32:19 +030074
Timothy Warren0688ac92012-04-20 10:25:04 -040075 /**
76 * Start tag for error wrapping
77 *
78 * @var string
79 */
Andrey Andreev78f55772012-04-03 19:59:08 +030080 protected $_error_prefix = '<p>';
Andrey Andreev56454792012-05-17 14:32:19 +030081
Timothy Warren0688ac92012-04-20 10:25:04 -040082 /**
83 * End tag for error wrapping
Andrey Andreev56454792012-05-17 14:32:19 +030084 *
Timothy Warren0688ac92012-04-20 10:25:04 -040085 * @var string
86 */
Andrey Andreev78f55772012-04-03 19:59:08 +030087 protected $_error_suffix = '</p>';
Andrey Andreev56454792012-05-17 14:32:19 +030088
Timothy Warren0688ac92012-04-20 10:25:04 -040089 /**
90 * Custom error message
91 *
92 * @var string
93 */
Andrey Andreev78f55772012-04-03 19:59:08 +030094 protected $error_string = '';
Andrey Andreev56454792012-05-17 14:32:19 +030095
Timothy Warren0688ac92012-04-20 10:25:04 -040096 /**
97 * Whether the form data has been validated as safe
98 *
99 * @var bool
100 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300101 protected $_safe_form_data = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300102
Timothy Warren0688ac92012-04-20 10:25:04 -0400103 /**
104 * Custom data to validate
105 *
106 * @var array
107 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300108 protected $validation_data = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000109
Timothy Warren0688ac92012-04-20 10:25:04 -0400110 /**
111 * Initialize Form_Validation class
112 *
Andrey Andreev56454792012-05-17 14:32:19 +0300113 * @param array $rules
114 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -0400115 */
Greg Akera9263282010-11-10 15:26:43 -0600116 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +0200117 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200119
Mike Funk326a5e72012-02-24 10:06:28 -0500120 // applies delimiters set in config file.
Mike Funk7f42d062012-03-08 09:00:57 -0500121 if (isset($rules['error_prefix']))
122 {
123 $this->_error_prefix = $rules['error_prefix'];
124 unset($rules['error_prefix']);
125 }
126 if (isset($rules['error_suffix']))
127 {
128 $this->_error_suffix = $rules['error_suffix'];
129 unset($rules['error_suffix']);
130 }
Andrey Andreev31cf46e2012-03-20 15:48:00 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 // Validation rules can be stored in a config file.
133 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // Automatically load the form helper
136 $this->CI->load->helper('form');
137
Andrey Andreev901573c2012-01-11 01:40:48 +0200138 log_message('debug', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 /**
144 * Set Rules
145 *
146 * This function takes an array of field names and validation
147 * rules as input, validates the info, and stores it
148 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400149 * @param mixed $field
150 * @param string $label
151 * @param mixed $rules
Andrey Andreev78f55772012-04-03 19:59:08 +0300152 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100154 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
156 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +0000157 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200158 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Greg Aker9f9af602010-11-10 15:41:51 -0600160 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
tiyowanc2acb232012-03-14 21:24:00 +0400163 // If an array was passed via the first parameter instead of individual string
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 // values we cycle through it and recursively call this function.
165 if (is_array($field))
166 {
167 foreach ($field as $row)
168 {
169 // Houston, we have a problem...
Andrey Andreev78f55772012-04-03 19:59:08 +0300170 if ( ! isset($row['field'], $row['rules']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
172 continue;
173 }
174
175 // If the field label wasn't passed we use the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300176 $label = isset($row['label']) ? $row['label'] : $row['field'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000177
178 // Here we go!
179 $this->set_rules($row['field'], $label, $row['rules']);
180 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300181
Greg Aker9f9af602010-11-10 15:41:51 -0600182 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Kevin Wood-Friend6a56d502012-06-12 09:54:01 -0400185 // Convert an array of rules to a string
186 if (is_array($rules))
187 {
188 $rules = implode('|', $rules);
189 }
190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // No fields? Nothing to do...
Alex Bilbied261b1e2012-06-02 11:12:16 +0100192 if ( ! is_string($field) OR ! is_string($rules) OR $field === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Greg Aker9f9af602010-11-10 15:41:51 -0600194 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
196
197 // If the field label wasn't passed we use the field name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100198 $label = ($label === '') ? $field : $label;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200200 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200201 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200202 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200203 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // Note: Due to a bug in current() that affects some versions
205 // of PHP we can not pass function call directly into it
206 $x = explode('[', $field);
207 $indexes[] = current($x);
208
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200209 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100211 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200213 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 $is_array = TRUE;
218 }
219 else
220 {
Barry Mienydd671972010-10-04 16:33:58 +0200221 $indexes = array();
222 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
225 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300227 'field' => $field,
228 'label' => $label,
229 'rules' => $rules,
230 'is_array' => $is_array,
231 'keys' => $indexes,
232 'postdata' => NULL,
233 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000234 );
Greg Aker9f9af602010-11-10 15:41:51 -0600235
236 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 }
238
239 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 /**
JonoB099c4782012-03-04 14:37:30 +0000242 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200243 *
JonoB099c4782012-03-04 14:37:30 +0000244 * If an array is set through this method, then this array will
245 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200246 *
247 * Note that if you are validating multiple arrays, then the
248 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000249 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200250 *
251 * @param array $data
252 * @return void
JonoB099c4782012-03-04 14:37:30 +0000253 */
254 public function set_data($data = '')
255 {
256 if ( ! empty($data) && is_array($data))
257 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200258 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000259 }
260 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200261
JonoB099c4782012-03-04 14:37:30 +0000262 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000263
264 /**
265 * Set Error Message
266 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300267 * Lets users set their own error messages on the fly. Note:
268 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300270 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300272 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100274 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 if ( ! is_array($lang))
277 {
278 $lang = array($lang => $val);
279 }
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600282 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 /**
288 * Set The Error Delimiter
289 *
290 * Permits a prefix/suffix to be added to each error message
291 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 * @param string
293 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300294 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200295 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100296 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
298 $this->_error_prefix = $prefix;
299 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600300 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302
303 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 /**
306 * Get Error Message
307 *
308 * Gets the error message associated with a particular field
309 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 * @param string the field name
Timothy Warren0688ac92012-04-20 10:25:04 -0400311 * @param string the html start tag
312 * @param strign the html end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300313 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200314 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100315 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200316 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300317 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 return '';
320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Alex Bilbied261b1e2012-06-02 11:12:16 +0100322 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
324 $prefix = $this->_error_prefix;
325 }
326
Alex Bilbied261b1e2012-06-02 11:12:16 +0100327 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 $suffix = $this->_error_suffix;
330 }
331
332 return $prefix.$this->_field_data[$field]['error'].$suffix;
333 }
334
335 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100338 * Get Array of Error Messages
339 *
340 * Returns the error messages as an array
341 *
342 * @return array
343 */
344 public function error_array()
345 {
346 return $this->_error_array;
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 * Error String
353 *
354 * Returns the error messages as a string, wrapped in the error delimiters
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @param string
357 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300358 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200359 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100360 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 // No errrors, validation passes!
363 if (count($this->_error_array) === 0)
364 {
365 return '';
366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Alex Bilbied261b1e2012-06-02 11:12:16 +0100368 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
370 $prefix = $this->_error_prefix;
371 }
372
Alex Bilbied261b1e2012-06-02 11:12:16 +0100373 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
375 $suffix = $this->_error_suffix;
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 // Generate the error string
379 $str = '';
380 foreach ($this->_error_array as $val)
381 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100382 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 $str .= $prefix.$val.$suffix."\n";
385 }
386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 return $str;
389 }
390
391 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 /**
394 * Run the Validator
395 *
396 * This function does all the work.
397 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400398 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200400 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100401 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500403 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300404 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000405 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
407 return FALSE;
408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 // Does the _field_data array containing the validation rules exist?
411 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200412 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500414 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200415 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 return FALSE;
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // Is there a validation rule for the particular URI being accessed?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100421 $uri = ($group === '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200422
Alex Bilbied261b1e2012-06-02 11:12:16 +0100423 if ($uri !== '' && isset($this->_config_rules[$uri]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
425 $this->set_rules($this->_config_rules[$uri]);
426 }
427 else
428 {
429 $this->set_rules($this->_config_rules);
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Andrey Andreev901573c2012-01-11 01:40:48 +0200432 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200433 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200435 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 return FALSE;
437 }
438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 // Load the language file containing error messages
441 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200442
Andrey Andreev751f2472012-11-03 18:26:27 +0200443 // Cycle through the rules for each field and match the corresponding $validation_data item
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200445 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200446 // Fetch the data from the validation_data array item 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 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Andrey Andreev751f2472012-11-03 18:26:27 +0200458 // Execute validation rules
459 // Note: A second foreach (for now) is required in order to avoid false-positives
460 // for rules like 'matches', which correlate to other validation fields.
461 foreach ($this->_field_data as $field => $row)
462 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300463 // Don't try to validate if we have no rules set
464 if (empty($row['rules']))
465 {
466 continue;
467 }
468
Barry Mienydd671972010-10-04 16:33:58 +0200469 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
471
472 // Did we end up with any errors?
473 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 if ($total_errors > 0)
475 {
476 $this->_safe_form_data = TRUE;
477 }
478
479 // Now we need to re-set the POST data with the new, processed data
480 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200481
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200482 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
484
485 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 /**
488 * Traverse a multidimensional $_POST array index until the data is found
489 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @param array
491 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300492 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200494 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100495 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200497 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200499 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 return $array;
503 }
504
505 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 /**
508 * Re-populate the _POST array with our finalized and processed data
509 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200510 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200511 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100512 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
514 foreach ($this->_field_data as $field => $row)
515 {
516 if ( ! is_null($row['postdata']))
517 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200518 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
520 if (isset($_POST[$row['field']]))
521 {
522 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
523 }
524 }
525 else
526 {
Derek Jones63eeae32009-02-10 19:08:56 +0000527 // start with a reference
528 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Jones63eeae32009-02-10 19:08:56 +0000530 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200531 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
Derek Jones63eeae32009-02-10 19:08:56 +0000533 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
535 else
536 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 foreach ($row['keys'] as $val)
538 {
Derek Jones63eeae32009-02-10 19:08:56 +0000539 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
541 }
Derek Jones63eeae32009-02-10 19:08:56 +0000542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000544 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 $array = array();
546 foreach ($row['postdata'] as $k => $v)
547 {
548 $array[$k] = $this->prep_for_form($v);
549 }
Derek Jones63eeae32009-02-10 19:08:56 +0000550
551 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
553 else
Derek Jones63eeae32009-02-10 19:08:56 +0000554 {
555 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
558 }
559 }
560 }
561
562 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 /**
565 * Executes the Validation routines
566 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 * @param array
568 * @param array
569 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300570 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200572 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100573 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
575 // If the $_POST data is an array we will run a recursive call
576 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200577 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 foreach ($postdata as $key => $val)
579 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300580 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 }
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 return;
584 }
Barry Mienydd671972010-10-04 16:33:58 +0200585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 // If the field is blank, but NOT required, no further tests are necessary
587 $callback = FALSE;
Andrey Andreev6de924c2012-01-20 13:18:18 +0200588 if ( ! in_array('required', $rules) && is_null($postdata))
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
590 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200591 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 $callback = TRUE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300594 $rules = array(1 => $match[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 }
596 else
597 {
598 return;
599 }
600 }
601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev6de924c2012-01-20 13:18:18 +0200603 if (is_null($postdata) && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
605 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
606 {
607 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300608 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200609
Andrey Andreev56454792012-05-17 14:32:19 +0300610 if (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
612 $line = $this->_error_messages[$type];
613 }
Andrey Andreev56454792012-05-17 14:32:19 +0300614 elseif (FALSE === ($line = $this->CI->lang->line($type)))
615 {
616 $line = 'The field was not set';
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // Build the error message
620 $message = sprintf($line, $this->_translate_fieldname($row['label']));
621
622 // Save the error message
623 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 if ( ! isset($this->_error_array[$row['field']]))
626 {
627 $this->_error_array[$row['field']] = $message;
628 }
629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 return;
632 }
633
634 // --------------------------------------------------------------------
635
636 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300637 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
639 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 // We set the $postdata variable with the current data in our master array so that
642 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100643 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 // We shouldn't need this safety, but just in case there isn't an array index
646 // associated with this cycle we'll bail out
647 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
648 {
649 continue;
650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
653 $_in_array = TRUE;
654 }
655 else
656 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300657 // If we get an array field, but it's not expected - then it is most likely
658 // somebody messing with the form on the client side, so we'll just consider
659 // it an empty field
660 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
661 ? NULL
662 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 }
664
Barry Mienydd671972010-10-04 16:33:58 +0200665 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200667 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
669 $rule = substr($rule, 9);
670 $callback = TRUE;
671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 // Strip the parameter (if exists) from the rule
674 // Rules can contain a parameter: max_length[5]
675 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200676 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
678 $rule = $match[1];
679 $param = $match[2];
680 }
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 // Call the function that corresponds to the rule
683 if ($callback === TRUE)
684 {
685 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200686 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200687 log_message('debug', 'Unable to find callback validation rule: '.$rule);
688 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200690 else
691 {
692 // Run the function and grab the result
693 $result = $this->CI->$rule($postdata, $param);
694 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000695
696 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200697 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300699 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 }
701 else
702 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300703 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200707 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000709 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 }
711 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300712 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200713 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300714 // If our own wrapper function doesn't exist we see if a native PHP function does.
715 // Users can use any native PHP function call that has one param.
716 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 {
Andrey Andreev320d37c2012-04-03 20:21:39 +0300718 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200719
Andrey Andreev78f55772012-04-03 19:59:08 +0300720 if ($_in_array === TRUE)
721 {
722 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 }
patwork02404a12011-04-08 15:45:46 +0200724 else
725 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300726 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200727 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200729 else
730 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300731 log_message('debug', 'Unable to find validation rule: '.$rule);
732 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200733 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300734 }
735 else
736 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 $result = $this->$rule($postdata, $param);
738
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200739 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300741 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
743 else
744 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300745 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 }
747 }
Barry Mienydd671972010-10-04 16:33:58 +0200748
Andrey Andreev901573c2012-01-11 01:40:48 +0200749 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200751 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 if ( ! isset($this->_error_messages[$rule]))
753 {
754 if (FALSE === ($line = $this->CI->lang->line($rule)))
755 {
756 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200757 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 }
759 else
760 {
761 $line = $this->_error_messages[$rule];
762 }
Barry Mienydd671972010-10-04 16:33:58 +0200763
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200765 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200766 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 {
Pascal Krietec1895832009-10-13 12:56:43 +0000768 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 }
Barry Mienydd671972010-10-04 16:33:58 +0200770
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 // Build the error message
772 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
773
774 // Save the error message
775 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 if ( ! isset($this->_error_array[$row['field']]))
778 {
779 $this->_error_array[$row['field']] = $message;
780 }
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 return;
783 }
784 }
785 }
786
787 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 /**
790 * Translate a field name
791 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 * @param string the field name
793 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200794 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100795 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 {
797 // Do we need to translate the field name?
798 // We look for the prefix lang: to determine this
Andrey Andreev901573c2012-01-11 01:40:48 +0200799 if (strpos($fieldname, 'lang:') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 {
801 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200802 $line = substr($fieldname, 5);
803
Derek Jones37f4b9c2011-07-01 17:56:50 -0500804 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
806 {
807 return $line;
808 }
809 }
810
811 return $fieldname;
812 }
813
814 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 /**
817 * Get the value from a form
818 *
819 * Permits you to repopulate a form field with the value it was submitted
820 * with, or, if that value doesn't exist, with the default
821 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 * @param string the field name
823 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200824 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200825 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100826 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200828 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 {
830 return $default;
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Phil Sturgeon5c561802011-01-05 16:31:59 +0000833 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600834 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000835 if (is_array($this->_field_data[$field]['postdata']))
836 {
837 return array_shift($this->_field_data[$field]['postdata']);
838 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 return $this->_field_data[$field]['postdata'];
841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 /**
846 * Set Select
847 *
848 * Enables pull-down lists to be set to the value the user
849 * selected in the event of an error
850 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 * @param string
852 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400853 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200855 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100856 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200857 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200858 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200860 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 if (is_array($field))
865 {
866 if ( ! in_array($value, $field))
867 {
868 return '';
869 }
870 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100871 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200873 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 }
Barry Mienydd671972010-10-04 16:33:58 +0200875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 return ' selected="selected"';
877 }
Barry Mienydd671972010-10-04 16:33:58 +0200878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 /**
882 * Set Radio
883 *
884 * Enables radio buttons to be set to the value the user
885 * selected in the event of an error
886 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 * @param string
888 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400889 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200891 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100892 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200894 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200896 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 }
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 if (is_array($field))
901 {
902 if ( ! in_array($value, $field))
903 {
904 return '';
905 }
906 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100907 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200909 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 }
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 return ' checked="checked"';
913 }
Barry Mienydd671972010-10-04 16:33:58 +0200914
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200916
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 /**
918 * Set Checkbox
919 *
920 * Enables checkboxes to be set to the value the user
921 * selected in the event of an error
922 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 * @param string
924 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400925 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200927 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100928 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200930 // Logic is exactly the same as for radio fields
931 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 }
Barry Mienydd671972010-10-04 16:33:58 +0200933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 /**
937 * Required
938 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * @param string
940 * @return bool
941 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100942 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300944 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500950 * Performs a Regular Expression match test.
951 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500952 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300953 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500954 * @return bool
955 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100956 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500957 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200958 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500959 }
960
961 // --------------------------------------------------------------------
962
963 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 * Match one field to another
965 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +0300966 * @param string $str string to compare against
967 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 * @return bool
969 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100970 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +0300972 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
973 ? ($str === $this->_field_data[$field]['postdata'])
974 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 }
Eric Barnescccde962011-12-04 00:01:17 -0500976
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100977 // --------------------------------------------------------------------
978
979 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -0300980 * Differs from another field
981 *
982 * @param string
983 * @param string field
984 * @return bool
985 */
986 public function differs($str, $field)
987 {
988 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
989 }
990
991 // --------------------------------------------------------------------
Andrey Andreev9ae82fa2012-10-11 20:58:45 +0300992
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -0300993 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200994 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100995 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200996 * Check if the input value doesn't already exist
997 * in the specified database field.
998 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100999 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001000 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001001 * @return bool
1002 */
1003 public function is_unique($str, $field)
1004 {
Eric Barnescccde962011-12-04 00:01:17 -05001005 list($table, $field) = explode('.', $field);
1006 if (isset($this->CI->db))
1007 {
1008 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
1009 return $query->num_rows() === 0;
1010 }
1011 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001012 }
Barry Mienydd671972010-10-04 16:33:58 +02001013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001015
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 /**
1017 * Minimum Length
1018 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001020 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001022 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001023 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001025 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
1027 return FALSE;
1028 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001029 else
1030 {
1031 $val = (int) $val;
1032 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001033
Andrey Andreev78f55772012-04-03 19:59:08 +03001034 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001035 ? ($val <= mb_strlen($str))
1036 : ($val <= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 }
Barry Mienydd671972010-10-04 16:33:58 +02001038
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001040
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 /**
1042 * Max Length
1043 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001045 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001047 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001048 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001050 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 {
1052 return FALSE;
1053 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001054 else
1055 {
1056 $val = (int) $val;
1057 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001058
Andrey Andreev78f55772012-04-03 19:59:08 +03001059 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001060 ? ($val >= mb_strlen($str))
1061 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 }
Barry Mienydd671972010-10-04 16:33:58 +02001063
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001065
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 /**
1067 * Exact Length
1068 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001070 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001072 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001073 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001075 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 {
1077 return FALSE;
1078 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001079 else
1080 {
1081 $val = (int) $val;
1082 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001083
Andrey Andreev78f55772012-04-03 19:59:08 +03001084 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001085 ? (mb_strlen($str) === $val)
1086 : (strlen($str) === $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 }
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001090
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 /**
1092 * Valid Email
1093 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 * @param string
1095 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001096 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001097 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001099 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 }
1101
1102 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001103
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 /**
1105 * Valid Emails
1106 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 * @param string
1108 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001109 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001110 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 {
1112 if (strpos($str, ',') === FALSE)
1113 {
1114 return $this->valid_email(trim($str));
1115 }
Barry Mienydd671972010-10-04 16:33:58 +02001116
Pascal Kriete14287f32011-02-14 13:39:34 -05001117 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001119 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 {
1121 return FALSE;
1122 }
1123 }
Barry Mienydd671972010-10-04 16:33:58 +02001124
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 return TRUE;
1126 }
1127
1128 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 /**
1131 * Validate IP Address
1132 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001134 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001135 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001137 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001139 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 }
1141
1142 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001143
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 /**
1145 * Alpha
1146 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 * @param string
1148 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001149 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001150 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001152 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 }
Barry Mienydd671972010-10-04 16:33:58 +02001154
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001156
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 /**
1158 * Alpha-numeric
1159 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 * @param string
1161 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001162 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001163 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001165 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 }
Barry Mienydd671972010-10-04 16:33:58 +02001167
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001169
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 /**
1171 * Alpha-numeric with underscores and dashes
1172 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 * @param string
1174 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001175 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001176 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001178 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 }
Barry Mienydd671972010-10-04 16:33:58 +02001180
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001182
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 /**
1184 * Numeric
1185 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 * @param string
1187 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001188 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001189 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001191 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001192
1193 }
1194
1195 // --------------------------------------------------------------------
1196
Barry Mienydd671972010-10-04 16:33:58 +02001197 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 * Integer
1199 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 * @param string
1201 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001202 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001203 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001205 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1206 }
1207
1208 // --------------------------------------------------------------------
1209
1210 /**
1211 * Decimal number
1212 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001213 * @param string
1214 * @return bool
1215 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001216 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001217 {
1218 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1219 }
1220
1221 // --------------------------------------------------------------------
1222
1223 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001224 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001225 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001226 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001227 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001228 * @return bool
1229 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001230 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001231 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001232 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001233 }
1234
1235 // --------------------------------------------------------------------
1236
1237 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001238 * Equal to or Greater than
1239 *
Nick Busey98c347d2012-02-02 11:07:03 -07001240 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001241 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001242 * @return bool
1243 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001244 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001245 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001246 return is_numeric($str) ? ($str >= $min) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001247 }
1248
1249 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001250
1251 /**
1252 * Less than
1253 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001254 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001255 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001256 * @return bool
1257 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001258 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001259 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001260 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001262
1263 // --------------------------------------------------------------------
1264
Barry Mienydd671972010-10-04 16:33:58 +02001265 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001266 * Equal to or Less than
1267 *
Nick Busey98c347d2012-02-02 11:07:03 -07001268 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001269 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001270 * @return bool
1271 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001272 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001273 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001274 return is_numeric($str) ? ($str <= $max) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001275 }
1276
1277 // --------------------------------------------------------------------
1278
1279 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001280 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001281 *
Barry Mienydd671972010-10-04 16:33:58 +02001282 * @param string
1283 * @return bool
1284 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001285 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001286 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001287 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001288 }
1289
1290 // --------------------------------------------------------------------
1291
1292 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001293 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001294 *
Barry Mienydd671972010-10-04 16:33:58 +02001295 * @param string
1296 * @return bool
1297 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001298 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001299 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001300 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001301 }
1302
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001304
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 /**
1306 * Valid Base64
1307 *
1308 * Tests a string for characters outside of the Base64 alphabet
1309 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1310 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 * @param string
1312 * @return bool
1313 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001314 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 {
Andrey Andreev56454792012-05-17 14:32:19 +03001316 return ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 }
Barry Mienydd671972010-10-04 16:33:58 +02001318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001320
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 /**
1322 * Prep data for form
1323 *
1324 * This function allows HTML to be safely shown in a form.
1325 * Special characters are converted.
1326 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 * @param string
1328 * @return string
1329 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001330 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001332 if ($this->_safe_form_data === FALSE OR empty($data))
1333 {
1334 return $data;
1335 }
1336
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 if (is_array($data))
1338 {
1339 foreach ($data as $key => $val)
1340 {
1341 $data[$key] = $this->prep_for_form($val);
1342 }
Barry Mienydd671972010-10-04 16:33:58 +02001343
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 return $data;
1345 }
Barry Mienydd671972010-10-04 16:33:58 +02001346
Andrey Andreev901573c2012-01-11 01:40:48 +02001347 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 }
Barry Mienydd671972010-10-04 16:33:58 +02001349
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001351
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 /**
1353 * Prep URL
1354 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 * @param string
1356 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001357 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001358 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001360 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 {
1362 return '';
1363 }
Barry Mienydd671972010-10-04 16:33:58 +02001364
Andrey Andreev901573c2012-01-11 01:40:48 +02001365 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001367 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 }
Barry Mienydd671972010-10-04 16:33:58 +02001369
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 return $str;
1371 }
Barry Mienydd671972010-10-04 16:33:58 +02001372
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001374
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 /**
1376 * Strip Image Tags
1377 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 * @param string
1379 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001380 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001381 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001383 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 }
Barry Mienydd671972010-10-04 16:33:58 +02001385
Derek Allard2067d1a2008-11-13 22:59:24 +00001386 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001387
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 /**
1389 * XSS Clean
1390 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 * @param string
1392 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001393 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001394 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 {
Derek Jones5640a712010-04-23 11:22:40 -05001396 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 }
Barry Mienydd671972010-10-04 16:33:58 +02001398
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001400
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 /**
1402 * Convert PHP tags to entities
1403 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 * @param string
1405 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001406 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001407 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 {
Andrey Andreeve446ad32012-06-15 15:23:41 +03001409 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 }
1411
JonoB099c4782012-03-04 14:37:30 +00001412 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001413
1414 /**
1415 * Reset validation vars
1416 *
1417 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001418 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001419 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001420 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001421 */
JonoB883f80f2012-03-05 09:51:27 +00001422 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001423 {
JonoB099c4782012-03-04 14:37:30 +00001424 $this->_field_data = array();
1425 $this->_config_rules = array();
1426 $this->_error_array = array();
1427 $this->_error_messages = array();
1428 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001429 }
1430
Derek Allard2067d1a2008-11-13 22:59:24 +00001431}
Derek Allard2067d1a2008-11-13 22:59:24 +00001432
1433/* End of file Form_validation.php */
Andrey Andreev31cf46e2012-03-20 15:48:00 +02001434/* Location: ./system/libraries/Form_validation.php */