blob: 7c441409fb6debc8b6e494b82727f31de5055bad [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
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600147 * rules as input, any custom error messages, validates the info,
148 * and stores it
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400150 * @param mixed $field
151 * @param string $label
152 * @param mixed $rules
Ahmedul Haque Abid0742fad2014-01-09 07:51:10 +0600153 * @param array $errors
Andrew Podner4296a652012-12-17 07:51:15 -0500154 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 */
Ahmedul Haque Abid0742fad2014-01-09 07:51:10 +0600156 public function set_rules($field, $label = '', $rules = '', $errors = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
158 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +0000159 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200160 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Greg Aker9f9af602010-11-10 15:41:51 -0600162 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
tiyowanc2acb232012-03-14 21:24:00 +0400165 // If an array was passed via the first parameter instead of individual string
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 // values we cycle through it and recursively call this function.
167 if (is_array($field))
168 {
169 foreach ($field as $row)
170 {
171 // Houston, we have a problem...
Andrey Andreev78f55772012-04-03 19:59:08 +0300172 if ( ! isset($row['field'], $row['rules']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
174 continue;
175 }
176
177 // If the field label wasn't passed we use the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300178 $label = isset($row['label']) ? $row['label'] : $row['field'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000179
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600180 // Add the custom error message array
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600181 $errors = (isset($row['errors']) && is_array($row['errors'])) ? $row['errors'] : array();
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // Here we go!
Ahmedul Haque Abidbc1cbad2014-01-09 07:53:34 +0600184 $this->set_rules($row['field'], $label, $row['rules'], $errors);
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
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 Andreev7a7ad782012-11-12 17:21:01 +0200207 $indexes = array();
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200208 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200209 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200210 sscanf($field, '%[^[][', $indexes[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000211
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200212 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100214 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200216 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 $is_array = TRUE;
221 }
222 else
223 {
Barry Mienydd671972010-10-04 16:33:58 +0200224 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
227 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300229 'field' => $field,
230 'label' => $label,
231 'rules' => $rules,
Ahmedul Haque Abid7945d302014-01-09 16:50:23 +0600232 'errors' => $errors,
Andrey Andreev56454792012-05-17 14:32:19 +0300233 'is_array' => $is_array,
234 'keys' => $indexes,
235 'postdata' => NULL,
236 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000237 );
Greg Aker9f9af602010-11-10 15:41:51 -0600238
239 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241
242 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 /**
JonoB099c4782012-03-04 14:37:30 +0000245 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200246 *
JonoB099c4782012-03-04 14:37:30 +0000247 * If an array is set through this method, then this array will
248 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200249 *
250 * Note that if you are validating multiple arrays, then the
251 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000252 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200253 *
254 * @param array $data
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200255 * @return CI_Form_validation
JonoB099c4782012-03-04 14:37:30 +0000256 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200257 public function set_data(array $data)
JonoB099c4782012-03-04 14:37:30 +0000258 {
Andrey Andreeva4712f52014-01-06 11:25:46 +0200259 if ( ! empty($data))
JonoB099c4782012-03-04 14:37:30 +0000260 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200261 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000262 }
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200263
264 return $this;
JonoB099c4782012-03-04 14:37:30 +0000265 }
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
Andrew Podner4296a652012-12-17 07:51:15 -0500277 * @return CI_Form_validation
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
Andrew Podner4296a652012-12-17 07:51:15 -0500299 * @return CI_Form_validation
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 *
Andrey Andreeva4712f52014-01-06 11:25:46 +0200315 * @param string $field Field name
316 * @param string $prefix HTML start tag
Andrey Andreev868301a2014-01-06 12:29:50 +0200317 * @param string $suffix HTML end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300318 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200319 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200320 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 {
vlakoff35672462013-02-15 01:36:04 +0100367 // No errors, validation passes!
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 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
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200425 if (empty($group))
426 {
427 // Is there a validation rule for the particular URI being accessed?
428 $group = trim($this->CI->uri->ruri_string(), '/');
429 isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method;
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200432 $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules);
Barry Mienydd671972010-10-04 16:33:58 +0200433
Andrey Andreev901573c2012-01-11 01:40:48 +0200434 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200435 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200437 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 return FALSE;
439 }
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 // Load the language file containing error messages
443 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200444
Andrey Andreev751f2472012-11-03 18:26:27 +0200445 // Cycle through the rules for each field and match the corresponding $validation_data item
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200447 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200448 // Fetch the data from the validation_data array item and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 // 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 +0200450 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
JonoB099c4782012-03-04 14:37:30 +0000452 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
Andrey Andreevfff6c2a2012-05-13 22:15:23 +0300454 elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300456 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Andrey Andreev751f2472012-11-03 18:26:27 +0200460 // Execute validation rules
461 // Note: A second foreach (for now) is required in order to avoid false-positives
462 // for rules like 'matches', which correlate to other validation fields.
463 foreach ($this->_field_data as $field => $row)
464 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300465 // Don't try to validate if we have no rules set
466 if (empty($row['rules']))
467 {
468 continue;
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
471 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
473
474 // Did we end up with any errors?
475 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 if ($total_errors > 0)
477 {
478 $this->_safe_form_data = TRUE;
479 }
480
481 // Now we need to re-set the POST data with the new, processed data
482 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200483
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200484 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
486
487 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 /**
490 * Traverse a multidimensional $_POST array index until the data is found
491 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 * @param array
493 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300494 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200496 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100497 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200499 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200501 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Andrey Andreev2d48b4f2012-11-23 17:33:21 +0200504 // NULL must be returned for empty fields
Andrey Andreev44c34632012-11-23 18:46:34 +0200505 return ($array === '') ? NULL : $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
507
508 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 /**
511 * Re-populate the _POST array with our finalized and processed data
512 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200513 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200514 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100515 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 foreach ($this->_field_data as $field => $row)
518 {
vlakoff1228fe22013-01-14 01:30:09 +0100519 if ($row['postdata'] !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200521 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
523 if (isset($_POST[$row['field']]))
524 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200525 $_POST[$row['field']] = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 }
527 }
528 else
529 {
Derek Jones63eeae32009-02-10 19:08:56 +0000530 // start with a reference
531 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Jones63eeae32009-02-10 19:08:56 +0000533 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200534 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
Derek Jones63eeae32009-02-10 19:08:56 +0000536 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
538 else
539 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 foreach ($row['keys'] as $val)
541 {
Derek Jones63eeae32009-02-10 19:08:56 +0000542 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 }
544 }
Derek Jones63eeae32009-02-10 19:08:56 +0000545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000547 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 $array = array();
549 foreach ($row['postdata'] as $k => $v)
550 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200551 $array[$k] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
Derek Jones63eeae32009-02-10 19:08:56 +0000553
554 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 }
556 else
Derek Jones63eeae32009-02-10 19:08:56 +0000557 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200558 $post_ref = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
561 }
562 }
563 }
564
565 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 /**
568 * Executes the Validation routines
569 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 * @param array
571 * @param array
572 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300573 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200575 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100576 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
578 // If the $_POST data is an array we will run a recursive call
579 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200580 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 foreach ($postdata as $key => $val)
582 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300583 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 }
Barry Mienydd671972010-10-04 16:33:58 +0200585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 return;
587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 // If the field is blank, but NOT required, no further tests are necessary
590 $callback = FALSE;
Hashem Qolami05370bf2013-07-22 01:52:04 +0430591 if ( ! in_array('required', $rules) && ($postdata === NULL OR $postdata === ''))
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200594 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 $callback = TRUE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300597 $rules = array(1 => $match[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 }
599 else
600 {
601 return;
602 }
603 }
604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 // Isset Test. Typically this rule will only apply to checkboxes.
Hashem Qolami05370bf2013-07-22 01:52:04 +0430606 if (($postdata === NULL OR $postdata === '') && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
608 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
609 {
610 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300611 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200612
Ahmedul Haque Abide9b0ccc2014-01-09 15:58:51 +0600613 // Check if a custom message is defined
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600614 if (isset($this->_field_data[$row['field']]['errors'][$type]))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600615 {
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600616 $line = $this->_field_data[$row['field']]['errors'][$type];
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600617 }
618 elseif (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
620 $line = $this->_error_messages[$type];
621 }
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200622 elseif (FALSE === ($line = $this->CI->lang->line('form_validation_'.$type))
623 // DEPRECATED support for non-prefixed keys
624 && FALSE === ($line = $this->CI->lang->line($type, FALSE)))
Andrey Andreev56454792012-05-17 14:32:19 +0300625 {
626 $line = 'The field was not set';
627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600630 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
Derek Allard2067d1a2008-11-13 22:59:24 +0000631
632 // Save the error message
633 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 if ( ! isset($this->_error_array[$row['field']]))
636 {
637 $this->_error_array[$row['field']] = $message;
638 }
639 }
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 return;
642 }
643
644 // --------------------------------------------------------------------
645
646 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300647 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
649 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 // We set the $postdata variable with the current data in our master array so that
652 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100653 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
655 // We shouldn't need this safety, but just in case there isn't an array index
656 // associated with this cycle we'll bail out
657 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
658 {
659 continue;
660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
663 $_in_array = TRUE;
664 }
665 else
666 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300667 // If we get an array field, but it's not expected - then it is most likely
668 // somebody messing with the form on the client side, so we'll just consider
669 // it an empty field
670 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
671 ? NULL
672 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 }
674
Barry Mienydd671972010-10-04 16:33:58 +0200675 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200677 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
679 $rule = substr($rule, 9);
680 $callback = TRUE;
681 }
Barry Mienydd671972010-10-04 16:33:58 +0200682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 // Strip the parameter (if exists) from the rule
684 // Rules can contain a parameter: max_length[5]
685 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200686 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
Andrey Andreevef758bd2012-11-15 12:24:52 +0200688 $rule = $match[1];
689 $param = $match[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 // Call the function that corresponds to the rule
693 if ($callback === TRUE)
694 {
695 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200696 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200697 log_message('debug', 'Unable to find callback validation rule: '.$rule);
698 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200700 else
701 {
702 // Run the function and grab the result
703 $result = $this->CI->$rule($postdata, $param);
704 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000705
706 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200707 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300709 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 }
711 else
712 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300713 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 }
Barry Mienydd671972010-10-04 16:33:58 +0200715
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200717 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000719 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 }
721 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300722 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200723 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300724 // If our own wrapper function doesn't exist we see if a native PHP function does.
725 // Users can use any native PHP function call that has one param.
726 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 {
Andrey Andreev320d37c2012-04-03 20:21:39 +0300728 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200729
Andrey Andreev78f55772012-04-03 19:59:08 +0300730 if ($_in_array === TRUE)
731 {
732 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 }
patwork02404a12011-04-08 15:45:46 +0200734 else
735 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300736 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200737 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200739 else
740 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300741 log_message('debug', 'Unable to find validation rule: '.$rule);
742 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200743 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300744 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 else
746 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 $result = $this->$rule($postdata, $param);
748
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200749 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300751 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 }
753 else
754 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300755 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 }
757 }
Barry Mienydd671972010-10-04 16:33:58 +0200758
Andrey Andreev901573c2012-01-11 01:40:48 +0200759 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200761 {
Ahmedul Haque Abid7945d302014-01-09 16:50:23 +0600762 // Check if a custom message is defined
Ahmedul Haque Abidd8a37162014-01-09 16:03:43 +0600763 if (isset($this->_field_data[$row['field']]['errors'][$rule]))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600764 {
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600765 $line = $this->_field_data[$row['field']]['errors'][$rule];
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600766 }
767 elseif ( ! isset($this->_error_messages[$rule]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 {
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200769 if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule))
770 // DEPRECATED support for non-prefixed keys
771 && FALSE === ($line = $this->CI->lang->line($rule, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 {
773 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200774 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 }
776 else
777 {
778 $line = $this->_error_messages[$rule];
779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200782 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200783 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 {
Pascal Krietec1895832009-10-13 12:56:43 +0000785 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 }
Barry Mienydd671972010-10-04 16:33:58 +0200787
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600789 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000790
791 // Save the error message
792 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200793
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 if ( ! isset($this->_error_array[$row['field']]))
795 {
796 $this->_error_array[$row['field']] = $message;
797 }
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 return;
800 }
801 }
802 }
803
804 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 /**
807 * Translate a field name
808 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 * @param string the field name
810 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200811 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100812 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
814 // Do we need to translate the field name?
815 // We look for the prefix lang: to determine this
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200816 if (sscanf($fieldname, 'lang:%s', $line) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500818 // Were we able to translate the field name? If not we use $line
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200819 if (FALSE === ($fieldname = $this->CI->lang->line('form_validation_'.$line))
820 // DEPRECATED support for non-prefixed keys
821 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 {
823 return $line;
824 }
825 }
826
827 return $fieldname;
828 }
829
830 // --------------------------------------------------------------------
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200831
Eric Roberts41cc0902012-01-24 00:59:44 -0600832 /**
833 * Build an error message using the field and param.
834 *
835 * @param string The error message line
836 * @param string A field's human name
837 * @param mixed A rule's optional parameter
838 * @return string
839 */
840 protected function _build_error_msg($line, $field = '', $param = '')
841 {
842 // Check for %s in the string for legacy support.
Eric Roberts24a13f52012-12-12 07:09:42 -0600843 if (strpos($line, '%s') !== FALSE)
Eric Roberts41cc0902012-01-24 00:59:44 -0600844 {
845 return sprintf($line, $field, $param);
846 }
Andrew Podner4296a652012-12-17 07:51:15 -0500847
Eric Roberts41cc0902012-01-24 00:59:44 -0600848 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
849 }
850
851 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 /**
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530854 * Checks if the rule is present within the validator
855 *
856 * Permits you to check if a rule is present within the validator
857 *
858 * @param string the field name
859 * @return bool
860 */
861 public function has_rule($field)
862 {
863 return isset($this->_field_data[$field]);
864 }
865
866 // --------------------------------------------------------------------
867
868 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 * Get the value from a form
870 *
871 * Permits you to repopulate a form field with the value it was submitted
872 * with, or, if that value doesn't exist, with the default
873 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * @param string the field name
875 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200876 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200877 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100878 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200880 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
882 return $default;
883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Phil Sturgeon5c561802011-01-05 16:31:59 +0000885 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600886 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000887 if (is_array($this->_field_data[$field]['postdata']))
888 {
889 return array_shift($this->_field_data[$field]['postdata']);
890 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000891
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 return $this->_field_data[$field]['postdata'];
893 }
Barry Mienydd671972010-10-04 16:33:58 +0200894
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200896
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 /**
898 * Set Select
899 *
900 * Enables pull-down lists to be set to the value the user
901 * selected in the event of an error
902 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 * @param string
904 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400905 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200907 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100908 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200909 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200910 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200912 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
Barry Mienydd671972010-10-04 16:33:58 +0200914
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300916 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 if (is_array($field))
918 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300919 // Note: in_array('', array(0)) returns TRUE, do not use it
920 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300922 if ($value === $v)
923 {
924 return ' selected="selected"';
925 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300927
928 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100930 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200932 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 }
Barry Mienydd671972010-10-04 16:33:58 +0200934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 return ' selected="selected"';
936 }
Barry Mienydd671972010-10-04 16:33:58 +0200937
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 /**
941 * Set Radio
942 *
943 * Enables radio buttons to be set to the value the user
944 * selected in the event of an error
945 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 * @param string
947 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400948 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200950 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100951 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200953 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200955 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300959 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 if (is_array($field))
961 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300962 // Note: in_array('', array(0)) returns TRUE, do not use it
963 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300965 if ($value === $v)
966 {
967 return ' checked="checked"';
968 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300970
971 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100973 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200975 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 }
Barry Mienydd671972010-10-04 16:33:58 +0200977
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 return ' checked="checked"';
979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 /**
984 * Set Checkbox
985 *
986 * Enables checkboxes to be set to the value the user
987 * selected in the event of an error
988 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 * @param string
990 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400991 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200993 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100994 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200996 // Logic is exactly the same as for radio fields
997 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 }
Barry Mienydd671972010-10-04 16:33:58 +0200999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 /**
1003 * Required
1004 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 * @param string
1006 * @return bool
1007 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001008 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001010 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 }
Barry Mienydd671972010-10-04 16:33:58 +02001012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001016 * Performs a Regular Expression match test.
1017 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001018 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001019 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001020 * @return bool
1021 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001022 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001023 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001024 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001025 }
1026
1027 // --------------------------------------------------------------------
1028
1029 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 * Match one field to another
1031 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001032 * @param string $str string to compare against
1033 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 * @return bool
1035 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001036 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001038 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
1039 ? ($str === $this->_field_data[$field]['postdata'])
1040 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001042
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001043 // --------------------------------------------------------------------
1044
1045 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -03001046 * Differs from another field
1047 *
1048 * @param string
1049 * @param string field
1050 * @return bool
1051 */
1052 public function differs($str, $field)
1053 {
1054 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001056
1057 // --------------------------------------------------------------------
1058
1059 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +02001060 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001061 *
Andrey Andreevd09d6502012-01-03 06:38:33 +02001062 * Check if the input value doesn't already exist
1063 * in the specified database field.
1064 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001065 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001066 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001067 * @return bool
1068 */
1069 public function is_unique($str, $field)
1070 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001071 sscanf($field, '%[^.].%[^.]', $table, $field);
Eric Barnescccde962011-12-04 00:01:17 -05001072 if (isset($this->CI->db))
1073 {
1074 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
1075 return $query->num_rows() === 0;
1076 }
1077 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001078 }
Barry Mienydd671972010-10-04 16:33:58 +02001079
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001081
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 /**
1083 * Minimum Length
1084 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001086 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001088 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001089 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001091 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 {
1093 return FALSE;
1094 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001095 else
1096 {
1097 $val = (int) $val;
1098 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001099
Andrey Andreev78f55772012-04-03 19:59:08 +03001100 return (MB_ENABLED === TRUE)
1101 ? ($val <= mb_strlen($str))
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001102 : ($val <= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 }
Barry Mienydd671972010-10-04 16:33:58 +02001104
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001106
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 /**
1108 * Max Length
1109 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001111 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001113 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001114 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001116 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 {
1118 return FALSE;
1119 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001120 else
1121 {
1122 $val = (int) $val;
1123 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001124
Andrey Andreev78f55772012-04-03 19:59:08 +03001125 return (MB_ENABLED === TRUE)
1126 ? ($val >= mb_strlen($str))
1127 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 }
Barry Mienydd671972010-10-04 16:33:58 +02001129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001131
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 /**
1133 * Exact Length
1134 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001136 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001138 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001139 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001141 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 {
1143 return FALSE;
1144 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001145 else
1146 {
1147 $val = (int) $val;
1148 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001149
Andrey Andreev78f55772012-04-03 19:59:08 +03001150 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001151 ? (mb_strlen($str) === $val)
1152 : (strlen($str) === $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 }
Barry Mienydd671972010-10-04 16:33:58 +02001154
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001156
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 /**
Andrey Andreevdaaca882012-11-26 22:16:12 +02001158 * Valid URL
1159 *
1160 * @param string $str
1161 * @return bool
1162 */
1163 public function valid_url($str)
1164 {
1165 if (empty($str))
1166 {
1167 return FALSE;
1168 }
1169 elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
1170 {
1171 if (empty($matches[2]))
1172 {
1173 return FALSE;
1174 }
1175 elseif ( ! in_array($matches[1], array('http', 'https'), TRUE))
1176 {
1177 return FALSE;
1178 }
1179
1180 $str = $matches[2];
1181 }
1182
1183 $str = 'http://'.$str;
1184
1185 // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the
1186 // underscore to be a valid hostname character instead of a dash.
1187 // Reference: https://bugs.php.net/bug.php?id=51192
1188 if (version_compare(PHP_VERSION, '5.2.13', '==') === 0 OR version_compare(PHP_VERSION, '5.3.2', '==') === 0)
1189 {
1190 sscanf($str, 'http://%[^/]', $host);
1191 $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host));
1192 }
1193
1194 return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 }
1196
1197 // --------------------------------------------------------------------
1198
1199 /**
1200 * Valid Email
1201 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 * @param string
1203 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001204 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001205 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001207 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 }
1209
1210 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001211
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 /**
1213 * Valid Emails
1214 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 * @param string
1216 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001217 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001218 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 {
1220 if (strpos($str, ',') === FALSE)
1221 {
1222 return $this->valid_email(trim($str));
1223 }
Barry Mienydd671972010-10-04 16:33:58 +02001224
Pascal Kriete14287f32011-02-14 13:39:34 -05001225 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001227 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 {
1229 return FALSE;
1230 }
1231 }
Barry Mienydd671972010-10-04 16:33:58 +02001232
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 return TRUE;
1234 }
1235
1236 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 /**
1239 * Validate IP Address
1240 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001242 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001243 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001245 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001247 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 }
1249
1250 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 /**
1253 * Alpha
1254 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 * @param string
1256 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001257 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001258 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001260 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 }
Barry Mienydd671972010-10-04 16:33:58 +02001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001264
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 /**
1266 * Alpha-numeric
1267 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 * @param string
1269 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001270 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001271 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001273 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 }
Barry Mienydd671972010-10-04 16:33:58 +02001275
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 // --------------------------------------------------------------------
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001277
1278 /**
1279 * Alpha-numeric w/ spaces
1280 *
1281 * @param string
1282 * @return bool
1283 */
1284 public function alpha_numeric_spaces($str)
1285 {
Sajan Parikhdf3bfed2013-02-04 12:25:49 -06001286 return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001287 }
1288
1289 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 /**
1292 * Alpha-numeric with underscores and dashes
1293 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 * @param string
1295 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001296 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001297 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001299 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 }
Barry Mienydd671972010-10-04 16:33:58 +02001301
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001303
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 /**
1305 * Numeric
1306 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 * @param string
1308 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001309 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001310 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001312 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001313
1314 }
1315
1316 // --------------------------------------------------------------------
1317
Barry Mienydd671972010-10-04 16:33:58 +02001318 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 * Integer
1320 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 * @param string
1322 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001323 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001324 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001326 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1327 }
1328
1329 // --------------------------------------------------------------------
1330
1331 /**
1332 * Decimal number
1333 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001334 * @param string
1335 * @return bool
1336 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001337 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001338 {
1339 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1340 }
1341
1342 // --------------------------------------------------------------------
1343
1344 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001345 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001346 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001347 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001348 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001349 * @return bool
1350 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001351 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001352 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001353 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001354 }
1355
1356 // --------------------------------------------------------------------
1357
1358 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001359 * Equal to or Greater than
1360 *
Nick Busey98c347d2012-02-02 11:07:03 -07001361 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001362 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001363 * @return bool
1364 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001365 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001366 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001367 return is_numeric($str) ? ($str >= $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001368 }
1369
1370 // --------------------------------------------------------------------
1371
1372 /**
1373 * Less than
1374 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001375 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001376 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001377 * @return bool
1378 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001379 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001380 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001381 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001383
1384 // --------------------------------------------------------------------
1385
Barry Mienydd671972010-10-04 16:33:58 +02001386 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001387 * Equal to or Less than
1388 *
Nick Busey98c347d2012-02-02 11:07:03 -07001389 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001390 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001391 * @return bool
1392 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001393 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001394 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001395 return is_numeric($str) ? ($str <= $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001396 }
1397
1398 // --------------------------------------------------------------------
1399
Barry Mienydd671972010-10-04 16:33:58 +02001400 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001401 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001402 *
Barry Mienydd671972010-10-04 16:33:58 +02001403 * @param string
1404 * @return bool
1405 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001406 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001407 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001408 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001409 }
1410
1411 // --------------------------------------------------------------------
1412
1413 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001414 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001415 *
Barry Mienydd671972010-10-04 16:33:58 +02001416 * @param string
1417 * @return bool
1418 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001419 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001420 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001421 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001422 }
1423
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001425
Derek Allard2067d1a2008-11-13 22:59:24 +00001426 /**
1427 * Valid Base64
1428 *
1429 * Tests a string for characters outside of the Base64 alphabet
1430 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1431 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 * @param string
1433 * @return bool
1434 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001435 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 {
Andrey Andreevcd9797a2013-06-28 14:03:48 +03001437 return (base64_encode(base64_decode($str)) === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 }
Barry Mienydd671972010-10-04 16:33:58 +02001439
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001441
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 /**
1443 * Prep data for form
1444 *
1445 * This function allows HTML to be safely shown in a form.
1446 * Special characters are converted.
1447 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 * @param string
1449 * @return string
1450 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001451 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001453 if ($this->_safe_form_data === FALSE OR empty($data))
1454 {
1455 return $data;
1456 }
1457
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 if (is_array($data))
1459 {
1460 foreach ($data as $key => $val)
1461 {
1462 $data[$key] = $this->prep_for_form($val);
1463 }
Barry Mienydd671972010-10-04 16:33:58 +02001464
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 return $data;
1466 }
Barry Mienydd671972010-10-04 16:33:58 +02001467
Andrey Andreev901573c2012-01-11 01:40:48 +02001468 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 }
Barry Mienydd671972010-10-04 16:33:58 +02001470
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001472
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 /**
1474 * Prep URL
1475 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 * @param string
1477 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001478 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001479 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001481 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 {
1483 return '';
1484 }
Barry Mienydd671972010-10-04 16:33:58 +02001485
Andrey Andreev901573c2012-01-11 01:40:48 +02001486 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001487 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001488 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 }
Barry Mienydd671972010-10-04 16:33:58 +02001490
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 return $str;
1492 }
Barry Mienydd671972010-10-04 16:33:58 +02001493
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001495
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 /**
1497 * Strip Image Tags
1498 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 * @param string
1500 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001501 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001502 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001504 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 }
Barry Mienydd671972010-10-04 16:33:58 +02001506
Derek Allard2067d1a2008-11-13 22:59:24 +00001507 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001508
Derek Allard2067d1a2008-11-13 22:59:24 +00001509 /**
1510 * XSS Clean
1511 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 * @param string
1513 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001514 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001515 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 {
Derek Jones5640a712010-04-23 11:22:40 -05001517 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 }
Barry Mienydd671972010-10-04 16:33:58 +02001519
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001521
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 /**
1523 * Convert PHP tags to entities
1524 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 * @param string
1526 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001527 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001528 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 {
Andrey Andreev838a9d62012-12-03 14:37:47 +02001530 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 }
1532
JonoB099c4782012-03-04 14:37:30 +00001533 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001534
1535 /**
1536 * Reset validation vars
1537 *
1538 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001539 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001540 *
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001541 * @return CI_Form_validation
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001542 */
JonoB883f80f2012-03-05 09:51:27 +00001543 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001544 {
JonoB099c4782012-03-04 14:37:30 +00001545 $this->_field_data = array();
1546 $this->_config_rules = array();
1547 $this->_error_array = array();
1548 $this->_error_messages = array();
1549 $this->error_string = '';
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001550 return $this;
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001551 }
1552
Derek Allard2067d1a2008-11-13 22:59:24 +00001553}
Derek Allard2067d1a2008-11-13 22:59:24 +00001554
1555/* End of file Form_validation.php */
Eric Roberts24a13f52012-12-12 07:09:42 -06001556/* Location: ./system/libraries/Form_validation.php */