blob: 0a80888d2b7e229bfefd07d9b93a1e95d02e51d3 [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 Andreev8dc532d2011-12-24 17:57:54 +0200211 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200212 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev4b90a372014-03-10 10:24:24 +0200213 if (($is_array = (bool) preg_match_all('/\[(.*?)\]/', $field, $matches)) === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200214 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200215 $indexes = array();
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200216 sscanf($field, '%[^[][', $indexes[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000217
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200218 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100220 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200222 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 $is_array = TRUE;
227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
229 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300231 'field' => $field,
232 'label' => $label,
233 'rules' => $rules,
Ahmedul Haque Abid7945d302014-01-09 16:50:23 +0600234 'errors' => $errors,
Andrey Andreev56454792012-05-17 14:32:19 +0300235 'is_array' => $is_array,
236 'keys' => $indexes,
237 'postdata' => NULL,
238 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000239 );
Greg Aker9f9af602010-11-10 15:41:51 -0600240
241 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243
244 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 /**
JonoB099c4782012-03-04 14:37:30 +0000247 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200248 *
JonoB099c4782012-03-04 14:37:30 +0000249 * If an array is set through this method, then this array will
250 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200251 *
252 * Note that if you are validating multiple arrays, then the
253 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000254 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200255 *
256 * @param array $data
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200257 * @return CI_Form_validation
JonoB099c4782012-03-04 14:37:30 +0000258 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200259 public function set_data(array $data)
JonoB099c4782012-03-04 14:37:30 +0000260 {
Andrey Andreeva4712f52014-01-06 11:25:46 +0200261 if ( ! empty($data))
JonoB099c4782012-03-04 14:37:30 +0000262 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200263 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000264 }
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200265
266 return $this;
JonoB099c4782012-03-04 14:37:30 +0000267 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200268
JonoB099c4782012-03-04 14:37:30 +0000269 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000270
271 /**
272 * Set Error Message
273 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300274 * Lets users set their own error messages on the fly. Note:
275 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300277 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500279 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100281 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
283 if ( ! is_array($lang))
284 {
285 $lang = array($lang => $val);
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600289 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 /**
295 * Set The Error Delimiter
296 *
297 * Permits a prefix/suffix to be added to each error message
298 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 * @param string
300 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500301 * @return CI_Form_validation
Barry Mienydd671972010-10-04 16:33:58 +0200302 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100303 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
305 $this->_error_prefix = $prefix;
306 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600307 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309
310 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 /**
313 * Get Error Message
314 *
315 * Gets the error message associated with a particular field
316 *
Andrey Andreeva4712f52014-01-06 11:25:46 +0200317 * @param string $field Field name
318 * @param string $prefix HTML start tag
Andrey Andreev868301a2014-01-06 12:29:50 +0200319 * @param string $suffix HTML end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300320 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200321 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200322 public function error($field, $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200323 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300324 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
326 return '';
327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Alex Bilbied261b1e2012-06-02 11:12:16 +0100329 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
331 $prefix = $this->_error_prefix;
332 }
333
Alex Bilbied261b1e2012-06-02 11:12:16 +0100334 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 $suffix = $this->_error_suffix;
337 }
338
339 return $prefix.$this->_field_data[$field]['error'].$suffix;
340 }
341
342 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100345 * Get Array of Error Messages
346 *
347 * Returns the error messages as an array
348 *
349 * @return array
350 */
351 public function error_array()
352 {
353 return $this->_error_array;
354 }
355
356 // --------------------------------------------------------------------
357
358 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 * Error String
360 *
361 * Returns the error messages as a string, wrapped in the error delimiters
362 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @param string
364 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300365 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200366 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100367 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
vlakoff35672462013-02-15 01:36:04 +0100369 // No errors, validation passes!
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 if (count($this->_error_array) === 0)
371 {
372 return '';
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Alex Bilbied261b1e2012-06-02 11:12:16 +0100375 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 $prefix = $this->_error_prefix;
378 }
379
Alex Bilbied261b1e2012-06-02 11:12:16 +0100380 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
382 $suffix = $this->_error_suffix;
383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // Generate the error string
386 $str = '';
387 foreach ($this->_error_array as $val)
388 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100389 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
391 $str .= $prefix.$val.$suffix."\n";
392 }
393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 return $str;
396 }
397
398 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 /**
401 * Run the Validator
402 *
403 * This function does all the work.
404 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400405 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200407 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100408 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500410 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300411 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000412 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 return FALSE;
415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 // Does the _field_data array containing the validation rules exist?
418 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200419 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500421 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200422 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
424 return FALSE;
425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200427 if (empty($group))
428 {
429 // Is there a validation rule for the particular URI being accessed?
430 $group = trim($this->CI->uri->ruri_string(), '/');
431 isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method;
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200434 $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules);
Barry Mienydd671972010-10-04 16:33:58 +0200435
Andrey Andreev901573c2012-01-11 01:40:48 +0200436 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200437 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200439 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 return FALSE;
441 }
442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // Load the language file containing error messages
445 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200446
Andrey Andreev751f2472012-11-03 18:26:27 +0200447 // Cycle through the rules for each field and match the corresponding $validation_data item
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200449 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200450 // Fetch the data from the validation_data array item and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 // 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 +0200452 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
JonoB099c4782012-03-04 14:37:30 +0000454 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
Andrey Andreevfff6c2a2012-05-13 22:15:23 +0300456 elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300458 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Andrey Andreev751f2472012-11-03 18:26:27 +0200462 // Execute validation rules
463 // Note: A second foreach (for now) is required in order to avoid false-positives
464 // for rules like 'matches', which correlate to other validation fields.
465 foreach ($this->_field_data as $field => $row)
466 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300467 // Don't try to validate if we have no rules set
468 if (empty($row['rules']))
469 {
470 continue;
471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Andrey Andreev4b90a372014-03-10 10:24:24 +0200473 $this->_execute($row, $row['rules'], $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
475
476 // Did we end up with any errors?
477 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 if ($total_errors > 0)
479 {
480 $this->_safe_form_data = TRUE;
481 }
482
483 // Now we need to re-set the POST data with the new, processed data
484 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200485
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200486 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488
489 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 /**
492 * Traverse a multidimensional $_POST array index until the data is found
493 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @param array
495 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300496 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200498 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100499 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200501 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200503 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Andrey Andreev2d48b4f2012-11-23 17:33:21 +0200506 // NULL must be returned for empty fields
Andrey Andreev44c34632012-11-23 18:46:34 +0200507 return ($array === '') ? NULL : $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
509
510 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 /**
513 * Re-populate the _POST array with our finalized and processed data
514 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200515 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200516 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100517 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
519 foreach ($this->_field_data as $field => $row)
520 {
vlakoff1228fe22013-01-14 01:30:09 +0100521 if ($row['postdata'] !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200523 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 if (isset($_POST[$row['field']]))
526 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200527 $_POST[$row['field']] = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529 }
530 else
531 {
Derek Jones63eeae32009-02-10 19:08:56 +0000532 // start with a reference
533 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Jones63eeae32009-02-10 19:08:56 +0000535 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200536 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 {
Derek Jones63eeae32009-02-10 19:08:56 +0000538 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 }
540 else
541 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 foreach ($row['keys'] as $val)
543 {
Derek Jones63eeae32009-02-10 19:08:56 +0000544 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
546 }
Derek Jones63eeae32009-02-10 19:08:56 +0000547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000549 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 $array = array();
551 foreach ($row['postdata'] as $k => $v)
552 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200553 $array[$k] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
Derek Jones63eeae32009-02-10 19:08:56 +0000555
556 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
558 else
Derek Jones63eeae32009-02-10 19:08:56 +0000559 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200560 $post_ref = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 }
563 }
564 }
565 }
566
567 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 /**
570 * Executes the Validation routines
571 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 * @param array
573 * @param array
574 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300575 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200577 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100578 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
580 // If the $_POST data is an array we will run a recursive call
581 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200582 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 foreach ($postdata as $key => $val)
584 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300585 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 }
Barry Mienydd671972010-10-04 16:33:58 +0200587
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 return;
589 }
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 // If the field is blank, but NOT required, no further tests are necessary
592 $callback = FALSE;
Hashem Qolami05370bf2013-07-22 01:52:04 +0430593 if ( ! in_array('required', $rules) && ($postdata === NULL OR $postdata === ''))
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 // Before we bail out, does the rule contain a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200596 foreach ($rules as &$rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200598 if (is_string($rule))
599 {
600 if (strncmp($rule, 'callback_', 9) === 0)
601 {
602 $callback = TRUE;
603 $rules = array(1 => $rule);
604 break;
605 }
606 }
607 elseif (is_callable($rule))
608 {
609 $callback = TRUE;
610 $rules = array(1 => $rule);
611 break;
612 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 }
Andrey Andreev4b90a372014-03-10 10:24:24 +0200614
615 if ( ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
617 return;
618 }
619 }
620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev4b90a372014-03-10 10:24:24 +0200622 if (($postdata === NULL OR $postdata === '') && ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
624 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
625 {
626 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300627 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200628
Ahmedul Haque Abide9b0ccc2014-01-09 15:58:51 +0600629 // Check if a custom message is defined
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600630 if (isset($this->_field_data[$row['field']]['errors'][$type]))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600631 {
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600632 $line = $this->_field_data[$row['field']]['errors'][$type];
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600633 }
634 elseif (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 $line = $this->_error_messages[$type];
637 }
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200638 elseif (FALSE === ($line = $this->CI->lang->line('form_validation_'.$type))
639 // DEPRECATED support for non-prefixed keys
640 && FALSE === ($line = $this->CI->lang->line($type, FALSE)))
Andrey Andreev56454792012-05-17 14:32:19 +0300641 {
642 $line = 'The field was not set';
643 }
Barry Mienydd671972010-10-04 16:33:58 +0200644
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600646 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
Derek Allard2067d1a2008-11-13 22:59:24 +0000647
648 // Save the error message
649 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 if ( ! isset($this->_error_array[$row['field']]))
652 {
653 $this->_error_array[$row['field']] = $message;
654 }
655 }
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 return;
658 }
659
660 // --------------------------------------------------------------------
661
662 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300663 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 {
665 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // We set the $postdata variable with the current data in our master array so that
668 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100669 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
671 // We shouldn't need this safety, but just in case there isn't an array index
672 // associated with this cycle we'll bail out
673 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
674 {
675 continue;
676 }
Barry Mienydd671972010-10-04 16:33:58 +0200677
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
679 $_in_array = TRUE;
680 }
681 else
682 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300683 // If we get an array field, but it's not expected - then it is most likely
684 // somebody messing with the form on the client side, so we'll just consider
685 // it an empty field
686 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
Andrey Andreev4b90a372014-03-10 10:24:24 +0200687 ? NULL
688 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
690
Barry Mienydd671972010-10-04 16:33:58 +0200691 // Is the rule a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200692 $callback = $callable = FALSE;
693 if (is_string($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200695 if (strpos($rule, 'callback_') === 0)
696 {
697 $rule = substr($rule, 9);
698 $callback = TRUE;
699 }
700 }
701 elseif (is_callable($rule))
702 {
703 $callable = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 // Strip the parameter (if exists) from the rule
707 // Rules can contain a parameter: max_length[5]
708 $param = FALSE;
Andrey Andreev4b90a372014-03-10 10:24:24 +0200709 if ( ! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
Andrey Andreevef758bd2012-11-15 12:24:52 +0200711 $rule = $match[1];
712 $param = $match[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 }
Barry Mienydd671972010-10-04 16:33:58 +0200714
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 // Call the function that corresponds to the rule
Andrey Andreev4b90a372014-03-10 10:24:24 +0200716 if ($callback OR $callable)
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200718 if ($callback)
Barry Mienydd671972010-10-04 16:33:58 +0200719 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200720 if ( ! method_exists($this->CI, $rule))
721 {
722 log_message('debug', 'Unable to find callback validation rule: '.$rule);
723 $result = FALSE;
724 }
725 else
726 {
727 // Run the function and grab the result
728 $result = $this->CI->$rule($postdata, $param);
729 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200731 else
732 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200733 $result = is_array($rule)
734 ? $rule[0]->{$rule[1]}($postdata, $param)
735 : $rule($postdata, $param);
Andrey Andreev901573c2012-01-11 01:40:48 +0200736 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000737
738 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200739 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300741 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
743 else
744 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300745 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 }
Barry Mienydd671972010-10-04 16:33:58 +0200747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200749 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000751 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 }
753 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300754 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200755 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300756 // If our own wrapper function doesn't exist we see if a native PHP function does.
757 // Users can use any native PHP function call that has one param.
758 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200760 // Native PHP functions issue warnings if you pass them more parameters than they use
Andrey Andreev320d37c2012-04-03 20:21:39 +0300761 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200762
Andrey Andreev78f55772012-04-03 19:59:08 +0300763 if ($_in_array === TRUE)
764 {
765 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 }
patwork02404a12011-04-08 15:45:46 +0200767 else
768 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300769 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200770 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200772 else
773 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300774 log_message('debug', 'Unable to find validation rule: '.$rule);
775 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200776 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300777 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 else
779 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 $result = $this->$rule($postdata, $param);
781
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200782 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300784 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 }
786 else
787 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300788 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 }
790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
Andrey Andreev901573c2012-01-11 01:40:48 +0200792 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200794 {
Ahmedul Haque Abid7945d302014-01-09 16:50:23 +0600795 // Check if a custom message is defined
Ahmedul Haque Abidd8a37162014-01-09 16:03:43 +0600796 if (isset($this->_field_data[$row['field']]['errors'][$rule]))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600797 {
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600798 $line = $this->_field_data[$row['field']]['errors'][$rule];
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600799 }
800 elseif ( ! isset($this->_error_messages[$rule]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200802 if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule))
803 // DEPRECATED support for non-prefixed keys
804 && FALSE === ($line = $this->CI->lang->line($rule, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 {
806 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200807 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 }
809 else
810 {
811 $line = $this->_error_messages[$rule];
812 }
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200815 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200816 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
Pascal Krietec1895832009-10-13 12:56:43 +0000818 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 }
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600822 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000823
824 // Save the error message
825 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 if ( ! isset($this->_error_array[$row['field']]))
828 {
829 $this->_error_array[$row['field']] = $message;
830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 return;
833 }
834 }
835 }
836
837 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 /**
840 * Translate a field name
841 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 * @param string the field name
843 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200844 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100845 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 {
847 // Do we need to translate the field name?
848 // We look for the prefix lang: to determine this
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200849 if (sscanf($fieldname, 'lang:%s', $line) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500851 // Were we able to translate the field name? If not we use $line
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200852 if (FALSE === ($fieldname = $this->CI->lang->line('form_validation_'.$line))
853 // DEPRECATED support for non-prefixed keys
854 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 {
856 return $line;
857 }
858 }
859
860 return $fieldname;
861 }
862
863 // --------------------------------------------------------------------
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200864
Eric Roberts41cc0902012-01-24 00:59:44 -0600865 /**
866 * Build an error message using the field and param.
867 *
868 * @param string The error message line
869 * @param string A field's human name
870 * @param mixed A rule's optional parameter
871 * @return string
872 */
873 protected function _build_error_msg($line, $field = '', $param = '')
874 {
875 // Check for %s in the string for legacy support.
Eric Roberts24a13f52012-12-12 07:09:42 -0600876 if (strpos($line, '%s') !== FALSE)
Eric Roberts41cc0902012-01-24 00:59:44 -0600877 {
878 return sprintf($line, $field, $param);
879 }
Andrew Podner4296a652012-12-17 07:51:15 -0500880
Eric Roberts41cc0902012-01-24 00:59:44 -0600881 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
882 }
883
884 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 /**
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530887 * Checks if the rule is present within the validator
888 *
889 * Permits you to check if a rule is present within the validator
890 *
891 * @param string the field name
892 * @return bool
893 */
894 public function has_rule($field)
895 {
896 return isset($this->_field_data[$field]);
897 }
898
899 // --------------------------------------------------------------------
900
901 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 * Get the value from a form
903 *
904 * Permits you to repopulate a form field with the value it was submitted
905 * with, or, if that value doesn't exist, with the default
906 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 * @param string the field name
908 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200909 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200910 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100911 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200913 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 {
915 return $default;
916 }
Barry Mienydd671972010-10-04 16:33:58 +0200917
Phil Sturgeon5c561802011-01-05 16:31:59 +0000918 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600919 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000920 if (is_array($this->_field_data[$field]['postdata']))
921 {
922 return array_shift($this->_field_data[$field]['postdata']);
923 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 return $this->_field_data[$field]['postdata'];
926 }
Barry Mienydd671972010-10-04 16:33:58 +0200927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 /**
931 * Set Select
932 *
933 * Enables pull-down lists to be set to the value the user
934 * selected in the event of an error
935 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 * @param string
937 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400938 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200940 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100941 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200942 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200943 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200945 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300949 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 if (is_array($field))
951 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300952 // Note: in_array('', array(0)) returns TRUE, do not use it
953 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300955 if ($value === $v)
956 {
957 return ' selected="selected"';
958 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300960
961 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100963 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200965 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 return ' selected="selected"';
969 }
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 /**
974 * Set Radio
975 *
976 * Enables radio buttons to be set to the value the user
977 * selected in the event of an error
978 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 * @param string
980 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400981 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200983 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100984 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200986 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200988 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 }
Barry Mienydd671972010-10-04 16:33:58 +0200990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300992 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 if (is_array($field))
994 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300995 // Note: in_array('', array(0)) returns TRUE, do not use it
996 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300998 if ($value === $v)
999 {
1000 return ' checked="checked"';
1001 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 }
Andrey Andreeva587a932013-10-23 19:57:46 +03001003
1004 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 }
Alex Bilbied261b1e2012-06-02 11:12:16 +01001006 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001008 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 return ' checked="checked"';
1012 }
Barry Mienydd671972010-10-04 16:33:58 +02001013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001015
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 /**
1017 * Set Checkbox
1018 *
1019 * Enables checkboxes to be set to the value the user
1020 * selected in the event of an error
1021 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 * @param string
1023 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001024 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001026 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001027 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001029 // Logic is exactly the same as for radio fields
1030 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 }
Barry Mienydd671972010-10-04 16:33:58 +02001032
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001034
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 /**
1036 * Required
1037 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 * @param string
1039 * @return bool
1040 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001041 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001043 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 }
Barry Mienydd671972010-10-04 16:33:58 +02001045
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001049 * Performs a Regular Expression match test.
1050 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001051 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001052 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001053 * @return bool
1054 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001055 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001056 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001057 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001058 }
1059
1060 // --------------------------------------------------------------------
1061
1062 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 * Match one field to another
1064 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001065 * @param string $str string to compare against
1066 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 * @return bool
1068 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001069 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001071 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
1072 ? ($str === $this->_field_data[$field]['postdata'])
1073 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001075
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001076 // --------------------------------------------------------------------
1077
1078 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -03001079 * Differs from another field
1080 *
1081 * @param string
1082 * @param string field
1083 * @return bool
1084 */
1085 public function differs($str, $field)
1086 {
1087 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001089
1090 // --------------------------------------------------------------------
1091
1092 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +02001093 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001094 *
Andrey Andreevd09d6502012-01-03 06:38:33 +02001095 * Check if the input value doesn't already exist
1096 * in the specified database field.
1097 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001098 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001099 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001100 * @return bool
1101 */
1102 public function is_unique($str, $field)
1103 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001104 sscanf($field, '%[^.].%[^.]', $table, $field);
Eric Barnescccde962011-12-04 00:01:17 -05001105 if (isset($this->CI->db))
1106 {
1107 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
1108 return $query->num_rows() === 0;
1109 }
1110 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001111 }
Barry Mienydd671972010-10-04 16:33:58 +02001112
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 /**
1116 * Minimum Length
1117 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001119 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001121 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001122 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001124 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 {
1126 return FALSE;
1127 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001128 else
1129 {
1130 $val = (int) $val;
1131 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001132
Andrey Andreev78f55772012-04-03 19:59:08 +03001133 return (MB_ENABLED === TRUE)
1134 ? ($val <= mb_strlen($str))
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001135 : ($val <= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 }
Barry Mienydd671972010-10-04 16:33:58 +02001137
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001139
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 /**
1141 * Max Length
1142 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001144 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001146 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001147 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001149 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 {
1151 return FALSE;
1152 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001153 else
1154 {
1155 $val = (int) $val;
1156 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001157
Andrey Andreev78f55772012-04-03 19:59:08 +03001158 return (MB_ENABLED === TRUE)
1159 ? ($val >= mb_strlen($str))
1160 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 }
Barry Mienydd671972010-10-04 16:33:58 +02001162
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001164
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 /**
1166 * Exact Length
1167 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001169 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001171 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001172 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001174 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 {
1176 return FALSE;
1177 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001178 else
1179 {
1180 $val = (int) $val;
1181 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001182
Andrey Andreev78f55772012-04-03 19:59:08 +03001183 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001184 ? (mb_strlen($str) === $val)
1185 : (strlen($str) === $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 }
Barry Mienydd671972010-10-04 16:33:58 +02001187
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001189
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 /**
Andrey Andreevdaaca882012-11-26 22:16:12 +02001191 * Valid URL
1192 *
1193 * @param string $str
1194 * @return bool
1195 */
1196 public function valid_url($str)
1197 {
1198 if (empty($str))
1199 {
1200 return FALSE;
1201 }
1202 elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
1203 {
1204 if (empty($matches[2]))
1205 {
1206 return FALSE;
1207 }
1208 elseif ( ! in_array($matches[1], array('http', 'https'), TRUE))
1209 {
1210 return FALSE;
1211 }
1212
1213 $str = $matches[2];
1214 }
1215
1216 $str = 'http://'.$str;
1217
1218 // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the
1219 // underscore to be a valid hostname character instead of a dash.
1220 // Reference: https://bugs.php.net/bug.php?id=51192
1221 if (version_compare(PHP_VERSION, '5.2.13', '==') === 0 OR version_compare(PHP_VERSION, '5.3.2', '==') === 0)
1222 {
1223 sscanf($str, 'http://%[^/]', $host);
1224 $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host));
1225 }
1226
1227 return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 }
1229
1230 // --------------------------------------------------------------------
1231
1232 /**
1233 * Valid Email
1234 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 * @param string
1236 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001237 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001238 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001240 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 }
1242
1243 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 /**
1246 * Valid Emails
1247 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 * @param string
1249 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001250 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001251 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 {
1253 if (strpos($str, ',') === FALSE)
1254 {
1255 return $this->valid_email(trim($str));
1256 }
Barry Mienydd671972010-10-04 16:33:58 +02001257
Pascal Kriete14287f32011-02-14 13:39:34 -05001258 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001260 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 {
1262 return FALSE;
1263 }
1264 }
Barry Mienydd671972010-10-04 16:33:58 +02001265
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 return TRUE;
1267 }
1268
1269 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001270
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 /**
1272 * Validate IP Address
1273 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001275 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001276 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001278 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001280 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 }
1282
1283 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001284
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 /**
1286 * Alpha
1287 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 * @param string
1289 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001290 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001291 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001293 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 }
Barry Mienydd671972010-10-04 16:33:58 +02001295
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001297
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 /**
1299 * Alpha-numeric
1300 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 * @param string
1302 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001303 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001304 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001306 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
Barry Mienydd671972010-10-04 16:33:58 +02001308
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 // --------------------------------------------------------------------
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001310
1311 /**
1312 * Alpha-numeric w/ spaces
1313 *
1314 * @param string
1315 * @return bool
1316 */
1317 public function alpha_numeric_spaces($str)
1318 {
Sajan Parikhdf3bfed2013-02-04 12:25:49 -06001319 return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001320 }
1321
1322 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001323
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 /**
1325 * Alpha-numeric with underscores and dashes
1326 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 * @param string
1328 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001329 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001330 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001332 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 }
Barry Mienydd671972010-10-04 16:33:58 +02001334
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001336
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 /**
1338 * Numeric
1339 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 * @param string
1341 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001342 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001343 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001345 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001346
1347 }
1348
1349 // --------------------------------------------------------------------
1350
Barry Mienydd671972010-10-04 16:33:58 +02001351 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 * Integer
1353 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 * @param string
1355 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001356 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001357 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001359 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1360 }
1361
1362 // --------------------------------------------------------------------
1363
1364 /**
1365 * Decimal number
1366 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001367 * @param string
1368 * @return bool
1369 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001370 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001371 {
1372 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1373 }
1374
1375 // --------------------------------------------------------------------
1376
1377 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001378 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001379 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001380 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001381 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001382 * @return bool
1383 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001384 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001385 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001386 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001387 }
1388
1389 // --------------------------------------------------------------------
1390
1391 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001392 * Equal to or Greater than
1393 *
Nick Busey98c347d2012-02-02 11:07:03 -07001394 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001395 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001396 * @return bool
1397 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001398 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001399 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001400 return is_numeric($str) ? ($str >= $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001401 }
1402
1403 // --------------------------------------------------------------------
1404
1405 /**
1406 * Less than
1407 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001408 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001409 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001410 * @return bool
1411 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001412 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001413 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001414 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001416
1417 // --------------------------------------------------------------------
1418
Barry Mienydd671972010-10-04 16:33:58 +02001419 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001420 * Equal to or Less than
1421 *
Nick Busey98c347d2012-02-02 11:07:03 -07001422 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001423 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001424 * @return bool
1425 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001426 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001427 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001428 return is_numeric($str) ? ($str <= $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 }
1430
1431 // --------------------------------------------------------------------
1432
Barry Mienydd671972010-10-04 16:33:58 +02001433 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001434 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001435 *
Barry Mienydd671972010-10-04 16:33:58 +02001436 * @param string
1437 * @return bool
1438 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001439 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001440 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001441 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001442 }
1443
1444 // --------------------------------------------------------------------
1445
1446 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001447 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001448 *
Barry Mienydd671972010-10-04 16:33:58 +02001449 * @param string
1450 * @return bool
1451 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001452 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001453 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001454 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001455 }
1456
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 * Valid Base64
1461 *
1462 * Tests a string for characters outside of the Base64 alphabet
1463 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1464 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 * @param string
1466 * @return bool
1467 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001468 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 {
Andrey Andreevcd9797a2013-06-28 14:03:48 +03001470 return (base64_encode(base64_decode($str)) === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 }
Barry Mienydd671972010-10-04 16:33:58 +02001472
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001474
Derek Allard2067d1a2008-11-13 22:59:24 +00001475 /**
1476 * Prep data for form
1477 *
1478 * This function allows HTML to be safely shown in a form.
1479 * Special characters are converted.
1480 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001481 * @param string
1482 * @return string
1483 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001484 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001485 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001486 if ($this->_safe_form_data === FALSE OR empty($data))
1487 {
1488 return $data;
1489 }
1490
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 if (is_array($data))
1492 {
1493 foreach ($data as $key => $val)
1494 {
1495 $data[$key] = $this->prep_for_form($val);
1496 }
Barry Mienydd671972010-10-04 16:33:58 +02001497
Derek Allard2067d1a2008-11-13 22:59:24 +00001498 return $data;
1499 }
Barry Mienydd671972010-10-04 16:33:58 +02001500
Andrey Andreev901573c2012-01-11 01:40:48 +02001501 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 }
Barry Mienydd671972010-10-04 16:33:58 +02001503
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001505
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 /**
1507 * Prep URL
1508 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001509 * @param string
1510 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001511 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001512 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001514 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 {
1516 return '';
1517 }
Barry Mienydd671972010-10-04 16:33:58 +02001518
Andrey Andreev901573c2012-01-11 01:40:48 +02001519 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001521 return 'http://'.$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 return $str;
1525 }
Barry Mienydd671972010-10-04 16:33:58 +02001526
Derek Allard2067d1a2008-11-13 22:59:24 +00001527 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001528
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 /**
1530 * Strip Image Tags
1531 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001532 * @param string
1533 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001534 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001535 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001537 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 }
Barry Mienydd671972010-10-04 16:33:58 +02001539
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001541
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 /**
1543 * XSS Clean
1544 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001545 * @param string
1546 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001547 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001548 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001549 {
Derek Jones5640a712010-04-23 11:22:40 -05001550 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 }
Barry Mienydd671972010-10-04 16:33:58 +02001552
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001554
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 /**
1556 * Convert PHP tags to entities
1557 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001558 * @param string
1559 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001560 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001561 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 {
Andrey Andreev838a9d62012-12-03 14:37:47 +02001563 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001564 }
1565
JonoB099c4782012-03-04 14:37:30 +00001566 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001567
1568 /**
1569 * Reset validation vars
1570 *
1571 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001572 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001573 *
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001574 * @return CI_Form_validation
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001575 */
JonoB883f80f2012-03-05 09:51:27 +00001576 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001577 {
JonoB099c4782012-03-04 14:37:30 +00001578 $this->_field_data = array();
1579 $this->_config_rules = array();
1580 $this->_error_array = array();
1581 $this->_error_messages = array();
1582 $this->error_string = '';
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001583 return $this;
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001584 }
1585
Derek Allard2067d1a2008-11-13 22:59:24 +00001586}
Derek Allard2067d1a2008-11-13 22:59:24 +00001587
1588/* End of file Form_validation.php */
Eric Roberts24a13f52012-12-12 07:09:42 -06001589/* Location: ./system/libraries/Form_validation.php */