blob: 852fc71442fd68be07c29e163f815f64ed74782f [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
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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;
Derek Allard2067d1a2008-11-13 22:59:24 +000046
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 Andreevcff35802012-11-26 15:49:52 +0200108 public $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
Andrew Podner4296a652012-12-17 07:51:15 -0500152 * @return CI_Form_validation
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 Andreev7a7ad782012-11-12 17:21:01 +0200202 $indexes = array();
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200203 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200204 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200205 sscanf($field, '%[^[][', $indexes[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000206
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200207 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100209 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200211 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 $is_array = TRUE;
216 }
217 else
218 {
Barry Mienydd671972010-10-04 16:33:58 +0200219 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
222 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300224 'field' => $field,
225 'label' => $label,
226 'rules' => $rules,
227 'is_array' => $is_array,
228 'keys' => $indexes,
229 'postdata' => NULL,
230 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000231 );
Greg Aker9f9af602010-11-10 15:41:51 -0600232
233 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235
236 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 /**
JonoB099c4782012-03-04 14:37:30 +0000239 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200240 *
JonoB099c4782012-03-04 14:37:30 +0000241 * If an array is set through this method, then this array will
242 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200243 *
244 * Note that if you are validating multiple arrays, then the
245 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000246 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200247 *
248 * @param array $data
249 * @return void
JonoB099c4782012-03-04 14:37:30 +0000250 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200251 public function set_data(array $data)
JonoB099c4782012-03-04 14:37:30 +0000252 {
Andrey Andreeva4712f52014-01-06 11:25:46 +0200253 if ( ! empty($data))
JonoB099c4782012-03-04 14:37:30 +0000254 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200255 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000256 }
257 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200258
JonoB099c4782012-03-04 14:37:30 +0000259 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000260
261 /**
262 * Set Error Message
263 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300264 * Lets users set their own error messages on the fly. Note:
265 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300267 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500269 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100271 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 if ( ! is_array($lang))
274 {
275 $lang = array($lang => $val);
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600279 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 /**
285 * Set The Error Delimiter
286 *
287 * Permits a prefix/suffix to be added to each error message
288 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 * @param string
290 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500291 * @return CI_Form_validation
Barry Mienydd671972010-10-04 16:33:58 +0200292 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100293 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
295 $this->_error_prefix = $prefix;
296 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600297 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
299
300 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 /**
303 * Get Error Message
304 *
305 * Gets the error message associated with a particular field
306 *
Andrey Andreeva4712f52014-01-06 11:25:46 +0200307 * @param string $field Field name
308 * @param string $prefix HTML start tag
Andrey Andreev868301a2014-01-06 12:29:50 +0200309 * @param string $suffix HTML end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300310 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200311 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200312 public function error($field, $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200313 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300314 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
316 return '';
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Alex Bilbied261b1e2012-06-02 11:12:16 +0100319 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 $prefix = $this->_error_prefix;
322 }
323
Alex Bilbied261b1e2012-06-02 11:12:16 +0100324 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
326 $suffix = $this->_error_suffix;
327 }
328
329 return $prefix.$this->_field_data[$field]['error'].$suffix;
330 }
331
332 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100335 * Get Array of Error Messages
336 *
337 * Returns the error messages as an array
338 *
339 * @return array
340 */
341 public function error_array()
342 {
343 return $this->_error_array;
344 }
345
346 // --------------------------------------------------------------------
347
348 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * Error String
350 *
351 * Returns the error messages as a string, wrapped in the error delimiters
352 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 * @param string
354 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300355 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200356 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100357 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
vlakoff35672462013-02-15 01:36:04 +0100359 // No errors, validation passes!
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 if (count($this->_error_array) === 0)
361 {
362 return '';
363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Alex Bilbied261b1e2012-06-02 11:12:16 +0100365 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
367 $prefix = $this->_error_prefix;
368 }
369
Alex Bilbied261b1e2012-06-02 11:12:16 +0100370 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
372 $suffix = $this->_error_suffix;
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 // Generate the error string
376 $str = '';
377 foreach ($this->_error_array as $val)
378 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100379 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 $str .= $prefix.$val.$suffix."\n";
382 }
383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 return $str;
386 }
387
388 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 /**
391 * Run the Validator
392 *
393 * This function does all the work.
394 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400395 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200397 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100398 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500400 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300401 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000402 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 return FALSE;
405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 // Does the _field_data array containing the validation rules exist?
408 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200409 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500411 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200412 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 return FALSE;
415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200417 if (empty($group))
418 {
419 // Is there a validation rule for the particular URI being accessed?
420 $group = trim($this->CI->uri->ruri_string(), '/');
421 isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method;
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200424 $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules);
Barry Mienydd671972010-10-04 16:33:58 +0200425
Andrey Andreev901573c2012-01-11 01:40:48 +0200426 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200427 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200429 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 return FALSE;
431 }
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // Load the language file containing error messages
435 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200436
Andrey Andreev751f2472012-11-03 18:26:27 +0200437 // Cycle through the rules for each field and match the corresponding $validation_data item
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200439 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200440 // Fetch the data from the validation_data array item and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 // 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 +0200442 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
JonoB099c4782012-03-04 14:37:30 +0000444 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
Andrey Andreevfff6c2a2012-05-13 22:15:23 +0300446 elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300448 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Andrey Andreev751f2472012-11-03 18:26:27 +0200452 // Execute validation rules
453 // Note: A second foreach (for now) is required in order to avoid false-positives
454 // for rules like 'matches', which correlate to other validation fields.
455 foreach ($this->_field_data as $field => $row)
456 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300457 // Don't try to validate if we have no rules set
458 if (empty($row['rules']))
459 {
460 continue;
461 }
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
Andrey Andreev2d48b4f2012-11-23 17:33:21 +0200496 // NULL must be returned for empty fields
Andrey Andreev44c34632012-11-23 18:46:34 +0200497 return ($array === '') ? NULL : $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
499
500 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 /**
503 * Re-populate the _POST array with our finalized and processed data
504 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200505 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200506 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100507 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
509 foreach ($this->_field_data as $field => $row)
510 {
vlakoff1228fe22013-01-14 01:30:09 +0100511 if ($row['postdata'] !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200513 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
515 if (isset($_POST[$row['field']]))
516 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200517 $_POST[$row['field']] = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
519 }
520 else
521 {
Derek Jones63eeae32009-02-10 19:08:56 +0000522 // start with a reference
523 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Jones63eeae32009-02-10 19:08:56 +0000525 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200526 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
Derek Jones63eeae32009-02-10 19:08:56 +0000528 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 }
530 else
531 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 foreach ($row['keys'] as $val)
533 {
Derek Jones63eeae32009-02-10 19:08:56 +0000534 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536 }
Derek Jones63eeae32009-02-10 19:08:56 +0000537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000539 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 $array = array();
541 foreach ($row['postdata'] as $k => $v)
542 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200543 $array[$k] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
Derek Jones63eeae32009-02-10 19:08:56 +0000545
546 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
548 else
Derek Jones63eeae32009-02-10 19:08:56 +0000549 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200550 $post_ref = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
553 }
554 }
555 }
556
557 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 /**
560 * Executes the Validation routines
561 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 * @param array
563 * @param array
564 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300565 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200567 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100568 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
570 // If the $_POST data is an array we will run a recursive call
571 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200572 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 foreach ($postdata as $key => $val)
574 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300575 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 }
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;
Hashem Qolami05370bf2013-07-22 01:52:04 +0430583 if ( ! in_array('required', $rules) && ($postdata === NULL OR $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.
Hashem Qolami05370bf2013-07-22 01:52:04 +0430598 if (($postdata === NULL OR $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 Andreevd4eec9f2012-12-14 11:07:13 +0200609 elseif (FALSE === ($line = $this->CI->lang->line('form_validation_'.$type))
610 // DEPRECATED support for non-prefixed keys
611 && FALSE === ($line = $this->CI->lang->line($type, FALSE)))
Andrey Andreev56454792012-05-17 14:32:19 +0300612 {
613 $line = 'The field was not set';
614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600617 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
Derek Allard2067d1a2008-11-13 22:59:24 +0000618
619 // Save the error message
620 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 if ( ! isset($this->_error_array[$row['field']]))
623 {
624 $this->_error_array[$row['field']] = $message;
625 }
626 }
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 return;
629 }
630
631 // --------------------------------------------------------------------
632
633 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300634 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 // We set the $postdata variable with the current data in our master array so that
639 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100640 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 // We shouldn't need this safety, but just in case there isn't an array index
643 // associated with this cycle we'll bail out
644 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
645 {
646 continue;
647 }
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
650 $_in_array = TRUE;
651 }
652 else
653 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300654 // If we get an array field, but it's not expected - then it is most likely
655 // somebody messing with the form on the client side, so we'll just consider
656 // it an empty field
657 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
658 ? NULL
659 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
661
Barry Mienydd671972010-10-04 16:33:58 +0200662 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200664 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 {
666 $rule = substr($rule, 9);
667 $callback = TRUE;
668 }
Barry Mienydd671972010-10-04 16:33:58 +0200669
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 // Strip the parameter (if exists) from the rule
671 // Rules can contain a parameter: max_length[5]
672 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200673 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
Andrey Andreevef758bd2012-11-15 12:24:52 +0200675 $rule = $match[1];
676 $param = $match[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 }
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 // Call the function that corresponds to the rule
680 if ($callback === TRUE)
681 {
682 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200683 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200684 log_message('debug', 'Unable to find callback validation rule: '.$rule);
685 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200687 else
688 {
689 // Run the function and grab the result
690 $result = $this->CI->$rule($postdata, $param);
691 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000692
693 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200694 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300696 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 }
698 else
699 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300700 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 }
Barry Mienydd671972010-10-04 16:33:58 +0200702
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200704 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000706 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 }
708 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300709 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200710 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300711 // If our own wrapper function doesn't exist we see if a native PHP function does.
712 // Users can use any native PHP function call that has one param.
713 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 {
Andrey Andreev320d37c2012-04-03 20:21:39 +0300715 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200716
Andrey Andreev78f55772012-04-03 19:59:08 +0300717 if ($_in_array === TRUE)
718 {
719 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 }
patwork02404a12011-04-08 15:45:46 +0200721 else
722 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300723 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200724 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200726 else
727 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300728 log_message('debug', 'Unable to find validation rule: '.$rule);
729 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200730 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300731 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 else
733 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 $result = $this->$rule($postdata, $param);
735
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200736 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300738 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 }
740 else
741 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300742 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 }
744 }
Barry Mienydd671972010-10-04 16:33:58 +0200745
Andrey Andreev901573c2012-01-11 01:40:48 +0200746 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200748 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 if ( ! isset($this->_error_messages[$rule]))
750 {
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200751 if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule))
752 // DEPRECATED support for non-prefixed keys
753 && FALSE === ($line = $this->CI->lang->line($rule, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
755 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200756 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 }
758 else
759 {
760 $line = $this->_error_messages[$rule];
761 }
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200764 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200765 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 {
Pascal Krietec1895832009-10-13 12:56:43 +0000767 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 }
Barry Mienydd671972010-10-04 16:33:58 +0200769
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600771 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000772
773 // Save the error message
774 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 if ( ! isset($this->_error_array[$row['field']]))
777 {
778 $this->_error_array[$row['field']] = $message;
779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 return;
782 }
783 }
784 }
785
786 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200787
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 /**
789 * Translate a field name
790 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 * @param string the field name
792 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200793 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100794 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 {
796 // Do we need to translate the field name?
797 // We look for the prefix lang: to determine this
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200798 if (sscanf($fieldname, 'lang:%s', $line) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500800 // Were we able to translate the field name? If not we use $line
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200801 if (FALSE === ($fieldname = $this->CI->lang->line('form_validation_'.$line))
802 // DEPRECATED support for non-prefixed keys
803 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 {
805 return $line;
806 }
807 }
808
809 return $fieldname;
810 }
811
812 // --------------------------------------------------------------------
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200813
Eric Roberts41cc0902012-01-24 00:59:44 -0600814 /**
815 * Build an error message using the field and param.
816 *
817 * @param string The error message line
818 * @param string A field's human name
819 * @param mixed A rule's optional parameter
820 * @return string
821 */
822 protected function _build_error_msg($line, $field = '', $param = '')
823 {
824 // Check for %s in the string for legacy support.
Eric Roberts24a13f52012-12-12 07:09:42 -0600825 if (strpos($line, '%s') !== FALSE)
Eric Roberts41cc0902012-01-24 00:59:44 -0600826 {
827 return sprintf($line, $field, $param);
828 }
Andrew Podner4296a652012-12-17 07:51:15 -0500829
Eric Roberts41cc0902012-01-24 00:59:44 -0600830 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
831 }
832
833 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 /**
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530836 * Checks if the rule is present within the validator
837 *
838 * Permits you to check if a rule is present within the validator
839 *
840 * @param string the field name
841 * @return bool
842 */
843 public function has_rule($field)
844 {
845 return isset($this->_field_data[$field]);
846 }
847
848 // --------------------------------------------------------------------
849
850 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 * Get the value from a form
852 *
853 * Permits you to repopulate a form field with the value it was submitted
854 * with, or, if that value doesn't exist, with the default
855 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 * @param string the field name
857 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200858 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200859 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100860 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200862 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 {
864 return $default;
865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Phil Sturgeon5c561802011-01-05 16:31:59 +0000867 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600868 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000869 if (is_array($this->_field_data[$field]['postdata']))
870 {
871 return array_shift($this->_field_data[$field]['postdata']);
872 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 return $this->_field_data[$field]['postdata'];
875 }
Barry Mienydd671972010-10-04 16:33:58 +0200876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 /**
880 * Set Select
881 *
882 * Enables pull-down lists to be set to the value the user
883 * selected in the event of an error
884 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 * @param string
886 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400887 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200889 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100890 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200891 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200892 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200894 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 }
Barry Mienydd671972010-10-04 16:33:58 +0200896
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300898 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 if (is_array($field))
900 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300901 // Note: in_array('', array(0)) returns TRUE, do not use it
902 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300904 if ($value === $v)
905 {
906 return ' selected="selected"';
907 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300909
910 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100912 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200914 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 }
Barry Mienydd671972010-10-04 16:33:58 +0200916
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 return ' selected="selected"';
918 }
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 /**
923 * Set Radio
924 *
925 * Enables radio buttons to be set to the value the user
926 * selected in the event of an error
927 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 * @param string
929 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400930 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200932 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100933 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200935 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200937 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 }
Barry Mienydd671972010-10-04 16:33:58 +0200939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300941 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 if (is_array($field))
943 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300944 // Note: in_array('', array(0)) returns TRUE, do not use it
945 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300947 if ($value === $v)
948 {
949 return ' checked="checked"';
950 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300952
953 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100955 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200957 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 return ' checked="checked"';
961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 /**
966 * Set Checkbox
967 *
968 * Enables checkboxes to be set to the value the user
969 * selected in the event of an error
970 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 * @param string
972 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400973 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200975 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100976 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200978 // Logic is exactly the same as for radio fields
979 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
Barry Mienydd671972010-10-04 16:33:58 +0200981
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 /**
985 * Required
986 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 * @param string
988 * @return bool
989 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100990 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300992 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 }
Barry Mienydd671972010-10-04 16:33:58 +0200994
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500998 * Performs a Regular Expression match test.
999 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001000 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001001 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001002 * @return bool
1003 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001004 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001005 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001006 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001007 }
1008
1009 // --------------------------------------------------------------------
1010
1011 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 * Match one field to another
1013 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001014 * @param string $str string to compare against
1015 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 * @return bool
1017 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001018 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001020 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
1021 ? ($str === $this->_field_data[$field]['postdata'])
1022 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001024
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001025 // --------------------------------------------------------------------
1026
1027 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -03001028 * Differs from another field
1029 *
1030 * @param string
1031 * @param string field
1032 * @return bool
1033 */
1034 public function differs($str, $field)
1035 {
1036 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001038
1039 // --------------------------------------------------------------------
1040
1041 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +02001042 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001043 *
Andrey Andreevd09d6502012-01-03 06:38:33 +02001044 * Check if the input value doesn't already exist
1045 * in the specified database field.
1046 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001047 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001048 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001049 * @return bool
1050 */
1051 public function is_unique($str, $field)
1052 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001053 sscanf($field, '%[^.].%[^.]', $table, $field);
Eric Barnescccde962011-12-04 00:01:17 -05001054 if (isset($this->CI->db))
1055 {
1056 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
1057 return $query->num_rows() === 0;
1058 }
1059 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001060 }
Barry Mienydd671972010-10-04 16:33:58 +02001061
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001063
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 /**
1065 * Minimum Length
1066 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001068 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001070 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001071 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001073 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 {
1075 return FALSE;
1076 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001077 else
1078 {
1079 $val = (int) $val;
1080 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001081
Andrey Andreev78f55772012-04-03 19:59:08 +03001082 return (MB_ENABLED === TRUE)
1083 ? ($val <= mb_strlen($str))
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001084 : ($val <= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 }
Barry Mienydd671972010-10-04 16:33:58 +02001086
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 /**
1090 * Max Length
1091 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001093 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001095 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001096 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001098 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
1100 return FALSE;
1101 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001102 else
1103 {
1104 $val = (int) $val;
1105 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001106
Andrey Andreev78f55772012-04-03 19:59:08 +03001107 return (MB_ENABLED === TRUE)
1108 ? ($val >= mb_strlen($str))
1109 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 }
Barry Mienydd671972010-10-04 16:33:58 +02001111
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001113
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 /**
1115 * Exact Length
1116 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001118 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001120 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001121 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001123 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
1125 return FALSE;
1126 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001127 else
1128 {
1129 $val = (int) $val;
1130 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001131
Andrey Andreev78f55772012-04-03 19:59:08 +03001132 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001133 ? (mb_strlen($str) === $val)
1134 : (strlen($str) === $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 }
Barry Mienydd671972010-10-04 16:33:58 +02001136
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001138
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 /**
Andrey Andreevdaaca882012-11-26 22:16:12 +02001140 * Valid URL
1141 *
1142 * @param string $str
1143 * @return bool
1144 */
1145 public function valid_url($str)
1146 {
1147 if (empty($str))
1148 {
1149 return FALSE;
1150 }
1151 elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
1152 {
1153 if (empty($matches[2]))
1154 {
1155 return FALSE;
1156 }
1157 elseif ( ! in_array($matches[1], array('http', 'https'), TRUE))
1158 {
1159 return FALSE;
1160 }
1161
1162 $str = $matches[2];
1163 }
1164
1165 $str = 'http://'.$str;
1166
1167 // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the
1168 // underscore to be a valid hostname character instead of a dash.
1169 // Reference: https://bugs.php.net/bug.php?id=51192
1170 if (version_compare(PHP_VERSION, '5.2.13', '==') === 0 OR version_compare(PHP_VERSION, '5.3.2', '==') === 0)
1171 {
1172 sscanf($str, 'http://%[^/]', $host);
1173 $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host));
1174 }
1175
1176 return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 }
1178
1179 // --------------------------------------------------------------------
1180
1181 /**
1182 * Valid Email
1183 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 * @param string
1185 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001186 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001187 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001189 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 }
1191
1192 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001193
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 /**
1195 * Valid Emails
1196 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 * @param string
1198 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001199 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001200 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 {
1202 if (strpos($str, ',') === FALSE)
1203 {
1204 return $this->valid_email(trim($str));
1205 }
Barry Mienydd671972010-10-04 16:33:58 +02001206
Pascal Kriete14287f32011-02-14 13:39:34 -05001207 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001209 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 {
1211 return FALSE;
1212 }
1213 }
Barry Mienydd671972010-10-04 16:33:58 +02001214
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 return TRUE;
1216 }
1217
1218 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001219
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 /**
1221 * Validate IP Address
1222 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001224 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001225 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001227 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001229 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 }
1231
1232 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001233
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 /**
1235 * Alpha
1236 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 * @param string
1238 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001239 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001240 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001242 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 }
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001246
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 /**
1248 * Alpha-numeric
1249 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 * @param string
1251 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001252 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001253 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001255 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 }
Barry Mienydd671972010-10-04 16:33:58 +02001257
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 // --------------------------------------------------------------------
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001259
1260 /**
1261 * Alpha-numeric w/ spaces
1262 *
1263 * @param string
1264 * @return bool
1265 */
1266 public function alpha_numeric_spaces($str)
1267 {
Sajan Parikhdf3bfed2013-02-04 12:25:49 -06001268 return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001269 }
1270
1271 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001272
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 /**
1274 * Alpha-numeric with underscores and dashes
1275 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 * @param string
1277 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001278 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001279 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001281 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 }
Barry Mienydd671972010-10-04 16:33:58 +02001283
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 /**
1287 * Numeric
1288 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 * @param string
1290 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001291 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001292 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001294 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001295
1296 }
1297
1298 // --------------------------------------------------------------------
1299
Barry Mienydd671972010-10-04 16:33:58 +02001300 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 * Integer
1302 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 * @param string
1304 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001305 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001306 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001308 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1309 }
1310
1311 // --------------------------------------------------------------------
1312
1313 /**
1314 * Decimal number
1315 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001316 * @param string
1317 * @return bool
1318 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001319 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001320 {
1321 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1322 }
1323
1324 // --------------------------------------------------------------------
1325
1326 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001327 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001328 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001329 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001330 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001331 * @return bool
1332 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001333 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001334 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001335 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001336 }
1337
1338 // --------------------------------------------------------------------
1339
1340 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001341 * Equal to or Greater than
1342 *
Nick Busey98c347d2012-02-02 11:07:03 -07001343 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001344 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001345 * @return bool
1346 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001347 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001348 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001349 return is_numeric($str) ? ($str >= $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001350 }
1351
1352 // --------------------------------------------------------------------
1353
1354 /**
1355 * Less than
1356 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001357 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001358 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001359 * @return bool
1360 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001361 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001362 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001363 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001365
1366 // --------------------------------------------------------------------
1367
Barry Mienydd671972010-10-04 16:33:58 +02001368 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001369 * Equal to or Less than
1370 *
Nick Busey98c347d2012-02-02 11:07:03 -07001371 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001372 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001373 * @return bool
1374 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001375 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001376 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001377 return is_numeric($str) ? ($str <= $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 }
1379
1380 // --------------------------------------------------------------------
1381
Barry Mienydd671972010-10-04 16:33:58 +02001382 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001383 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001384 *
Barry Mienydd671972010-10-04 16:33:58 +02001385 * @param string
1386 * @return bool
1387 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001388 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001389 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001390 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001391 }
1392
1393 // --------------------------------------------------------------------
1394
1395 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001396 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001397 *
Barry Mienydd671972010-10-04 16:33:58 +02001398 * @param string
1399 * @return bool
1400 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001401 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001402 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001403 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001404 }
1405
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001407
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 /**
1409 * Valid Base64
1410 *
1411 * Tests a string for characters outside of the Base64 alphabet
1412 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1413 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 * @param string
1415 * @return bool
1416 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001417 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001418 {
Andrey Andreevcd9797a2013-06-28 14:03:48 +03001419 return (base64_encode(base64_decode($str)) === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 }
Barry Mienydd671972010-10-04 16:33:58 +02001421
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001423
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 /**
1425 * Prep data for form
1426 *
1427 * This function allows HTML to be safely shown in a form.
1428 * Special characters are converted.
1429 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 * @param string
1431 * @return string
1432 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001433 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001434 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001435 if ($this->_safe_form_data === FALSE OR empty($data))
1436 {
1437 return $data;
1438 }
1439
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 if (is_array($data))
1441 {
1442 foreach ($data as $key => $val)
1443 {
1444 $data[$key] = $this->prep_for_form($val);
1445 }
Barry Mienydd671972010-10-04 16:33:58 +02001446
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 return $data;
1448 }
Barry Mienydd671972010-10-04 16:33:58 +02001449
Andrey Andreev901573c2012-01-11 01:40:48 +02001450 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001451 }
Barry Mienydd671972010-10-04 16:33:58 +02001452
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001454
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 /**
1456 * Prep URL
1457 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 * @param string
1459 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001460 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001461 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001462 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001463 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 {
1465 return '';
1466 }
Barry Mienydd671972010-10-04 16:33:58 +02001467
Andrey Andreev901573c2012-01-11 01:40:48 +02001468 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001470 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 }
Barry Mienydd671972010-10-04 16:33:58 +02001472
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 return $str;
1474 }
Barry Mienydd671972010-10-04 16:33:58 +02001475
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001477
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 /**
1479 * Strip Image Tags
1480 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001481 * @param string
1482 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001483 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001484 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001485 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001486 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001487 }
Barry Mienydd671972010-10-04 16:33:58 +02001488
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001490
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 /**
1492 * XSS Clean
1493 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 * @param string
1495 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001496 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001497 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001498 {
Derek Jones5640a712010-04-23 11:22:40 -05001499 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001500 }
Barry Mienydd671972010-10-04 16:33:58 +02001501
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001503
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 /**
1505 * Convert PHP tags to entities
1506 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001507 * @param string
1508 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001509 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001510 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001511 {
Andrey Andreev838a9d62012-12-03 14:37:47 +02001512 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 }
1514
JonoB099c4782012-03-04 14:37:30 +00001515 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001516
1517 /**
1518 * Reset validation vars
1519 *
1520 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001521 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001522 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001523 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001524 */
JonoB883f80f2012-03-05 09:51:27 +00001525 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001526 {
JonoB099c4782012-03-04 14:37:30 +00001527 $this->_field_data = array();
1528 $this->_config_rules = array();
1529 $this->_error_array = array();
1530 $this->_error_messages = array();
1531 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001532 }
1533
Derek Allard2067d1a2008-11-13 22:59:24 +00001534}
Derek Allard2067d1a2008-11-13 22:59:24 +00001535
1536/* End of file Form_validation.php */
Eric Roberts24a13f52012-12-12 07:09:42 -06001537/* Location: ./system/libraries/Form_validation.php */