blob: 31d73303e4ea5c235fc942a3d8d1192ef354ea47 [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 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 $is_array = TRUE;
228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
230 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300232 'field' => $field,
233 'label' => $label,
234 'rules' => $rules,
Ahmedul Haque Abid7945d302014-01-09 16:50:23 +0600235 'errors' => $errors,
Andrey Andreev56454792012-05-17 14:32:19 +0300236 'is_array' => $is_array,
237 'keys' => $indexes,
238 'postdata' => NULL,
239 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000240 );
Greg Aker9f9af602010-11-10 15:41:51 -0600241
242 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
244
245 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 /**
JonoB099c4782012-03-04 14:37:30 +0000248 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200249 *
JonoB099c4782012-03-04 14:37:30 +0000250 * If an array is set through this method, then this array will
251 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200252 *
253 * Note that if you are validating multiple arrays, then the
254 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000255 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200256 *
257 * @param array $data
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200258 * @return CI_Form_validation
JonoB099c4782012-03-04 14:37:30 +0000259 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200260 public function set_data(array $data)
JonoB099c4782012-03-04 14:37:30 +0000261 {
Andrey Andreeva4712f52014-01-06 11:25:46 +0200262 if ( ! empty($data))
JonoB099c4782012-03-04 14:37:30 +0000263 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200264 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000265 }
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200266
267 return $this;
JonoB099c4782012-03-04 14:37:30 +0000268 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200269
JonoB099c4782012-03-04 14:37:30 +0000270 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000271
272 /**
273 * Set Error Message
274 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300275 * Lets users set their own error messages on the fly. Note:
276 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300278 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500280 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100282 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
284 if ( ! is_array($lang))
285 {
286 $lang = array($lang => $val);
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600290 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 /**
296 * Set The Error Delimiter
297 *
298 * Permits a prefix/suffix to be added to each error message
299 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 * @param string
301 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500302 * @return CI_Form_validation
Barry Mienydd671972010-10-04 16:33:58 +0200303 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100304 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
306 $this->_error_prefix = $prefix;
307 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600308 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
310
311 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 /**
314 * Get Error Message
315 *
316 * Gets the error message associated with a particular field
317 *
Andrey Andreeva4712f52014-01-06 11:25:46 +0200318 * @param string $field Field name
319 * @param string $prefix HTML start tag
Andrey Andreev868301a2014-01-06 12:29:50 +0200320 * @param string $suffix HTML end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300321 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200322 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200323 public function error($field, $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200324 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300325 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
327 return '';
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Alex Bilbied261b1e2012-06-02 11:12:16 +0100330 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 $prefix = $this->_error_prefix;
333 }
334
Alex Bilbied261b1e2012-06-02 11:12:16 +0100335 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 $suffix = $this->_error_suffix;
338 }
339
340 return $prefix.$this->_field_data[$field]['error'].$suffix;
341 }
342
343 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100346 * Get Array of Error Messages
347 *
348 * Returns the error messages as an array
349 *
350 * @return array
351 */
352 public function error_array()
353 {
354 return $this->_error_array;
355 }
356
357 // --------------------------------------------------------------------
358
359 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 * Error String
361 *
362 * Returns the error messages as a string, wrapped in the error delimiters
363 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @param string
365 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300366 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200367 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100368 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
vlakoff35672462013-02-15 01:36:04 +0100370 // No errors, validation passes!
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 if (count($this->_error_array) === 0)
372 {
373 return '';
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Alex Bilbied261b1e2012-06-02 11:12:16 +0100376 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 $prefix = $this->_error_prefix;
379 }
380
Alex Bilbied261b1e2012-06-02 11:12:16 +0100381 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 $suffix = $this->_error_suffix;
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 // Generate the error string
387 $str = '';
388 foreach ($this->_error_array as $val)
389 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100390 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 $str .= $prefix.$val.$suffix."\n";
393 }
394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 return $str;
397 }
398
399 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 /**
402 * Run the Validator
403 *
404 * This function does all the work.
405 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400406 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200408 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100409 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500411 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300412 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000413 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 return FALSE;
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // Does the _field_data array containing the validation rules exist?
419 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200420 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500422 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200423 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
425 return FALSE;
426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200428 if (empty($group))
429 {
430 // Is there a validation rule for the particular URI being accessed?
431 $group = trim($this->CI->uri->ruri_string(), '/');
432 isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method;
433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200435 $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules);
Barry Mienydd671972010-10-04 16:33:58 +0200436
Andrey Andreev901573c2012-01-11 01:40:48 +0200437 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200438 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200440 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 return FALSE;
442 }
443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 // Load the language file containing error messages
446 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200447
Andrey Andreev751f2472012-11-03 18:26:27 +0200448 // Cycle through the rules for each field and match the corresponding $validation_data item
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200450 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200451 // Fetch the data from the validation_data array item and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // 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 +0200453 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
JonoB099c4782012-03-04 14:37:30 +0000455 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
Andrey Andreevfff6c2a2012-05-13 22:15:23 +0300457 elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300459 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Andrey Andreev751f2472012-11-03 18:26:27 +0200463 // Execute validation rules
464 // Note: A second foreach (for now) is required in order to avoid false-positives
465 // for rules like 'matches', which correlate to other validation fields.
466 foreach ($this->_field_data as $field => $row)
467 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300468 // Don't try to validate if we have no rules set
469 if (empty($row['rules']))
470 {
471 continue;
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Andrey Andreev4b90a372014-03-10 10:24:24 +0200474 $this->_execute($row, $row['rules'], $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476
477 // Did we end up with any errors?
478 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 if ($total_errors > 0)
480 {
481 $this->_safe_form_data = TRUE;
482 }
483
484 // Now we need to re-set the POST data with the new, processed data
485 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200486
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200487 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489
490 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 /**
493 * Traverse a multidimensional $_POST array index until the data is found
494 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 * @param array
496 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300497 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200499 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100500 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200502 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200504 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 }
Barry Mienydd671972010-10-04 16:33:58 +0200506
Andrey Andreev2d48b4f2012-11-23 17:33:21 +0200507 // NULL must be returned for empty fields
Andrey Andreev44c34632012-11-23 18:46:34 +0200508 return ($array === '') ? NULL : $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 }
510
511 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 /**
514 * Re-populate the _POST array with our finalized and processed data
515 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200516 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200517 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100518 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
520 foreach ($this->_field_data as $field => $row)
521 {
vlakoff1228fe22013-01-14 01:30:09 +0100522 if ($row['postdata'] !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200524 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
526 if (isset($_POST[$row['field']]))
527 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200528 $_POST[$row['field']] = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 }
530 }
531 else
532 {
Derek Jones63eeae32009-02-10 19:08:56 +0000533 // start with a reference
534 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Jones63eeae32009-02-10 19:08:56 +0000536 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200537 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
Derek Jones63eeae32009-02-10 19:08:56 +0000539 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
541 else
542 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 foreach ($row['keys'] as $val)
544 {
Derek Jones63eeae32009-02-10 19:08:56 +0000545 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 }
547 }
Derek Jones63eeae32009-02-10 19:08:56 +0000548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000550 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 $array = array();
552 foreach ($row['postdata'] as $k => $v)
553 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200554 $array[$k] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 }
Derek Jones63eeae32009-02-10 19:08:56 +0000556
557 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 }
559 else
Derek Jones63eeae32009-02-10 19:08:56 +0000560 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200561 $post_ref = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 }
564 }
565 }
566 }
567
568 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 /**
571 * Executes the Validation routines
572 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 * @param array
574 * @param array
575 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300576 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200578 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100579 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
581 // If the $_POST data is an array we will run a recursive call
582 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200583 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 foreach ($postdata as $key => $val)
585 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300586 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 return;
590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 // If the field is blank, but NOT required, no further tests are necessary
593 $callback = FALSE;
Hashem Qolami05370bf2013-07-22 01:52:04 +0430594 if ( ! in_array('required', $rules) && ($postdata === NULL OR $postdata === ''))
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
596 // Before we bail out, does the rule contain a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200597 foreach ($rules as &$rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200599 if (is_string($rule))
600 {
601 if (strncmp($rule, 'callback_', 9) === 0)
602 {
603 $callback = TRUE;
604 $rules = array(1 => $rule);
605 break;
606 }
607 }
608 elseif (is_callable($rule))
609 {
610 $callback = TRUE;
611 $rules = array(1 => $rule);
612 break;
613 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
Andrey Andreev4b90a372014-03-10 10:24:24 +0200615
616 if ( ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
618 return;
619 }
620 }
621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev4b90a372014-03-10 10:24:24 +0200623 if (($postdata === NULL OR $postdata === '') && ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
625 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
626 {
627 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300628 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200629
Ahmedul Haque Abide9b0ccc2014-01-09 15:58:51 +0600630 // Check if a custom message is defined
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600631 if (isset($this->_field_data[$row['field']]['errors'][$type]))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600632 {
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600633 $line = $this->_field_data[$row['field']]['errors'][$type];
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600634 }
635 elseif (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
637 $line = $this->_error_messages[$type];
638 }
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200639 elseif (FALSE === ($line = $this->CI->lang->line('form_validation_'.$type))
640 // DEPRECATED support for non-prefixed keys
641 && FALSE === ($line = $this->CI->lang->line($type, FALSE)))
Andrey Andreev56454792012-05-17 14:32:19 +0300642 {
643 $line = 'The field was not set';
644 }
Barry Mienydd671972010-10-04 16:33:58 +0200645
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600647 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
Derek Allard2067d1a2008-11-13 22:59:24 +0000648
649 // Save the error message
650 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 if ( ! isset($this->_error_array[$row['field']]))
653 {
654 $this->_error_array[$row['field']] = $message;
655 }
656 }
Barry Mienydd671972010-10-04 16:33:58 +0200657
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 return;
659 }
660
661 // --------------------------------------------------------------------
662
663 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300664 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 {
666 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 // We set the $postdata variable with the current data in our master array so that
669 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100670 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
672 // We shouldn't need this safety, but just in case there isn't an array index
673 // associated with this cycle we'll bail out
674 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
675 {
676 continue;
677 }
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
680 $_in_array = TRUE;
681 }
682 else
683 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300684 // If we get an array field, but it's not expected - then it is most likely
685 // somebody messing with the form on the client side, so we'll just consider
686 // it an empty field
687 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
Andrey Andreev4b90a372014-03-10 10:24:24 +0200688 ? NULL
689 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
691
Barry Mienydd671972010-10-04 16:33:58 +0200692 // Is the rule a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200693 $callback = $callable = FALSE;
694 if (is_string($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200696 if (strpos($rule, 'callback_') === 0)
697 {
698 $rule = substr($rule, 9);
699 $callback = TRUE;
700 }
701 }
702 elseif (is_callable($rule))
703 {
704 $callable = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 // Strip the parameter (if exists) from the rule
708 // Rules can contain a parameter: max_length[5]
709 $param = FALSE;
Andrey Andreev4b90a372014-03-10 10:24:24 +0200710 if ( ! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 {
Andrey Andreevef758bd2012-11-15 12:24:52 +0200712 $rule = $match[1];
713 $param = $match[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 }
Barry Mienydd671972010-10-04 16:33:58 +0200715
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 // Call the function that corresponds to the rule
Andrey Andreev4b90a372014-03-10 10:24:24 +0200717 if ($callback OR $callable)
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200719 if ($callback)
Barry Mienydd671972010-10-04 16:33:58 +0200720 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200721 if ( ! method_exists($this->CI, $rule))
722 {
723 log_message('debug', 'Unable to find callback validation rule: '.$rule);
724 $result = FALSE;
725 }
726 else
727 {
728 // Run the function and grab the result
729 $result = $this->CI->$rule($postdata, $param);
730 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200732 else
733 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200734 $result = is_array($rule)
735 ? $rule[0]->{$rule[1]}($postdata, $param)
736 : $rule($postdata, $param);
Andrey Andreev901573c2012-01-11 01:40:48 +0200737 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000738
739 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200740 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300742 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 }
744 else
745 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300746 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 }
Barry Mienydd671972010-10-04 16:33:58 +0200748
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200750 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000752 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 }
754 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300755 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200756 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300757 // If our own wrapper function doesn't exist we see if a native PHP function does.
758 // Users can use any native PHP function call that has one param.
759 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200761 // Native PHP functions issue warnings if you pass them more parameters than they use
Andrey Andreev320d37c2012-04-03 20:21:39 +0300762 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200763
Andrey Andreev78f55772012-04-03 19:59:08 +0300764 if ($_in_array === TRUE)
765 {
766 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 }
patwork02404a12011-04-08 15:45:46 +0200768 else
769 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300770 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200771 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200773 else
774 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300775 log_message('debug', 'Unable to find validation rule: '.$rule);
776 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200777 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300778 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 else
780 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 $result = $this->$rule($postdata, $param);
782
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200783 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300785 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 }
787 else
788 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300789 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 }
791 }
Barry Mienydd671972010-10-04 16:33:58 +0200792
Andrey Andreev901573c2012-01-11 01:40:48 +0200793 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200795 {
Ahmedul Haque Abid7945d302014-01-09 16:50:23 +0600796 // Check if a custom message is defined
Ahmedul Haque Abidd8a37162014-01-09 16:03:43 +0600797 if (isset($this->_field_data[$row['field']]['errors'][$rule]))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600798 {
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600799 $line = $this->_field_data[$row['field']]['errors'][$rule];
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600800 }
801 elseif ( ! isset($this->_error_messages[$rule]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 {
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200803 if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule))
804 // DEPRECATED support for non-prefixed keys
805 && FALSE === ($line = $this->CI->lang->line($rule, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 {
807 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200808 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 }
810 else
811 {
812 $line = $this->_error_messages[$rule];
813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200816 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200817 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 {
Pascal Krietec1895832009-10-13 12:56:43 +0000819 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 }
Barry Mienydd671972010-10-04 16:33:58 +0200821
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600823 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000824
825 // Save the error message
826 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 if ( ! isset($this->_error_array[$row['field']]))
829 {
830 $this->_error_array[$row['field']] = $message;
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 return;
834 }
835 }
836 }
837
838 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 /**
841 * Translate a field name
842 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 * @param string the field name
844 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200845 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100846 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 {
848 // Do we need to translate the field name?
849 // We look for the prefix lang: to determine this
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200850 if (sscanf($fieldname, 'lang:%s', $line) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500852 // Were we able to translate the field name? If not we use $line
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200853 if (FALSE === ($fieldname = $this->CI->lang->line('form_validation_'.$line))
854 // DEPRECATED support for non-prefixed keys
855 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 {
857 return $line;
858 }
859 }
860
861 return $fieldname;
862 }
863
864 // --------------------------------------------------------------------
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200865
Eric Roberts41cc0902012-01-24 00:59:44 -0600866 /**
867 * Build an error message using the field and param.
868 *
869 * @param string The error message line
870 * @param string A field's human name
871 * @param mixed A rule's optional parameter
872 * @return string
873 */
874 protected function _build_error_msg($line, $field = '', $param = '')
875 {
876 // Check for %s in the string for legacy support.
Eric Roberts24a13f52012-12-12 07:09:42 -0600877 if (strpos($line, '%s') !== FALSE)
Eric Roberts41cc0902012-01-24 00:59:44 -0600878 {
879 return sprintf($line, $field, $param);
880 }
Andrew Podner4296a652012-12-17 07:51:15 -0500881
Eric Roberts41cc0902012-01-24 00:59:44 -0600882 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
883 }
884
885 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200886
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 /**
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530888 * Checks if the rule is present within the validator
889 *
890 * Permits you to check if a rule is present within the validator
891 *
892 * @param string the field name
893 * @return bool
894 */
895 public function has_rule($field)
896 {
897 return isset($this->_field_data[$field]);
898 }
899
900 // --------------------------------------------------------------------
901
902 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 * Get the value from a form
904 *
905 * Permits you to repopulate a form field with the value it was submitted
906 * with, or, if that value doesn't exist, with the default
907 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 * @param string the field name
909 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200910 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200911 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100912 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200914 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
916 return $default;
917 }
Barry Mienydd671972010-10-04 16:33:58 +0200918
Phil Sturgeon5c561802011-01-05 16:31:59 +0000919 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600920 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000921 if (is_array($this->_field_data[$field]['postdata']))
922 {
923 return array_shift($this->_field_data[$field]['postdata']);
924 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 return $this->_field_data[$field]['postdata'];
927 }
Barry Mienydd671972010-10-04 16:33:58 +0200928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 /**
932 * Set Select
933 *
934 * Enables pull-down lists to be set to the value the user
935 * selected in the event of an error
936 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 * @param string
938 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400939 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200941 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100942 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200943 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200944 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200946 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 }
Barry Mienydd671972010-10-04 16:33:58 +0200948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300950 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 if (is_array($field))
952 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300953 // Note: in_array('', array(0)) returns TRUE, do not use it
954 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300956 if ($value === $v)
957 {
958 return ' selected="selected"';
959 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300961
962 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100964 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200966 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 return ' selected="selected"';
970 }
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 /**
975 * Set Radio
976 *
977 * Enables radio buttons to be set to the value the user
978 * selected in the event of an error
979 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 * @param string
981 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400982 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200984 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100985 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200987 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200989 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300993 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 if (is_array($field))
995 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300996 // Note: in_array('', array(0)) returns TRUE, do not use it
997 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300999 if ($value === $v)
1000 {
1001 return ' checked="checked"';
1002 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
Andrey Andreeva587a932013-10-23 19:57:46 +03001004
1005 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 }
Alex Bilbied261b1e2012-06-02 11:12:16 +01001007 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001009 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 }
Barry Mienydd671972010-10-04 16:33:58 +02001011
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 return ' checked="checked"';
1013 }
Barry Mienydd671972010-10-04 16:33:58 +02001014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001016
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 /**
1018 * Set Checkbox
1019 *
1020 * Enables checkboxes to be set to the value the user
1021 * selected in the event of an error
1022 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 * @param string
1024 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001025 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001027 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001028 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001030 // Logic is exactly the same as for radio fields
1031 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 }
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001035
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 /**
1037 * Required
1038 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 * @param string
1040 * @return bool
1041 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001042 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001044 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 }
Barry Mienydd671972010-10-04 16:33:58 +02001046
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001050 * Performs a Regular Expression match test.
1051 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001052 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001053 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001054 * @return bool
1055 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001056 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001057 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001058 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001059 }
1060
1061 // --------------------------------------------------------------------
1062
1063 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 * Match one field to another
1065 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001066 * @param string $str string to compare against
1067 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 * @return bool
1069 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001070 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001072 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
1073 ? ($str === $this->_field_data[$field]['postdata'])
1074 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001076
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001077 // --------------------------------------------------------------------
1078
1079 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -03001080 * Differs from another field
1081 *
1082 * @param string
1083 * @param string field
1084 * @return bool
1085 */
1086 public function differs($str, $field)
1087 {
1088 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001090
1091 // --------------------------------------------------------------------
1092
1093 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +02001094 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001095 *
Andrey Andreevd09d6502012-01-03 06:38:33 +02001096 * Check if the input value doesn't already exist
1097 * in the specified database field.
1098 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001099 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001100 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001101 * @return bool
1102 */
1103 public function is_unique($str, $field)
1104 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001105 sscanf($field, '%[^.].%[^.]', $table, $field);
Eric Barnescccde962011-12-04 00:01:17 -05001106 if (isset($this->CI->db))
1107 {
1108 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
1109 return $query->num_rows() === 0;
1110 }
1111 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001112 }
Barry Mienydd671972010-10-04 16:33:58 +02001113
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 /**
1117 * Minimum Length
1118 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001120 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001122 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001123 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001125 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 {
1127 return FALSE;
1128 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001129 else
1130 {
1131 $val = (int) $val;
1132 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001133
Andrey Andreev78f55772012-04-03 19:59:08 +03001134 return (MB_ENABLED === TRUE)
1135 ? ($val <= mb_strlen($str))
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001136 : ($val <= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 }
Barry Mienydd671972010-10-04 16:33:58 +02001138
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001140
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 /**
1142 * Max Length
1143 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001145 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001147 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001148 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001150 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 {
1152 return FALSE;
1153 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001154 else
1155 {
1156 $val = (int) $val;
1157 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001158
Andrey Andreev78f55772012-04-03 19:59:08 +03001159 return (MB_ENABLED === TRUE)
1160 ? ($val >= mb_strlen($str))
1161 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 }
Barry Mienydd671972010-10-04 16:33:58 +02001163
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001165
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 /**
1167 * Exact Length
1168 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001170 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001172 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001173 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001175 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 {
1177 return FALSE;
1178 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001179 else
1180 {
1181 $val = (int) $val;
1182 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001183
Andrey Andreev78f55772012-04-03 19:59:08 +03001184 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001185 ? (mb_strlen($str) === $val)
1186 : (strlen($str) === $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 }
Barry Mienydd671972010-10-04 16:33:58 +02001188
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001190
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 /**
Andrey Andreevdaaca882012-11-26 22:16:12 +02001192 * Valid URL
1193 *
1194 * @param string $str
1195 * @return bool
1196 */
1197 public function valid_url($str)
1198 {
1199 if (empty($str))
1200 {
1201 return FALSE;
1202 }
1203 elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
1204 {
1205 if (empty($matches[2]))
1206 {
1207 return FALSE;
1208 }
1209 elseif ( ! in_array($matches[1], array('http', 'https'), TRUE))
1210 {
1211 return FALSE;
1212 }
1213
1214 $str = $matches[2];
1215 }
1216
1217 $str = 'http://'.$str;
1218
1219 // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the
1220 // underscore to be a valid hostname character instead of a dash.
1221 // Reference: https://bugs.php.net/bug.php?id=51192
1222 if (version_compare(PHP_VERSION, '5.2.13', '==') === 0 OR version_compare(PHP_VERSION, '5.3.2', '==') === 0)
1223 {
1224 sscanf($str, 'http://%[^/]', $host);
1225 $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host));
1226 }
1227
1228 return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 }
1230
1231 // --------------------------------------------------------------------
1232
1233 /**
1234 * Valid Email
1235 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 * @param string
1237 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001238 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001239 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001241 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 }
1243
1244 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001245
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 /**
1247 * Valid Emails
1248 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 * @param string
1250 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001251 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001252 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 {
1254 if (strpos($str, ',') === FALSE)
1255 {
1256 return $this->valid_email(trim($str));
1257 }
Barry Mienydd671972010-10-04 16:33:58 +02001258
Pascal Kriete14287f32011-02-14 13:39:34 -05001259 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001261 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 {
1263 return FALSE;
1264 }
1265 }
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 return TRUE;
1268 }
1269
1270 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001271
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 /**
1273 * Validate IP Address
1274 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001276 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001277 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001279 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001281 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 }
1283
1284 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 /**
1287 * Alpha
1288 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 * @param string
1290 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001291 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001292 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001294 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 }
Barry Mienydd671972010-10-04 16:33:58 +02001296
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001298
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 /**
1300 * Alpha-numeric
1301 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 * @param string
1303 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001304 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001305 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001307 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 }
Barry Mienydd671972010-10-04 16:33:58 +02001309
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 // --------------------------------------------------------------------
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001311
1312 /**
1313 * Alpha-numeric w/ spaces
1314 *
1315 * @param string
1316 * @return bool
1317 */
1318 public function alpha_numeric_spaces($str)
1319 {
Sajan Parikhdf3bfed2013-02-04 12:25:49 -06001320 return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001321 }
1322
1323 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001324
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 /**
1326 * Alpha-numeric with underscores and dashes
1327 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 * @param string
1329 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001330 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001331 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001333 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 }
Barry Mienydd671972010-10-04 16:33:58 +02001335
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001337
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 /**
1339 * Numeric
1340 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 * @param string
1342 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001343 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001344 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001346 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001347
1348 }
1349
1350 // --------------------------------------------------------------------
1351
Barry Mienydd671972010-10-04 16:33:58 +02001352 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 * Integer
1354 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 * @param string
1356 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001357 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001358 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001360 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1361 }
1362
1363 // --------------------------------------------------------------------
1364
1365 /**
1366 * Decimal number
1367 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001368 * @param string
1369 * @return bool
1370 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001371 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001372 {
1373 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1374 }
1375
1376 // --------------------------------------------------------------------
1377
1378 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001379 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001380 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001381 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001382 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001383 * @return bool
1384 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001385 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001386 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001387 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001388 }
1389
1390 // --------------------------------------------------------------------
1391
1392 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001393 * Equal to or Greater than
1394 *
Nick Busey98c347d2012-02-02 11:07:03 -07001395 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001396 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001397 * @return bool
1398 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001399 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001400 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001401 return is_numeric($str) ? ($str >= $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001402 }
1403
1404 // --------------------------------------------------------------------
1405
1406 /**
1407 * Less than
1408 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001409 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001410 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001411 * @return bool
1412 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001413 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001414 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001415 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001416 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001417
1418 // --------------------------------------------------------------------
1419
Barry Mienydd671972010-10-04 16:33:58 +02001420 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001421 * Equal to or Less than
1422 *
Nick Busey98c347d2012-02-02 11:07:03 -07001423 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001424 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001425 * @return bool
1426 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001427 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001428 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001429 return is_numeric($str) ? ($str <= $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 }
1431
1432 // --------------------------------------------------------------------
1433
Barry Mienydd671972010-10-04 16:33:58 +02001434 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001435 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001436 *
Barry Mienydd671972010-10-04 16:33:58 +02001437 * @param string
1438 * @return bool
1439 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001440 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001441 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001442 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001443 }
1444
1445 // --------------------------------------------------------------------
1446
1447 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001448 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001449 *
Barry Mienydd671972010-10-04 16:33:58 +02001450 * @param string
1451 * @return bool
1452 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001453 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001454 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001455 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001456 }
1457
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001459
Derek Allard2067d1a2008-11-13 22:59:24 +00001460 /**
1461 * Valid Base64
1462 *
1463 * Tests a string for characters outside of the Base64 alphabet
1464 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1465 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 * @param string
1467 * @return bool
1468 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001469 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 {
Andrey Andreevcd9797a2013-06-28 14:03:48 +03001471 return (base64_encode(base64_decode($str)) === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001472 }
Barry Mienydd671972010-10-04 16:33:58 +02001473
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001475
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 /**
1477 * Prep data for form
1478 *
1479 * This function allows HTML to be safely shown in a form.
1480 * Special characters are converted.
1481 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 * @param string
1483 * @return string
1484 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001485 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001487 if ($this->_safe_form_data === FALSE OR empty($data))
1488 {
1489 return $data;
1490 }
1491
Derek Allard2067d1a2008-11-13 22:59:24 +00001492 if (is_array($data))
1493 {
1494 foreach ($data as $key => $val)
1495 {
1496 $data[$key] = $this->prep_for_form($val);
1497 }
Barry Mienydd671972010-10-04 16:33:58 +02001498
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 return $data;
1500 }
Barry Mienydd671972010-10-04 16:33:58 +02001501
Andrey Andreev901573c2012-01-11 01:40:48 +02001502 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 }
Barry Mienydd671972010-10-04 16:33:58 +02001504
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001506
Derek Allard2067d1a2008-11-13 22:59:24 +00001507 /**
1508 * Prep URL
1509 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 * @param string
1511 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001512 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001513 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001515 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 {
1517 return '';
1518 }
Barry Mienydd671972010-10-04 16:33:58 +02001519
Andrey Andreev901573c2012-01-11 01:40:48 +02001520 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001522 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 }
Barry Mienydd671972010-10-04 16:33:58 +02001524
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 return $str;
1526 }
Barry Mienydd671972010-10-04 16:33:58 +02001527
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001529
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 /**
1531 * Strip Image Tags
1532 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 * @param string
1534 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001535 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001536 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001538 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001539 }
Barry Mienydd671972010-10-04 16:33:58 +02001540
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001542
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 /**
1544 * XSS Clean
1545 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 * @param string
1547 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001548 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001549 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001550 {
Derek Jones5640a712010-04-23 11:22:40 -05001551 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001552 }
Barry Mienydd671972010-10-04 16:33:58 +02001553
Derek Allard2067d1a2008-11-13 22:59:24 +00001554 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001555
Derek Allard2067d1a2008-11-13 22:59:24 +00001556 /**
1557 * Convert PHP tags to entities
1558 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 * @param string
1560 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001561 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001562 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001563 {
Andrey Andreev838a9d62012-12-03 14:37:47 +02001564 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 }
1566
JonoB099c4782012-03-04 14:37:30 +00001567 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001568
1569 /**
1570 * Reset validation vars
1571 *
1572 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001573 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001574 *
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001575 * @return CI_Form_validation
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001576 */
JonoB883f80f2012-03-05 09:51:27 +00001577 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001578 {
JonoB099c4782012-03-04 14:37:30 +00001579 $this->_field_data = array();
1580 $this->_config_rules = array();
1581 $this->_error_array = array();
1582 $this->_error_messages = array();
1583 $this->error_string = '';
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001584 return $this;
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001585 }
1586
Derek Allard2067d1a2008-11-13 22:59:24 +00001587}
Derek Allard2067d1a2008-11-13 22:59:24 +00001588
1589/* End of file Form_validation.php */
Eric Roberts24a13f52012-12-12 07:09:42 -06001590/* Location: ./system/libraries/Form_validation.php */