blob: 37eb7a9cd6f9fff402e2d8632dc14b5b6ca4a132 [file] [log] [blame]
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnescccde962011-12-04 00:01:17 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnescccde962011-12-04 00:01:17 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreevb998f3e2012-01-24 15:31:26 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Form Validation Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
36 */
37class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020038
Timothy Warren0688ac92012-04-20 10:25:04 -040039 /**
40 * Reference to the CodeIgniter instance
41 *
42 * @var object
43 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010044 protected $CI;
Andrey Andreev56454792012-05-17 14:32:19 +030045
Timothy Warren0688ac92012-04-20 10:25:04 -040046 /**
47 * Validation data for the current form submission
48 *
49 * @var array
50 */
Andrey Andreev56454792012-05-17 14:32:19 +030051 protected $_field_data = array();
52
Timothy Warren0688ac92012-04-20 10:25:04 -040053 /**
54 * Validation rules for the current form
55 *
56 * @var array
57 */
Andrey Andreev56454792012-05-17 14:32:19 +030058 protected $_config_rules = array();
59
Timothy Warren0688ac92012-04-20 10:25:04 -040060 /**
61 * Array of validation errors
62 *
63 * @var array
64 */
Andrey Andreev78f55772012-04-03 19:59:08 +030065 protected $_error_array = array();
Andrey Andreev56454792012-05-17 14:32:19 +030066
Timothy Warren0688ac92012-04-20 10:25:04 -040067 /**
68 * Array of custom error messages
69 *
70 * @var array
71 */
Andrey Andreev78f55772012-04-03 19:59:08 +030072 protected $_error_messages = array();
Andrey Andreev56454792012-05-17 14:32:19 +030073
Timothy Warren0688ac92012-04-20 10:25:04 -040074 /**
75 * Start tag for error wrapping
76 *
77 * @var string
78 */
Andrey Andreev78f55772012-04-03 19:59:08 +030079 protected $_error_prefix = '<p>';
Andrey Andreev56454792012-05-17 14:32:19 +030080
Timothy Warren0688ac92012-04-20 10:25:04 -040081 /**
82 * End tag for error wrapping
Andrey Andreev56454792012-05-17 14:32:19 +030083 *
Timothy Warren0688ac92012-04-20 10:25:04 -040084 * @var string
85 */
Andrey Andreev78f55772012-04-03 19:59:08 +030086 protected $_error_suffix = '</p>';
Andrey Andreev56454792012-05-17 14:32:19 +030087
Timothy Warren0688ac92012-04-20 10:25:04 -040088 /**
89 * Custom error message
90 *
91 * @var string
92 */
Andrey Andreev78f55772012-04-03 19:59:08 +030093 protected $error_string = '';
Andrey Andreev56454792012-05-17 14:32:19 +030094
Timothy Warren0688ac92012-04-20 10:25:04 -040095 /**
96 * Whether the form data has been validated as safe
97 *
98 * @var bool
99 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300100 protected $_safe_form_data = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300101
Timothy Warren0688ac92012-04-20 10:25:04 -0400102 /**
103 * Custom data to validate
104 *
105 * @var array
106 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300107 protected $validation_data = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
Timothy Warren0688ac92012-04-20 10:25:04 -0400109 /**
110 * Initialize Form_Validation class
111 *
Andrey Andreev56454792012-05-17 14:32:19 +0300112 * @param array $rules
113 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -0400114 */
Greg Akera9263282010-11-10 15:26:43 -0600115 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +0200116 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200118
Mike Funk326a5e72012-02-24 10:06:28 -0500119 // applies delimiters set in config file.
Mike Funk7f42d062012-03-08 09:00:57 -0500120 if (isset($rules['error_prefix']))
121 {
122 $this->_error_prefix = $rules['error_prefix'];
123 unset($rules['error_prefix']);
124 }
125 if (isset($rules['error_suffix']))
126 {
127 $this->_error_suffix = $rules['error_suffix'];
128 unset($rules['error_suffix']);
129 }
Andrey Andreev31cf46e2012-03-20 15:48:00 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 // Validation rules can be stored in a config file.
132 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // Automatically load the form helper
135 $this->CI->load->helper('form');
136
137 // Set the character encoding in MB.
tiyowan5b9fd2d2012-03-12 20:26:59 +0400138 if (MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
140 mb_internal_encoding($this->CI->config->item('charset'));
141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Andrey Andreev901573c2012-01-11 01:40:48 +0200143 log_message('debug', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 /**
149 * Set Rules
150 *
151 * This function takes an array of field names and validation
152 * rules as input, validates the info, and stores it
153 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400154 * @param mixed $field
155 * @param string $label
156 * @param mixed $rules
Andrey Andreev78f55772012-04-03 19:59:08 +0300157 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100159 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
161 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +0000162 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200163 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Greg Aker9f9af602010-11-10 15:41:51 -0600165 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
tiyowanc2acb232012-03-14 21:24:00 +0400168 // If an array was passed via the first parameter instead of individual string
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // values we cycle through it and recursively call this function.
170 if (is_array($field))
171 {
172 foreach ($field as $row)
173 {
174 // Houston, we have a problem...
Andrey Andreev78f55772012-04-03 19:59:08 +0300175 if ( ! isset($row['field'], $row['rules']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
177 continue;
178 }
179
180 // If the field label wasn't passed we use the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300181 $label = isset($row['label']) ? $row['label'] : $row['field'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000182
183 // Here we go!
184 $this->set_rules($row['field'], $label, $row['rules']);
185 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300186
Greg Aker9f9af602010-11-10 15:41:51 -0600187 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Kevin Wood-Friend6a56d502012-06-12 09:54:01 -0400190 // Convert an array of rules to a string
191 if (is_array($rules))
192 {
193 $rules = implode('|', $rules);
194 }
195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 // No fields? Nothing to do...
Alex Bilbied261b1e2012-06-02 11:12:16 +0100197 if ( ! is_string($field) OR ! is_string($rules) OR $field === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Greg Aker9f9af602010-11-10 15:41:51 -0600199 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
201
202 // If the field label wasn't passed we use the field name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100203 $label = ($label === '') ? $field : $label;
Derek Allard2067d1a2008-11-13 22:59:24 +0000204
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200205 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200206 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200207 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200208 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 // Note: Due to a bug in current() that affects some versions
210 // of PHP we can not pass function call directly into it
211 $x = explode('[', $field);
212 $indexes[] = current($x);
213
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200214 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100216 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200218 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 $is_array = TRUE;
223 }
224 else
225 {
Barry Mienydd671972010-10-04 16:33:58 +0200226 $indexes = array();
227 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
230 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300232 'field' => $field,
233 'label' => $label,
234 'rules' => $rules,
235 'is_array' => $is_array,
236 'keys' => $indexes,
237 'postdata' => NULL,
238 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000239 );
Greg Aker9f9af602010-11-10 15:41:51 -0600240
241 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243
244 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 /**
JonoB099c4782012-03-04 14:37:30 +0000247 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200248 *
JonoB099c4782012-03-04 14:37:30 +0000249 * If an array is set through this method, then this array will
250 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200251 *
252 * Note that if you are validating multiple arrays, then the
253 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000254 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200255 *
256 * @param array $data
257 * @return void
JonoB099c4782012-03-04 14:37:30 +0000258 */
259 public function set_data($data = '')
260 {
261 if ( ! empty($data) && is_array($data))
262 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200263 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000264 }
265 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200266
JonoB099c4782012-03-04 14:37:30 +0000267 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000268
269 /**
270 * Set Error Message
271 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300272 * Lets users set their own error messages on the fly. Note:
273 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300275 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300277 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100279 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 if ( ! is_array($lang))
282 {
283 $lang = array($lang => $val);
284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600287 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 /**
293 * Set The Error Delimiter
294 *
295 * Permits a prefix/suffix to be added to each error message
296 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 * @param string
298 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300299 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200300 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100301 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
303 $this->_error_prefix = $prefix;
304 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600305 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307
308 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 /**
311 * Get Error Message
312 *
313 * Gets the error message associated with a particular field
314 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * @param string the field name
Timothy Warren0688ac92012-04-20 10:25:04 -0400316 * @param string the html start tag
317 * @param strign the html end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300318 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200319 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100320 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200321 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300322 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
324 return '';
325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Alex Bilbied261b1e2012-06-02 11:12:16 +0100327 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 $prefix = $this->_error_prefix;
330 }
331
Alex Bilbied261b1e2012-06-02 11:12:16 +0100332 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 $suffix = $this->_error_suffix;
335 }
336
337 return $prefix.$this->_field_data[$field]['error'].$suffix;
338 }
339
340 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100343 * Get Array of Error Messages
344 *
345 * Returns the error messages as an array
346 *
347 * @return array
348 */
349 public function error_array()
350 {
351 return $this->_error_array;
352 }
353
354 // --------------------------------------------------------------------
355
356 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 * Error String
358 *
359 * Returns the error messages as a string, wrapped in the error delimiters
360 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * @param string
362 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300363 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200364 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100365 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
367 // No errrors, validation passes!
368 if (count($this->_error_array) === 0)
369 {
370 return '';
371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Alex Bilbied261b1e2012-06-02 11:12:16 +0100373 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
375 $prefix = $this->_error_prefix;
376 }
377
Alex Bilbied261b1e2012-06-02 11:12:16 +0100378 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 $suffix = $this->_error_suffix;
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 // Generate the error string
384 $str = '';
385 foreach ($this->_error_array as $val)
386 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100387 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
389 $str .= $prefix.$val.$suffix."\n";
390 }
391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 return $str;
394 }
395
396 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 /**
399 * Run the Validator
400 *
401 * This function does all the work.
402 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400403 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200405 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100406 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500408 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300409 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000410 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
412 return FALSE;
413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 // Does the _field_data array containing the validation rules exist?
416 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200417 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500419 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200420 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 return FALSE;
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // Is there a validation rule for the particular URI being accessed?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100426 $uri = ($group === '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200427
Alex Bilbied261b1e2012-06-02 11:12:16 +0100428 if ($uri !== '' && isset($this->_config_rules[$uri]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
430 $this->set_rules($this->_config_rules[$uri]);
431 }
432 else
433 {
434 $this->set_rules($this->_config_rules);
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Andrey Andreev901573c2012-01-11 01:40:48 +0200437 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200438 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200440 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 return FALSE;
442 }
443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 // Load the language file containing error messages
446 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200447
448 // Cycle through the rules for each field, match the
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 // corresponding $_POST item and test for errors
450 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200451 {
JonoB099c4782012-03-04 14:37:30 +0000452 // 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 +0000453 // 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 +0200454 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
JonoB099c4782012-03-04 14:37:30 +0000456 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Andrey Andreevfff6c2a2012-05-13 22:15:23 +0300458 elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300460 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
463 $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 {
574 $this->_execute($row, $rules, $val, $cycles);
575 $cycles++;
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 return;
579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 // If the field is blank, but NOT required, no further tests are necessary
582 $callback = FALSE;
Andrey Andreev6de924c2012-01-20 13:18:18 +0200583 if ( ! in_array('required', $rules) && is_null($postdata))
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200586 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 $callback = TRUE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300589 $rules = array(1 => $match[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
591 else
592 {
593 return;
594 }
595 }
596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev6de924c2012-01-20 13:18:18 +0200598 if (is_null($postdata) && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 {
600 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
601 {
602 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300603 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200604
Andrey Andreev56454792012-05-17 14:32:19 +0300605 if (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
607 $line = $this->_error_messages[$type];
608 }
Andrey Andreev56454792012-05-17 14:32:19 +0300609 elseif (FALSE === ($line = $this->CI->lang->line($type)))
610 {
611 $line = 'The field was not set';
612 }
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 // Build the error message
615 $message = sprintf($line, $this->_translate_fieldname($row['label']));
616
617 // Save the error message
618 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 if ( ! isset($this->_error_array[$row['field']]))
621 {
622 $this->_error_array[$row['field']] = $message;
623 }
624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 return;
627 }
628
629 // --------------------------------------------------------------------
630
631 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300632 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
634 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 // We set the $postdata variable with the current data in our master array so that
637 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100638 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
640 // We shouldn't need this safety, but just in case there isn't an array index
641 // associated with this cycle we'll bail out
642 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
643 {
644 continue;
645 }
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
648 $_in_array = TRUE;
649 }
650 else
651 {
652 $postdata = $this->_field_data[$row['field']]['postdata'];
653 }
654
Barry Mienydd671972010-10-04 16:33:58 +0200655 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200657 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
659 $rule = substr($rule, 9);
660 $callback = TRUE;
661 }
Barry Mienydd671972010-10-04 16:33:58 +0200662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 // Strip the parameter (if exists) from the rule
664 // Rules can contain a parameter: max_length[5]
665 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200666 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 {
668 $rule = $match[1];
669 $param = $match[2];
670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 // Call the function that corresponds to the rule
673 if ($callback === TRUE)
674 {
675 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200676 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200677 log_message('debug', 'Unable to find callback validation rule: '.$rule);
678 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200680 else
681 {
682 // Run the function and grab the result
683 $result = $this->CI->$rule($postdata, $param);
684 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000685
686 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200687 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300689 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
691 else
692 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300693 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200697 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000699 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 }
701 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300702 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200703 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300704 // If our own wrapper function doesn't exist we see if a native PHP function does.
705 // Users can use any native PHP function call that has one param.
706 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 {
Andrey Andreev320d37c2012-04-03 20:21:39 +0300708 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200709
Andrey Andreev78f55772012-04-03 19:59:08 +0300710 if ($_in_array === TRUE)
711 {
712 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 }
patwork02404a12011-04-08 15:45:46 +0200714 else
715 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300716 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200717 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200719 else
720 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300721 log_message('debug', 'Unable to find validation rule: '.$rule);
722 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200723 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300724 }
725 else
726 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 $result = $this->$rule($postdata, $param);
728
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200729 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300731 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 }
733 else
734 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300735 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
737 }
Barry Mienydd671972010-10-04 16:33:58 +0200738
Andrey Andreev901573c2012-01-11 01:40:48 +0200739 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200741 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 if ( ! isset($this->_error_messages[$rule]))
743 {
744 if (FALSE === ($line = $this->CI->lang->line($rule)))
745 {
746 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200747 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 }
749 else
750 {
751 $line = $this->_error_messages[$rule];
752 }
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200755 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200756 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 {
Pascal Krietec1895832009-10-13 12:56:43 +0000758 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 }
Barry Mienydd671972010-10-04 16:33:58 +0200760
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 // Build the error message
762 $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
763
764 // Save the error message
765 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200766
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 if ( ! isset($this->_error_array[$row['field']]))
768 {
769 $this->_error_array[$row['field']] = $message;
770 }
Barry Mienydd671972010-10-04 16:33:58 +0200771
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 return;
773 }
774 }
775 }
776
777 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 /**
780 * Translate a field name
781 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 * @param string the field name
783 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200784 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100785 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 {
787 // Do we need to translate the field name?
788 // We look for the prefix lang: to determine this
Andrey Andreev901573c2012-01-11 01:40:48 +0200789 if (strpos($fieldname, 'lang:') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 {
791 // Grab the variable
Barry Mienydd671972010-10-04 16:33:58 +0200792 $line = substr($fieldname, 5);
793
Derek Jones37f4b9c2011-07-01 17:56:50 -0500794 // Were we able to translate the field name? If not we use $line
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 if (FALSE === ($fieldname = $this->CI->lang->line($line)))
796 {
797 return $line;
798 }
799 }
800
801 return $fieldname;
802 }
803
804 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 /**
807 * Get the value from a form
808 *
809 * Permits you to repopulate a form field with the value it was submitted
810 * with, or, if that value doesn't exist, with the default
811 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 * @param string the field name
813 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200814 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200815 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100816 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200818 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 {
820 return $default;
821 }
Barry Mienydd671972010-10-04 16:33:58 +0200822
Phil Sturgeon5c561802011-01-05 16:31:59 +0000823 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600824 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000825 if (is_array($this->_field_data[$field]['postdata']))
826 {
827 return array_shift($this->_field_data[$field]['postdata']);
828 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000829
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 return $this->_field_data[$field]['postdata'];
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 /**
836 * Set Select
837 *
838 * Enables pull-down lists to be set to the value the user
839 * selected in the event of an error
840 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 * @param string
842 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400843 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200845 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100846 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200847 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200848 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200850 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 }
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 if (is_array($field))
855 {
856 if ( ! in_array($value, $field))
857 {
858 return '';
859 }
860 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100861 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200863 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 }
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 return ' selected="selected"';
867 }
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 /**
872 * Set Radio
873 *
874 * Enables radio buttons to be set to the value the user
875 * selected in the event of an error
876 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 * @param string
878 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400879 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200881 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100882 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200884 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200886 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 if (is_array($field))
891 {
892 if ( ! in_array($value, $field))
893 {
894 return '';
895 }
896 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100897 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200899 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 }
Barry Mienydd671972010-10-04 16:33:58 +0200901
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 return ' checked="checked"';
903 }
Barry Mienydd671972010-10-04 16:33:58 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200906
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 /**
908 * Set Checkbox
909 *
910 * Enables checkboxes to be set to the value the user
911 * selected in the event of an error
912 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 * @param string
914 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400915 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200917 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100918 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200920 // Logic is exactly the same as for radio fields
921 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 /**
927 * Required
928 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 * @param string
930 * @return bool
931 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100932 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300934 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500940 * Performs a Regular Expression match test.
941 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500942 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300943 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500944 * @return bool
945 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100946 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500947 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200948 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500949 }
950
951 // --------------------------------------------------------------------
952
953 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 * Match one field to another
955 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300957 * @param string field
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 * @return bool
959 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100960 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300962 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
Barry Mienydd671972010-10-04 16:33:58 +0200963
Andrey Andreev56454792012-05-17 14:32:19 +0300964 return isset($validation_array[$field]) ? ($str === $validation_array[$field]) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Eric Barnescccde962011-12-04 00:01:17 -0500966
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100967 // --------------------------------------------------------------------
968
969 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +0200970 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100971 *
Andrey Andreevd09d6502012-01-03 06:38:33 +0200972 * Check if the input value doesn't already exist
973 * in the specified database field.
974 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100975 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300976 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100977 * @return bool
978 */
979 public function is_unique($str, $field)
980 {
Eric Barnescccde962011-12-04 00:01:17 -0500981 list($table, $field) = explode('.', $field);
982 if (isset($this->CI->db))
983 {
984 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
985 return $query->num_rows() === 0;
986 }
987 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -0600988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 /**
993 * Minimum Length
994 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +0200996 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200998 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100999 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001001 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 {
1003 return FALSE;
1004 }
1005
Andrey Andreev78f55772012-04-03 19:59:08 +03001006 return (MB_ENABLED === TRUE)
1007 ? ($val <= mb_strlen($str))
George Petsagourakis2d51c082012-05-02 20:29:04 +03001008 : ($val <= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 /**
1014 * Max Length
1015 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001017 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001019 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001020 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001022 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
1024 return FALSE;
1025 }
1026
Andrey Andreev78f55772012-04-03 19:59:08 +03001027 return (MB_ENABLED === TRUE)
1028 ? ($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 * Exact Length
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +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 exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001043 if (preg_match('/[^0-9]/', $val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 {
1045 return FALSE;
1046 }
1047
Andrey Andreev78f55772012-04-03 19:59:08 +03001048 return (MB_ENABLED === TRUE)
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001049 ? (mb_strlen($str) === intval($val))
1050 : (strlen($str) === intval($val));
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 }
Barry Mienydd671972010-10-04 16:33:58 +02001052
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 /**
1056 * Valid Email
1057 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 * @param string
1059 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001060 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001061 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001063 return (bool) preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 }
1065
1066 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001067
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 /**
1069 * Valid Emails
1070 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 * @param string
1072 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001073 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001074 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 {
1076 if (strpos($str, ',') === FALSE)
1077 {
1078 return $this->valid_email(trim($str));
1079 }
Barry Mienydd671972010-10-04 16:33:58 +02001080
Pascal Kriete14287f32011-02-14 13:39:34 -05001081 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001083 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 {
1085 return FALSE;
1086 }
1087 }
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 return TRUE;
1090 }
1091
1092 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001093
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 /**
1095 * Validate IP Address
1096 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001098 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001099 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001101 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001103 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 }
1105
1106 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 /**
1109 * Alpha
1110 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 * @param string
1112 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001113 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001114 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001116 return (bool) preg_match('/^[a-z]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 }
Barry Mienydd671972010-10-04 16:33:58 +02001118
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001120
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 /**
1122 * Alpha-numeric
1123 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 * @param string
1125 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001126 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001127 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001129 return (bool) preg_match('/^[a-z0-9]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 }
Barry Mienydd671972010-10-04 16:33:58 +02001131
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001133
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 /**
1135 * Alpha-numeric with underscores and dashes
1136 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 * @param string
1138 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001139 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001140 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001142 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 }
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 /**
1148 * Numeric
1149 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 * @param string
1151 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001152 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001153 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001155 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001156
1157 }
1158
1159 // --------------------------------------------------------------------
1160
Barry Mienydd671972010-10-04 16:33:58 +02001161 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 * Integer
1163 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 * @param string
1165 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001166 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001167 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001169 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1170 }
1171
1172 // --------------------------------------------------------------------
1173
1174 /**
1175 * Decimal number
1176 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001177 * @param string
1178 * @return bool
1179 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001180 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001181 {
1182 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1183 }
1184
1185 // --------------------------------------------------------------------
1186
1187 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001188 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001189 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001190 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001191 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001192 * @return bool
1193 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001194 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001195 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001196 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001197 }
1198
1199 // --------------------------------------------------------------------
1200
1201 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001202 * Equal to or Greater than
1203 *
Nick Busey98c347d2012-02-02 11:07:03 -07001204 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001205 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001206 * @return bool
1207 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001208 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001209 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001210 return is_numeric($str) ? ($str >= $min) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001211 }
1212
1213 // --------------------------------------------------------------------
Phil Sturgeonef112c02011-02-07 13:01:47 +00001214
1215 /**
1216 * Less than
1217 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001218 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001219 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001220 * @return bool
1221 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001222 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001223 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001224 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001226
1227 // --------------------------------------------------------------------
1228
Barry Mienydd671972010-10-04 16:33:58 +02001229 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001230 * Equal to or Less than
1231 *
Nick Busey98c347d2012-02-02 11:07:03 -07001232 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001233 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001234 * @return bool
1235 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001236 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001237 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001238 return is_numeric($str) ? ($str <= $max) : FALSE;
Nick Busey98c347d2012-02-02 11:07:03 -07001239 }
1240
1241 // --------------------------------------------------------------------
1242
1243 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001244 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001245 *
Barry Mienydd671972010-10-04 16:33:58 +02001246 * @param string
1247 * @return bool
1248 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001249 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001250 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001251 return (bool) preg_match('/^[0-9]+$/', $str);
Barry Mienydd671972010-10-04 16:33:58 +02001252 }
1253
1254 // --------------------------------------------------------------------
1255
1256 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001257 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001258 *
Barry Mienydd671972010-10-04 16:33:58 +02001259 * @param string
1260 * @return bool
1261 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001262 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001263 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001264 return ($str !== 0 && preg_match('/^[0-9]+$/', $str));
Barry Mienydd671972010-10-04 16:33:58 +02001265 }
1266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 /**
1270 * Valid Base64
1271 *
1272 * Tests a string for characters outside of the Base64 alphabet
1273 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1274 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 * @param string
1276 * @return bool
1277 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001278 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
Andrey Andreev56454792012-05-17 14:32:19 +03001280 return ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 }
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001284
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 /**
1286 * Prep data for form
1287 *
1288 * This function allows HTML to be safely shown in a form.
1289 * Special characters are converted.
1290 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 * @param string
1292 * @return string
1293 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001294 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 {
1296 if (is_array($data))
1297 {
1298 foreach ($data as $key => $val)
1299 {
1300 $data[$key] = $this->prep_for_form($val);
1301 }
Barry Mienydd671972010-10-04 16:33:58 +02001302
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 return $data;
1304 }
Barry Mienydd671972010-10-04 16:33:58 +02001305
Alex Bilbied261b1e2012-06-02 11:12:16 +01001306 if ($this->_safe_form_data === FALSE OR $data === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 {
1308 return $data;
1309 }
1310
Andrey Andreev901573c2012-01-11 01:40:48 +02001311 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 }
Barry Mienydd671972010-10-04 16:33:58 +02001313
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001315
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 /**
1317 * Prep URL
1318 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 * @param string
1320 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001321 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001322 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001324 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 {
1326 return '';
1327 }
Barry Mienydd671972010-10-04 16:33:58 +02001328
Andrey Andreev901573c2012-01-11 01:40:48 +02001329 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001331 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 }
Barry Mienydd671972010-10-04 16:33:58 +02001333
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 return $str;
1335 }
Barry Mienydd671972010-10-04 16:33:58 +02001336
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001338
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 /**
1340 * Strip Image Tags
1341 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 * @param string
1343 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001344 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001345 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 {
1347 return $this->CI->input->strip_image_tags($str);
1348 }
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 * XSS Clean
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 xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 {
Derek Jones5640a712010-04-23 11:22:40 -05001360 return $this->CI->security->xss_clean($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 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001364
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 /**
1366 * Convert PHP tags to entities
1367 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 * @param string
1369 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001370 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001371 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001373 return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 }
1375
JonoB099c4782012-03-04 14:37:30 +00001376 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001377
1378 /**
1379 * Reset validation vars
1380 *
1381 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001382 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001383 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001384 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001385 */
JonoB883f80f2012-03-05 09:51:27 +00001386 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001387 {
JonoB099c4782012-03-04 14:37:30 +00001388 $this->_field_data = array();
1389 $this->_config_rules = array();
1390 $this->_error_array = array();
1391 $this->_error_messages = array();
1392 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001393 }
1394
Derek Allard2067d1a2008-11-13 22:59:24 +00001395}
Derek Allard2067d1a2008-11-13 22:59:24 +00001396
1397/* End of file Form_validation.php */
Andrey Andreev31cf46e2012-03-20 15:48:00 +02001398/* Location: ./system/libraries/Form_validation.php */