blob: 2f072343d9176806931d83079caf71128e275898 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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
Andrey Andreev4b90a372014-03-10 10:24:24 +0200147 * rules as input, any custom error messages, validates the info,
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600148 * 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 */
Andrey Andreev4b90a372014-03-10 10:24:24 +0200156 public function set_rules($field, $label = '', $rules = array(), $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
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // No fields? Nothing to do...
Andrey Andreev4b90a372014-03-10 10:24:24 +0200191 if ( ! is_string($field) OR $field === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Greg Aker9f9af602010-11-10 15:41:51 -0600193 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 }
Andrey Andreev4b90a372014-03-10 10:24:24 +0200195 elseif ( ! is_array($rules))
196 {
197 // BC: Convert pipe-separated rules string to an array
198 if (is_string($rules))
199 {
200 $rules = explode('|', $rules);
201 }
202 else
203 {
204 return $this;
205 }
206 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000207
208 // If the field label wasn't passed we use the field name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100209 $label = ($label === '') ? $field : $label;
Derek Allard2067d1a2008-11-13 22:59:24 +0000210
Andrey Andreevfde170c2014-03-10 19:55:11 +0200211 $indexes = array();
212
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200213 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200214 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev4b90a372014-03-10 10:24:24 +0200215 if (($is_array = (bool) preg_match_all('/\[(.*?)\]/', $field, $matches)) === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200216 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200217 sscanf($field, '%[^[][', $indexes[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200219 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100221 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200223 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
228 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300230 'field' => $field,
231 'label' => $label,
232 'rules' => $rules,
Ahmedul Haque Abid7945d302014-01-09 16:50:23 +0600233 'errors' => $errors,
Andrey Andreev56454792012-05-17 14:32:19 +0300234 'is_array' => $is_array,
235 'keys' => $indexes,
236 'postdata' => NULL,
237 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000238 );
Greg Aker9f9af602010-11-10 15:41:51 -0600239
240 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
242
243 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 /**
JonoB099c4782012-03-04 14:37:30 +0000246 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200247 *
JonoB099c4782012-03-04 14:37:30 +0000248 * If an array is set through this method, then this array will
249 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200250 *
251 * Note that if you are validating multiple arrays, then the
252 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000253 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200254 *
255 * @param array $data
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200256 * @return CI_Form_validation
JonoB099c4782012-03-04 14:37:30 +0000257 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200258 public function set_data(array $data)
JonoB099c4782012-03-04 14:37:30 +0000259 {
Andrey Andreeva4712f52014-01-06 11:25:46 +0200260 if ( ! empty($data))
JonoB099c4782012-03-04 14:37:30 +0000261 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200262 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000263 }
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200264
265 return $this;
JonoB099c4782012-03-04 14:37:30 +0000266 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200267
JonoB099c4782012-03-04 14:37:30 +0000268 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000269
270 /**
271 * Set Error Message
272 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300273 * Lets users set their own error messages on the fly. Note:
274 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300276 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500278 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100280 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 if ( ! is_array($lang))
283 {
284 $lang = array($lang => $val);
285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600288 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 /**
294 * Set The Error Delimiter
295 *
296 * Permits a prefix/suffix to be added to each error message
297 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 * @param string
299 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500300 * @return CI_Form_validation
Barry Mienydd671972010-10-04 16:33:58 +0200301 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100302 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 $this->_error_prefix = $prefix;
305 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600306 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308
309 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 /**
312 * Get Error Message
313 *
314 * Gets the error message associated with a particular field
315 *
Andrey Andreeva4712f52014-01-06 11:25:46 +0200316 * @param string $field Field name
317 * @param string $prefix HTML start tag
Andrey Andreev868301a2014-01-06 12:29:50 +0200318 * @param string $suffix HTML end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300319 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200320 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200321 public function error($field, $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200322 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300323 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
325 return '';
326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Alex Bilbied261b1e2012-06-02 11:12:16 +0100328 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 $prefix = $this->_error_prefix;
331 }
332
Alex Bilbied261b1e2012-06-02 11:12:16 +0100333 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 $suffix = $this->_error_suffix;
336 }
337
338 return $prefix.$this->_field_data[$field]['error'].$suffix;
339 }
340
341 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100344 * Get Array of Error Messages
345 *
346 * Returns the error messages as an array
347 *
348 * @return array
349 */
350 public function error_array()
351 {
352 return $this->_error_array;
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 * Error String
359 *
360 * Returns the error messages as a string, wrapped in the error delimiters
361 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 * @param string
363 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300364 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200365 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100366 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
vlakoff35672462013-02-15 01:36:04 +0100368 // No errors, validation passes!
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 if (count($this->_error_array) === 0)
370 {
371 return '';
372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Alex Bilbied261b1e2012-06-02 11:12:16 +0100374 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 $prefix = $this->_error_prefix;
377 }
378
Alex Bilbied261b1e2012-06-02 11:12:16 +0100379 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 $suffix = $this->_error_suffix;
382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 // Generate the error string
385 $str = '';
386 foreach ($this->_error_array as $val)
387 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100388 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 $str .= $prefix.$val.$suffix."\n";
391 }
392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 return $str;
395 }
396
397 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 /**
400 * Run the Validator
401 *
402 * This function does all the work.
403 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400404 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200406 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100407 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500409 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300410 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000411 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
413 return FALSE;
414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 // Does the _field_data array containing the validation rules exist?
417 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200418 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500420 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200421 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 return FALSE;
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200426 if (empty($group))
427 {
428 // Is there a validation rule for the particular URI being accessed?
429 $group = trim($this->CI->uri->ruri_string(), '/');
430 isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method;
431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200433 $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules);
Barry Mienydd671972010-10-04 16:33:58 +0200434
Andrey Andreev901573c2012-01-11 01:40:48 +0200435 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200436 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200438 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 return FALSE;
440 }
441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 // Load the language file containing error messages
444 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200445
Andrey Andreev751f2472012-11-03 18:26:27 +0200446 // Cycle through the rules for each field and match the corresponding $validation_data item
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200448 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200449 // Fetch the data from the validation_data array item and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 // 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 +0200451 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
JonoB099c4782012-03-04 14:37:30 +0000453 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
Andrey Andreevfff6c2a2012-05-13 22:15:23 +0300455 elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300457 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Andrey Andreev751f2472012-11-03 18:26:27 +0200461 // Execute validation rules
462 // Note: A second foreach (for now) is required in order to avoid false-positives
463 // for rules like 'matches', which correlate to other validation fields.
464 foreach ($this->_field_data as $field => $row)
465 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300466 // Don't try to validate if we have no rules set
467 if (empty($row['rules']))
468 {
469 continue;
470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Andrey Andreev4b90a372014-03-10 10:24:24 +0200472 $this->_execute($row, $row['rules'], $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474
475 // Did we end up with any errors?
476 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 if ($total_errors > 0)
478 {
479 $this->_safe_form_data = TRUE;
480 }
481
482 // Now we need to re-set the POST data with the new, processed data
483 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200484
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200485 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
487
488 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 /**
491 * Traverse a multidimensional $_POST array index until the data is found
492 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 * @param array
494 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300495 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200497 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100498 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200500 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200502 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Andrey Andreev2d48b4f2012-11-23 17:33:21 +0200505 // NULL must be returned for empty fields
Andrey Andreev44c34632012-11-23 18:46:34 +0200506 return ($array === '') ? NULL : $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508
509 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 /**
512 * Re-populate the _POST array with our finalized and processed data
513 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200514 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200515 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100516 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 foreach ($this->_field_data as $field => $row)
519 {
vlakoff1228fe22013-01-14 01:30:09 +0100520 if ($row['postdata'] !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200522 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 if (isset($_POST[$row['field']]))
525 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200526 $_POST[$row['field']] = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
528 }
529 else
530 {
Derek Jones63eeae32009-02-10 19:08:56 +0000531 // start with a reference
532 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Jones63eeae32009-02-10 19:08:56 +0000534 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200535 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
Derek Jones63eeae32009-02-10 19:08:56 +0000537 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
539 else
540 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 foreach ($row['keys'] as $val)
542 {
Derek Jones63eeae32009-02-10 19:08:56 +0000543 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
545 }
Derek Jones63eeae32009-02-10 19:08:56 +0000546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000548 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 $array = array();
550 foreach ($row['postdata'] as $k => $v)
551 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200552 $array[$k] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
Derek Jones63eeae32009-02-10 19:08:56 +0000554
555 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 }
557 else
Derek Jones63eeae32009-02-10 19:08:56 +0000558 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200559 $post_ref = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
562 }
563 }
564 }
565
566 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 /**
569 * Executes the Validation routines
570 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * @param array
572 * @param array
573 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300574 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200576 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100577 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
579 // If the $_POST data is an array we will run a recursive call
580 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200581 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 foreach ($postdata as $key => $val)
583 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300584 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 return;
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 // If the field is blank, but NOT required, no further tests are necessary
591 $callback = FALSE;
Hashem Qolami05370bf2013-07-22 01:52:04 +0430592 if ( ! in_array('required', $rules) && ($postdata === NULL OR $postdata === ''))
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
594 // Before we bail out, does the rule contain a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200595 foreach ($rules as &$rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200597 if (is_string($rule))
598 {
599 if (strncmp($rule, 'callback_', 9) === 0)
600 {
601 $callback = TRUE;
602 $rules = array(1 => $rule);
603 break;
604 }
605 }
606 elseif (is_callable($rule))
607 {
608 $callback = TRUE;
609 $rules = array(1 => $rule);
610 break;
611 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
Andrey Andreev4b90a372014-03-10 10:24:24 +0200613
614 if ( ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 {
616 return;
617 }
618 }
619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev4b90a372014-03-10 10:24:24 +0200621 if (($postdata === NULL OR $postdata === '') && ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
624 {
625 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300626 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200627
Ahmedul Haque Abide9b0ccc2014-01-09 15:58:51 +0600628 // Check if a custom message is defined
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600629 if (isset($this->_field_data[$row['field']]['errors'][$type]))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600630 {
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600631 $line = $this->_field_data[$row['field']]['errors'][$type];
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600632 }
633 elseif (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
635 $line = $this->_error_messages[$type];
636 }
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200637 elseif (FALSE === ($line = $this->CI->lang->line('form_validation_'.$type))
638 // DEPRECATED support for non-prefixed keys
639 && FALSE === ($line = $this->CI->lang->line($type, FALSE)))
Andrey Andreev56454792012-05-17 14:32:19 +0300640 {
641 $line = 'The field was not set';
642 }
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600645 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
Derek Allard2067d1a2008-11-13 22:59:24 +0000646
647 // Save the error message
648 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 if ( ! isset($this->_error_array[$row['field']]))
651 {
652 $this->_error_array[$row['field']] = $message;
653 }
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 return;
657 }
658
659 // --------------------------------------------------------------------
660
661 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300662 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
664 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 // We set the $postdata variable with the current data in our master array so that
667 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100668 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 {
670 // We shouldn't need this safety, but just in case there isn't an array index
671 // associated with this cycle we'll bail out
672 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
673 {
674 continue;
675 }
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
678 $_in_array = TRUE;
679 }
680 else
681 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300682 // If we get an array field, but it's not expected - then it is most likely
683 // somebody messing with the form on the client side, so we'll just consider
684 // it an empty field
685 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
Andrey Andreev4b90a372014-03-10 10:24:24 +0200686 ? NULL
687 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 }
689
Barry Mienydd671972010-10-04 16:33:58 +0200690 // Is the rule a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200691 $callback = $callable = FALSE;
692 if (is_string($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200694 if (strpos($rule, 'callback_') === 0)
695 {
696 $rule = substr($rule, 9);
697 $callback = TRUE;
698 }
699 }
700 elseif (is_callable($rule))
701 {
702 $callable = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 }
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 // Strip the parameter (if exists) from the rule
706 // Rules can contain a parameter: max_length[5]
707 $param = FALSE;
Andrey Andreev4b90a372014-03-10 10:24:24 +0200708 if ( ! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 {
Andrey Andreevef758bd2012-11-15 12:24:52 +0200710 $rule = $match[1];
711 $param = $match[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 // Call the function that corresponds to the rule
Andrey Andreev4b90a372014-03-10 10:24:24 +0200715 if ($callback OR $callable)
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200717 if ($callback)
Barry Mienydd671972010-10-04 16:33:58 +0200718 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200719 if ( ! method_exists($this->CI, $rule))
720 {
721 log_message('debug', 'Unable to find callback validation rule: '.$rule);
722 $result = FALSE;
723 }
724 else
725 {
726 // Run the function and grab the result
727 $result = $this->CI->$rule($postdata, $param);
728 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200730 else
731 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200732 $result = is_array($rule)
733 ? $rule[0]->{$rule[1]}($postdata, $param)
734 : $rule($postdata, $param);
Andrey Andreev901573c2012-01-11 01:40:48 +0200735 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000736
737 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200738 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300740 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 }
742 else
743 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300744 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 }
Barry Mienydd671972010-10-04 16:33:58 +0200746
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200748 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000750 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 }
752 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300753 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200754 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300755 // If our own wrapper function doesn't exist we see if a native PHP function does.
756 // Users can use any native PHP function call that has one param.
757 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200759 // Native PHP functions issue warnings if you pass them more parameters than they use
Andrey Andreev320d37c2012-04-03 20:21:39 +0300760 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200761
Andrey Andreev78f55772012-04-03 19:59:08 +0300762 if ($_in_array === TRUE)
763 {
764 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 }
patwork02404a12011-04-08 15:45:46 +0200766 else
767 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300768 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200769 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200771 else
772 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300773 log_message('debug', 'Unable to find validation rule: '.$rule);
774 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200775 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300776 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 else
778 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 $result = $this->$rule($postdata, $param);
780
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200781 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300783 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
785 else
786 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300787 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 }
789 }
Barry Mienydd671972010-10-04 16:33:58 +0200790
Andrey Andreev901573c2012-01-11 01:40:48 +0200791 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200793 {
Ahmedul Haque Abid7945d302014-01-09 16:50:23 +0600794 // Check if a custom message is defined
Ahmedul Haque Abidd8a37162014-01-09 16:03:43 +0600795 if (isset($this->_field_data[$row['field']]['errors'][$rule]))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600796 {
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600797 $line = $this->_field_data[$row['field']]['errors'][$rule];
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600798 }
799 elseif ( ! isset($this->_error_messages[$rule]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 {
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200801 if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule))
802 // DEPRECATED support for non-prefixed keys
803 && FALSE === ($line = $this->CI->lang->line($rule, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 {
805 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200806 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 }
808 else
809 {
810 $line = $this->_error_messages[$rule];
811 }
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200814 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200815 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
Pascal Krietec1895832009-10-13 12:56:43 +0000817 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 }
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600821 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000822
823 // Save the error message
824 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 if ( ! isset($this->_error_array[$row['field']]))
827 {
828 $this->_error_array[$row['field']] = $message;
829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 return;
832 }
833 }
834 }
835
836 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 /**
839 * Translate a field name
840 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 * @param string the field name
842 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200843 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100844 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 {
846 // Do we need to translate the field name?
847 // We look for the prefix lang: to determine this
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200848 if (sscanf($fieldname, 'lang:%s', $line) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500850 // Were we able to translate the field name? If not we use $line
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200851 if (FALSE === ($fieldname = $this->CI->lang->line('form_validation_'.$line))
852 // DEPRECATED support for non-prefixed keys
853 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 {
855 return $line;
856 }
857 }
858
859 return $fieldname;
860 }
861
862 // --------------------------------------------------------------------
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200863
Eric Roberts41cc0902012-01-24 00:59:44 -0600864 /**
865 * Build an error message using the field and param.
866 *
867 * @param string The error message line
868 * @param string A field's human name
869 * @param mixed A rule's optional parameter
870 * @return string
871 */
872 protected function _build_error_msg($line, $field = '', $param = '')
873 {
874 // Check for %s in the string for legacy support.
Eric Roberts24a13f52012-12-12 07:09:42 -0600875 if (strpos($line, '%s') !== FALSE)
Eric Roberts41cc0902012-01-24 00:59:44 -0600876 {
877 return sprintf($line, $field, $param);
878 }
Andrew Podner4296a652012-12-17 07:51:15 -0500879
Eric Roberts41cc0902012-01-24 00:59:44 -0600880 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
881 }
882
883 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 /**
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530886 * Checks if the rule is present within the validator
887 *
888 * Permits you to check if a rule is present within the validator
889 *
890 * @param string the field name
891 * @return bool
892 */
893 public function has_rule($field)
894 {
895 return isset($this->_field_data[$field]);
896 }
897
898 // --------------------------------------------------------------------
899
900 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 * Get the value from a form
902 *
903 * Permits you to repopulate a form field with the value it was submitted
904 * with, or, if that value doesn't exist, with the default
905 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 * @param string the field name
907 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200908 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200909 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100910 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200912 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
914 return $default;
915 }
Barry Mienydd671972010-10-04 16:33:58 +0200916
Phil Sturgeon5c561802011-01-05 16:31:59 +0000917 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600918 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000919 if (is_array($this->_field_data[$field]['postdata']))
920 {
921 return array_shift($this->_field_data[$field]['postdata']);
922 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 return $this->_field_data[$field]['postdata'];
925 }
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 /**
930 * Set Select
931 *
932 * Enables pull-down lists to be set to the value the user
933 * selected in the event of an error
934 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 * @param string
936 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400937 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200939 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100940 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200941 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200942 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200944 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300948 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 if (is_array($field))
950 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300951 // Note: in_array('', array(0)) returns TRUE, do not use it
952 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300954 if ($value === $v)
955 {
956 return ' selected="selected"';
957 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300959
960 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100962 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200964 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 return ' selected="selected"';
968 }
Barry Mienydd671972010-10-04 16:33:58 +0200969
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 /**
973 * Set Radio
974 *
975 * Enables radio buttons to be set to the value the user
976 * selected in the event of an error
977 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 * @param string
979 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400980 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200982 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100983 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200985 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200987 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300991 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 if (is_array($field))
993 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300994 // Note: in_array('', array(0)) returns TRUE, do not use it
995 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300997 if ($value === $v)
998 {
999 return ' checked="checked"';
1000 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 }
Andrey Andreeva587a932013-10-23 19:57:46 +03001002
1003 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 }
Alex Bilbied261b1e2012-06-02 11:12:16 +01001005 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001007 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 }
Barry Mienydd671972010-10-04 16:33:58 +02001009
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 return ' checked="checked"';
1011 }
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 /**
1016 * Set Checkbox
1017 *
1018 * Enables checkboxes to be set to the value the user
1019 * selected in the event of an error
1020 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 * @param string
1022 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001023 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001025 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001026 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001028 // Logic is exactly the same as for radio fields
1029 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 }
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 /**
1035 * Required
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @param string
1038 * @return bool
1039 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001040 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001042 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 }
Barry Mienydd671972010-10-04 16:33:58 +02001044
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001046
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001048 * Performs a Regular Expression match test.
1049 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001050 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001051 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001052 * @return bool
1053 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001054 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001055 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001056 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001057 }
1058
1059 // --------------------------------------------------------------------
1060
1061 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 * Match one field to another
1063 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001064 * @param string $str string to compare against
1065 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 * @return bool
1067 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001068 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001070 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
1071 ? ($str === $this->_field_data[$field]['postdata'])
1072 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001074
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001075 // --------------------------------------------------------------------
1076
1077 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -03001078 * Differs from another field
1079 *
1080 * @param string
1081 * @param string field
1082 * @return bool
1083 */
1084 public function differs($str, $field)
1085 {
1086 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001088
1089 // --------------------------------------------------------------------
1090
1091 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +02001092 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001093 *
Andrey Andreevd09d6502012-01-03 06:38:33 +02001094 * Check if the input value doesn't already exist
1095 * in the specified database field.
1096 *
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001097 * @param string $str
1098 * @param string $field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001099 * @return bool
1100 */
1101 public function is_unique($str, $field)
1102 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001103 sscanf($field, '%[^.].%[^.]', $table, $field);
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001104 return isset($this->CI->db)
1105 ? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
1106 : FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001107 }
Barry Mienydd671972010-10-04 16:33:58 +02001108
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 /**
1112 * Minimum Length
1113 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001115 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001117 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001118 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001120 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
1122 return FALSE;
1123 }
1124
Andrey Andreev78f55772012-04-03 19:59:08 +03001125 return (MB_ENABLED === TRUE)
1126 ? ($val <= mb_strlen($str))
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001127 : ($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 * Max Length
1134 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +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 max_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 }
1145
Andrey Andreev78f55772012-04-03 19:59:08 +03001146 return (MB_ENABLED === TRUE)
1147 ? ($val >= mb_strlen($str))
1148 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 }
Barry Mienydd671972010-10-04 16:33:58 +02001150
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001152
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 /**
1154 * Exact Length
1155 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001157 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001159 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001160 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001162 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
1164 return FALSE;
1165 }
1166
Andrey Andreev78f55772012-04-03 19:59:08 +03001167 return (MB_ENABLED === TRUE)
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001168 ? (mb_strlen($str) === (int) $val)
1169 : (strlen($str) === (int) $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 }
Barry Mienydd671972010-10-04 16:33:58 +02001171
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001173
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 /**
Andrey Andreevdaaca882012-11-26 22:16:12 +02001175 * Valid URL
1176 *
1177 * @param string $str
1178 * @return bool
1179 */
1180 public function valid_url($str)
1181 {
1182 if (empty($str))
1183 {
1184 return FALSE;
1185 }
1186 elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
1187 {
1188 if (empty($matches[2]))
1189 {
1190 return FALSE;
1191 }
1192 elseif ( ! in_array($matches[1], array('http', 'https'), TRUE))
1193 {
1194 return FALSE;
1195 }
1196
1197 $str = $matches[2];
1198 }
1199
1200 $str = 'http://'.$str;
1201
1202 // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the
1203 // underscore to be a valid hostname character instead of a dash.
1204 // Reference: https://bugs.php.net/bug.php?id=51192
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001205 if (version_compare(PHP_VERSION, '5.2.13', '==') OR version_compare(PHP_VERSION, '5.3.2', '=='))
Andrey Andreevdaaca882012-11-26 22:16:12 +02001206 {
1207 sscanf($str, 'http://%[^/]', $host);
1208 $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host));
1209 }
1210
1211 return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 }
1213
1214 // --------------------------------------------------------------------
1215
1216 /**
1217 * Valid Email
1218 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 * @param string
1220 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001221 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001222 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001224 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 }
1226
1227 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001228
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 /**
1230 * Valid Emails
1231 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 * @param string
1233 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001234 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001235 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 {
1237 if (strpos($str, ',') === FALSE)
1238 {
1239 return $this->valid_email(trim($str));
1240 }
Barry Mienydd671972010-10-04 16:33:58 +02001241
Pascal Kriete14287f32011-02-14 13:39:34 -05001242 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001244 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 {
1246 return FALSE;
1247 }
1248 }
Barry Mienydd671972010-10-04 16:33:58 +02001249
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 return TRUE;
1251 }
1252
1253 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001254
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 /**
1256 * Validate IP Address
1257 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001259 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001260 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001262 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001264 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 }
1266
1267 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 /**
1270 * Alpha
1271 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 * @param string
1273 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001274 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001275 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001277 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 }
Barry Mienydd671972010-10-04 16:33:58 +02001279
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001281
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 /**
1283 * Alpha-numeric
1284 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 * @param string
1286 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001287 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001288 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001290 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 }
Barry Mienydd671972010-10-04 16:33:58 +02001292
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 // --------------------------------------------------------------------
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001294
1295 /**
1296 * Alpha-numeric w/ spaces
1297 *
1298 * @param string
1299 * @return bool
1300 */
1301 public function alpha_numeric_spaces($str)
1302 {
Sajan Parikhdf3bfed2013-02-04 12:25:49 -06001303 return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001304 }
1305
1306 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001307
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 /**
1309 * Alpha-numeric with underscores and dashes
1310 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 * @param string
1312 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001313 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001314 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001316 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 }
Barry Mienydd671972010-10-04 16:33:58 +02001318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001320
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 /**
1322 * Numeric
1323 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 * @param string
1325 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001326 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001327 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001329 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001330
1331 }
1332
1333 // --------------------------------------------------------------------
1334
Barry Mienydd671972010-10-04 16:33:58 +02001335 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 * Integer
1337 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 * @param string
1339 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001340 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001341 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001343 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1344 }
1345
1346 // --------------------------------------------------------------------
1347
1348 /**
1349 * Decimal number
1350 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001351 * @param string
1352 * @return bool
1353 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001354 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001355 {
1356 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1357 }
1358
1359 // --------------------------------------------------------------------
1360
1361 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001362 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001363 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001364 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001365 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001366 * @return bool
1367 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001368 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001369 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001370 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001371 }
1372
1373 // --------------------------------------------------------------------
1374
1375 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001376 * Equal to or Greater than
1377 *
Nick Busey98c347d2012-02-02 11:07:03 -07001378 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001379 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001380 * @return bool
1381 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001382 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001383 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001384 return is_numeric($str) ? ($str >= $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001385 }
1386
1387 // --------------------------------------------------------------------
1388
1389 /**
1390 * Less than
1391 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001392 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001393 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001394 * @return bool
1395 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001396 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001397 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001398 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001400
1401 // --------------------------------------------------------------------
1402
Barry Mienydd671972010-10-04 16:33:58 +02001403 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001404 * Equal to or Less than
1405 *
Nick Busey98c347d2012-02-02 11:07:03 -07001406 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001407 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001408 * @return bool
1409 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001410 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001411 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001412 return is_numeric($str) ? ($str <= $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 }
1414
1415 // --------------------------------------------------------------------
1416
Barry Mienydd671972010-10-04 16:33:58 +02001417 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001418 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001419 *
Barry Mienydd671972010-10-04 16:33:58 +02001420 * @param string
1421 * @return bool
1422 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001423 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001424 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001425 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001426 }
1427
1428 // --------------------------------------------------------------------
1429
1430 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001431 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001432 *
Barry Mienydd671972010-10-04 16:33:58 +02001433 * @param string
1434 * @return bool
1435 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001436 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001437 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001438 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001439 }
1440
Derek Allard2067d1a2008-11-13 22:59:24 +00001441 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001442
Derek Allard2067d1a2008-11-13 22:59:24 +00001443 /**
1444 * Valid Base64
1445 *
1446 * Tests a string for characters outside of the Base64 alphabet
1447 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1448 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 * @param string
1450 * @return bool
1451 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001452 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 {
Andrey Andreevcd9797a2013-06-28 14:03:48 +03001454 return (base64_encode(base64_decode($str)) === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 }
Barry Mienydd671972010-10-04 16:33:58 +02001456
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001458
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 /**
1460 * Prep data for form
1461 *
1462 * This function allows HTML to be safely shown in a form.
1463 * Special characters are converted.
1464 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 * @param string
1466 * @return string
1467 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001468 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001470 if ($this->_safe_form_data === FALSE OR empty($data))
1471 {
1472 return $data;
1473 }
1474
Derek Allard2067d1a2008-11-13 22:59:24 +00001475 if (is_array($data))
1476 {
1477 foreach ($data as $key => $val)
1478 {
1479 $data[$key] = $this->prep_for_form($val);
1480 }
Barry Mienydd671972010-10-04 16:33:58 +02001481
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 return $data;
1483 }
Barry Mienydd671972010-10-04 16:33:58 +02001484
Andrey Andreev901573c2012-01-11 01:40:48 +02001485 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 }
Barry Mienydd671972010-10-04 16:33:58 +02001487
Derek Allard2067d1a2008-11-13 22:59:24 +00001488 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001489
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 /**
1491 * Prep URL
1492 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 * @param string
1494 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001495 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001496 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001497 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001498 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 {
1500 return '';
1501 }
Barry Mienydd671972010-10-04 16:33:58 +02001502
Andrey Andreev901573c2012-01-11 01:40:48 +02001503 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001505 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 }
Barry Mienydd671972010-10-04 16:33:58 +02001507
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 return $str;
1509 }
Barry Mienydd671972010-10-04 16:33:58 +02001510
Derek Allard2067d1a2008-11-13 22:59:24 +00001511 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001512
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 /**
1514 * Strip Image Tags
1515 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 * @param string
1517 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001518 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001519 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001521 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 }
Barry Mienydd671972010-10-04 16:33:58 +02001523
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001525
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 /**
1527 * XSS Clean
1528 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 * @param string
1530 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001531 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001532 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 {
Derek Jones5640a712010-04-23 11:22:40 -05001534 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001535 }
Barry Mienydd671972010-10-04 16:33:58 +02001536
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001538
Derek Allard2067d1a2008-11-13 22:59:24 +00001539 /**
1540 * Convert PHP tags to entities
1541 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 * @param string
1543 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001544 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001545 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 {
Andrey Andreev838a9d62012-12-03 14:37:47 +02001547 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 }
1549
JonoB099c4782012-03-04 14:37:30 +00001550 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001551
1552 /**
1553 * Reset validation vars
1554 *
1555 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001556 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001557 *
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001558 * @return CI_Form_validation
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001559 */
JonoB883f80f2012-03-05 09:51:27 +00001560 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001561 {
JonoB099c4782012-03-04 14:37:30 +00001562 $this->_field_data = array();
1563 $this->_config_rules = array();
1564 $this->_error_array = array();
1565 $this->_error_messages = array();
1566 $this->error_string = '';
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001567 return $this;
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001568 }
1569
Derek Allard2067d1a2008-11-13 22:59:24 +00001570}
Derek Allard2067d1a2008-11-13 22:59:24 +00001571
1572/* End of file Form_validation.php */
Eric Roberts24a13f52012-12-12 07:09:42 -06001573/* Location: ./system/libraries/Form_validation.php */