blob: 36a8148df707ca27b82a5d0b9fdadcc184f39765 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Eric Barnescccde962011-12-04 00:01:17 -05008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Eric Barnescccde962011-12-04 00:01:17 -050010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Form Validation Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
48 */
49class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020050
Timothy Warren0688ac92012-04-20 10:25:04 -040051 /**
52 * Reference to the CodeIgniter instance
53 *
54 * @var object
55 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010056 protected $CI;
Derek Allard2067d1a2008-11-13 22:59:24 +000057
Timothy Warren0688ac92012-04-20 10:25:04 -040058 /**
59 * Validation data for the current form submission
60 *
61 * @var array
62 */
Andrey Andreev56454792012-05-17 14:32:19 +030063 protected $_field_data = array();
64
Timothy Warren0688ac92012-04-20 10:25:04 -040065 /**
66 * Validation rules for the current form
67 *
68 * @var array
69 */
Andrey Andreev56454792012-05-17 14:32:19 +030070 protected $_config_rules = array();
71
Timothy Warren0688ac92012-04-20 10:25:04 -040072 /**
73 * Array of validation errors
74 *
75 * @var array
76 */
Andrey Andreev78f55772012-04-03 19:59:08 +030077 protected $_error_array = array();
Andrey Andreev56454792012-05-17 14:32:19 +030078
Timothy Warren0688ac92012-04-20 10:25:04 -040079 /**
80 * Array of custom error messages
81 *
82 * @var array
83 */
Andrey Andreev78f55772012-04-03 19:59:08 +030084 protected $_error_messages = array();
Andrey Andreev56454792012-05-17 14:32:19 +030085
Timothy Warren0688ac92012-04-20 10:25:04 -040086 /**
87 * Start tag for error wrapping
88 *
89 * @var string
90 */
Andrey Andreev78f55772012-04-03 19:59:08 +030091 protected $_error_prefix = '<p>';
Andrey Andreev56454792012-05-17 14:32:19 +030092
Timothy Warren0688ac92012-04-20 10:25:04 -040093 /**
94 * End tag for error wrapping
Andrey Andreev56454792012-05-17 14:32:19 +030095 *
Timothy Warren0688ac92012-04-20 10:25:04 -040096 * @var string
97 */
Andrey Andreev78f55772012-04-03 19:59:08 +030098 protected $_error_suffix = '</p>';
Andrey Andreev56454792012-05-17 14:32:19 +030099
Timothy Warren0688ac92012-04-20 10:25:04 -0400100 /**
101 * Custom error message
102 *
103 * @var string
104 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300105 protected $error_string = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300106
Timothy Warren0688ac92012-04-20 10:25:04 -0400107 /**
108 * Whether the form data has been validated as safe
109 *
110 * @var bool
111 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300112 protected $_safe_form_data = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300113
Timothy Warren0688ac92012-04-20 10:25:04 -0400114 /**
115 * Custom data to validate
116 *
117 * @var array
118 */
Andrey Andreevcff35802012-11-26 15:49:52 +0200119 public $validation_data = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000120
Timothy Warren0688ac92012-04-20 10:25:04 -0400121 /**
122 * Initialize Form_Validation class
123 *
Andrey Andreev56454792012-05-17 14:32:19 +0300124 * @param array $rules
125 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -0400126 */
Greg Akera9263282010-11-10 15:26:43 -0600127 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +0200128 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200130
Mike Funk326a5e72012-02-24 10:06:28 -0500131 // applies delimiters set in config file.
Mike Funk7f42d062012-03-08 09:00:57 -0500132 if (isset($rules['error_prefix']))
133 {
134 $this->_error_prefix = $rules['error_prefix'];
135 unset($rules['error_prefix']);
136 }
137 if (isset($rules['error_suffix']))
138 {
139 $this->_error_suffix = $rules['error_suffix'];
140 unset($rules['error_suffix']);
141 }
Andrey Andreev31cf46e2012-03-20 15:48:00 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 // Validation rules can be stored in a config file.
144 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // Automatically load the form helper
147 $this->CI->load->helper('form');
148
Andrey Andreev90726b82015-01-20 12:39:22 +0200149 log_message('info', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 /**
155 * Set Rules
156 *
157 * This function takes an array of field names and validation
Andrey Andreev4b90a372014-03-10 10:24:24 +0200158 * rules as input, any custom error messages, validates the info,
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600159 * and stores it
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400161 * @param mixed $field
162 * @param string $label
163 * @param mixed $rules
Ahmedul Haque Abid0742fad2014-01-09 07:51:10 +0600164 * @param array $errors
Andrew Podner4296a652012-12-17 07:51:15 -0500165 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 */
Andrey Andreev4b90a372014-03-10 10:24:24 +0200167 public function set_rules($field, $label = '', $rules = array(), $errors = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +0000170 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200171 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Greg Aker9f9af602010-11-10 15:41:51 -0600173 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
tiyowanc2acb232012-03-14 21:24:00 +0400176 // If an array was passed via the first parameter instead of individual string
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 // values we cycle through it and recursively call this function.
178 if (is_array($field))
179 {
180 foreach ($field as $row)
181 {
182 // Houston, we have a problem...
Andrey Andreev78f55772012-04-03 19:59:08 +0300183 if ( ! isset($row['field'], $row['rules']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
185 continue;
186 }
187
188 // If the field label wasn't passed we use the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300189 $label = isset($row['label']) ? $row['label'] : $row['field'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000190
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600191 // Add the custom error message array
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600192 $errors = (isset($row['errors']) && is_array($row['errors'])) ? $row['errors'] : array();
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // Here we go!
Ahmedul Haque Abidbc1cbad2014-01-09 07:53:34 +0600195 $this->set_rules($row['field'], $label, $row['rules'], $errors);
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300197
Greg Aker9f9af602010-11-10 15:41:51 -0600198 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Andrey Andreev475dfac2015-04-07 00:07:04 +0300201 // No fields or no rules? Nothing to do...
202 if ( ! is_string($field) OR $field === '' OR empty($rules))
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Greg Aker9f9af602010-11-10 15:41:51 -0600204 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
Andrey Andreev4b90a372014-03-10 10:24:24 +0200206 elseif ( ! is_array($rules))
207 {
208 // BC: Convert pipe-separated rules string to an array
Andrey Andreev475dfac2015-04-07 00:07:04 +0300209 if ( ! is_string($rules))
Andrey Andreev4b90a372014-03-10 10:24:24 +0200210 {
211 return $this;
212 }
Andrey Andreev475dfac2015-04-07 00:07:04 +0300213
214 $rules = explode('|', $rules);
Andrey Andreev4b90a372014-03-10 10:24:24 +0200215 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
217 // If the field label wasn't passed we use the field name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100218 $label = ($label === '') ? $field : $label;
Derek Allard2067d1a2008-11-13 22:59:24 +0000219
Andrey Andreevfde170c2014-03-10 19:55:11 +0200220 $indexes = array();
221
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200222 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200223 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev4b90a372014-03-10 10:24:24 +0200224 if (($is_array = (bool) preg_match_all('/\[(.*?)\]/', $field, $matches)) === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200225 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200226 sscanf($field, '%[^[][', $indexes[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000227
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200228 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100230 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200232 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
234 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
237 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300239 'field' => $field,
240 'label' => $label,
241 'rules' => $rules,
Ahmedul Haque Abid7945d302014-01-09 16:50:23 +0600242 'errors' => $errors,
Andrey Andreev56454792012-05-17 14:32:19 +0300243 'is_array' => $is_array,
244 'keys' => $indexes,
245 'postdata' => NULL,
246 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000247 );
Greg Aker9f9af602010-11-10 15:41:51 -0600248
249 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
251
252 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 /**
JonoB099c4782012-03-04 14:37:30 +0000255 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200256 *
JonoB099c4782012-03-04 14:37:30 +0000257 * If an array is set through this method, then this array will
258 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200259 *
260 * Note that if you are validating multiple arrays, then the
261 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000262 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200263 *
264 * @param array $data
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200265 * @return CI_Form_validation
JonoB099c4782012-03-04 14:37:30 +0000266 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200267 public function set_data(array $data)
JonoB099c4782012-03-04 14:37:30 +0000268 {
Andrey Andreeva4712f52014-01-06 11:25:46 +0200269 if ( ! empty($data))
JonoB099c4782012-03-04 14:37:30 +0000270 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200271 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000272 }
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200273
274 return $this;
JonoB099c4782012-03-04 14:37:30 +0000275 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200276
JonoB099c4782012-03-04 14:37:30 +0000277 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000278
279 /**
280 * Set Error Message
281 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300282 * Lets users set their own error messages on the fly. Note:
283 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300285 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500287 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100289 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 if ( ! is_array($lang))
292 {
293 $lang = array($lang => $val);
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600297 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 /**
303 * Set The Error Delimiter
304 *
305 * Permits a prefix/suffix to be added to each error message
306 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @param string
308 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500309 * @return CI_Form_validation
Barry Mienydd671972010-10-04 16:33:58 +0200310 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100311 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
313 $this->_error_prefix = $prefix;
314 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600315 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
317
318 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 /**
321 * Get Error Message
322 *
323 * Gets the error message associated with a particular field
324 *
Andrey Andreeva4712f52014-01-06 11:25:46 +0200325 * @param string $field Field name
326 * @param string $prefix HTML start tag
Andrey Andreev868301a2014-01-06 12:29:50 +0200327 * @param string $suffix HTML end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300328 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200329 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200330 public function error($field, $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200331 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300332 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 return '';
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Alex Bilbied261b1e2012-06-02 11:12:16 +0100337 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 $prefix = $this->_error_prefix;
340 }
341
Alex Bilbied261b1e2012-06-02 11:12:16 +0100342 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
344 $suffix = $this->_error_suffix;
345 }
346
347 return $prefix.$this->_field_data[$field]['error'].$suffix;
348 }
349
350 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100353 * Get Array of Error Messages
354 *
355 * Returns the error messages as an array
356 *
357 * @return array
358 */
359 public function error_array()
360 {
361 return $this->_error_array;
362 }
363
364 // --------------------------------------------------------------------
365
366 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 * Error String
368 *
369 * Returns the error messages as a string, wrapped in the error delimiters
370 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @param string
372 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300373 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200374 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100375 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
vlakoff35672462013-02-15 01:36:04 +0100377 // No errors, validation passes!
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 if (count($this->_error_array) === 0)
379 {
380 return '';
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
Alex Bilbied261b1e2012-06-02 11:12:16 +0100383 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
385 $prefix = $this->_error_prefix;
386 }
387
Alex Bilbied261b1e2012-06-02 11:12:16 +0100388 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 $suffix = $this->_error_suffix;
391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // Generate the error string
394 $str = '';
395 foreach ($this->_error_array as $val)
396 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100397 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 $str .= $prefix.$val.$suffix."\n";
400 }
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 return $str;
404 }
405
406 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 /**
409 * Run the Validator
410 *
411 * This function does all the work.
412 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400413 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200415 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100416 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500418 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300419 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000420 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 return FALSE;
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // Does the _field_data array containing the validation rules exist?
426 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200427 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500429 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200430 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
432 return FALSE;
433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200435 if (empty($group))
436 {
437 // Is there a validation rule for the particular URI being accessed?
438 $group = trim($this->CI->uri->ruri_string(), '/');
439 isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method;
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200442 $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules);
Barry Mienydd671972010-10-04 16:33:58 +0200443
Andrey Andreev901573c2012-01-11 01:40:48 +0200444 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200445 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200447 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 return FALSE;
449 }
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // Load the language file containing error messages
453 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200454
Andrey Andreev751f2472012-11-03 18:26:27 +0200455 // Cycle through the rules for each field and match the corresponding $validation_data item
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200457 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200458 // Fetch the data from the validation_data array item and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 // 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 +0200460 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
JonoB099c4782012-03-04 14:37:30 +0000462 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
Andrey Andreevb137d232015-04-29 11:44:38 +0300464 elseif (isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300466 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200468 }
Barry Mienydd671972010-10-04 16:33:58 +0200469
Andrey Andreev751f2472012-11-03 18:26:27 +0200470 // Execute validation rules
471 // Note: A second foreach (for now) is required in order to avoid false-positives
472 // for rules like 'matches', which correlate to other validation fields.
473 foreach ($this->_field_data as $field => $row)
474 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300475 // Don't try to validate if we have no rules set
476 if (empty($row['rules']))
477 {
478 continue;
479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Andrey Andreev4b90a372014-03-10 10:24:24 +0200481 $this->_execute($row, $row['rules'], $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
483
484 // Did we end up with any errors?
485 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 if ($total_errors > 0)
487 {
488 $this->_safe_form_data = TRUE;
489 }
490
491 // Now we need to re-set the POST data with the new, processed data
492 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200493
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200494 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
496
497 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 /**
500 * Traverse a multidimensional $_POST array index until the data is found
501 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 * @param array
503 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300504 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200506 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100507 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200509 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200511 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Andrey Andreev2d48b4f2012-11-23 17:33:21 +0200514 // NULL must be returned for empty fields
Andrey Andreev44c34632012-11-23 18:46:34 +0200515 return ($array === '') ? NULL : $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 }
517
518 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 /**
521 * Re-populate the _POST array with our finalized and processed data
522 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200523 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200524 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100525 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
527 foreach ($this->_field_data as $field => $row)
528 {
vlakoff1228fe22013-01-14 01:30:09 +0100529 if ($row['postdata'] !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200531 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
533 if (isset($_POST[$row['field']]))
534 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200535 $_POST[$row['field']] = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
537 }
538 else
539 {
Derek Jones63eeae32009-02-10 19:08:56 +0000540 // start with a reference
541 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Jones63eeae32009-02-10 19:08:56 +0000543 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200544 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
Derek Jones63eeae32009-02-10 19:08:56 +0000546 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
548 else
549 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 foreach ($row['keys'] as $val)
551 {
Derek Jones63eeae32009-02-10 19:08:56 +0000552 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
554 }
Derek Jones63eeae32009-02-10 19:08:56 +0000555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000557 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 $array = array();
559 foreach ($row['postdata'] as $k => $v)
560 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200561 $array[$k] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 }
Derek Jones63eeae32009-02-10 19:08:56 +0000563
564 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
566 else
Derek Jones63eeae32009-02-10 19:08:56 +0000567 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200568 $post_ref = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
571 }
572 }
573 }
574
575 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 /**
578 * Executes the Validation routines
579 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * @param array
581 * @param array
582 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300583 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200585 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100586 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 // If the $_POST data is an array we will run a recursive call
589 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200590 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 foreach ($postdata as $key => $val)
592 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300593 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 return;
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 // If the field is blank, but NOT required, no further tests are necessary
600 $callback = FALSE;
Hashem Qolami05370bf2013-07-22 01:52:04 +0430601 if ( ! in_array('required', $rules) && ($postdata === NULL OR $postdata === ''))
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 {
603 // Before we bail out, does the rule contain a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200604 foreach ($rules as &$rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200606 if (is_string($rule))
607 {
608 if (strncmp($rule, 'callback_', 9) === 0)
609 {
610 $callback = TRUE;
611 $rules = array(1 => $rule);
612 break;
613 }
614 }
615 elseif (is_callable($rule))
616 {
617 $callback = TRUE;
618 $rules = array(1 => $rule);
619 break;
620 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 }
Andrey Andreev4b90a372014-03-10 10:24:24 +0200622
623 if ( ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
625 return;
626 }
627 }
628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev4b90a372014-03-10 10:24:24 +0200630 if (($postdata === NULL OR $postdata === '') && ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
632 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
633 {
634 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300635 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200636
Ahmedul Haque Abide9b0ccc2014-01-09 15:58:51 +0600637 // Check if a custom message is defined
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600638 if (isset($this->_field_data[$row['field']]['errors'][$type]))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600639 {
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600640 $line = $this->_field_data[$row['field']]['errors'][$type];
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600641 }
642 elseif (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 $line = $this->_error_messages[$type];
645 }
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200646 elseif (FALSE === ($line = $this->CI->lang->line('form_validation_'.$type))
647 // DEPRECATED support for non-prefixed keys
648 && FALSE === ($line = $this->CI->lang->line($type, FALSE)))
Andrey Andreev56454792012-05-17 14:32:19 +0300649 {
650 $line = 'The field was not set';
651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600654 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
Derek Allard2067d1a2008-11-13 22:59:24 +0000655
656 // Save the error message
657 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 if ( ! isset($this->_error_array[$row['field']]))
660 {
661 $this->_error_array[$row['field']] = $message;
662 }
663 }
Barry Mienydd671972010-10-04 16:33:58 +0200664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 return;
666 }
667
668 // --------------------------------------------------------------------
669
670 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300671 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
673 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // We set the $postdata variable with the current data in our master array so that
676 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100677 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
679 // We shouldn't need this safety, but just in case there isn't an array index
680 // associated with this cycle we'll bail out
681 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
682 {
683 continue;
684 }
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
687 $_in_array = TRUE;
688 }
689 else
690 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300691 // If we get an array field, but it's not expected - then it is most likely
692 // somebody messing with the form on the client side, so we'll just consider
693 // it an empty field
694 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
Andrey Andreev4b90a372014-03-10 10:24:24 +0200695 ? NULL
696 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 }
698
Barry Mienydd671972010-10-04 16:33:58 +0200699 // Is the rule a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200700 $callback = $callable = FALSE;
701 if (is_string($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200703 if (strpos($rule, 'callback_') === 0)
704 {
705 $rule = substr($rule, 9);
706 $callback = TRUE;
707 }
708 }
709 elseif (is_callable($rule))
710 {
711 $callable = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
Andrey Andreev60726ef2014-09-08 11:31:48 +0300713 elseif (is_array($rule) && isset($rule[0], $rule[1]) && is_callable($rule[1]))
714 {
715 // We have a "named" callable, so save the name
716 $callable = $rule[0];
717 $rule = $rule[1];
718 }
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 // Strip the parameter (if exists) from the rule
721 // Rules can contain a parameter: max_length[5]
722 $param = FALSE;
Andrey Andreev4b90a372014-03-10 10:24:24 +0200723 if ( ! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 {
Andrey Andreevef758bd2012-11-15 12:24:52 +0200725 $rule = $match[1];
726 $param = $match[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 }
Barry Mienydd671972010-10-04 16:33:58 +0200728
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 // Call the function that corresponds to the rule
Andrey Andreev60726ef2014-09-08 11:31:48 +0300730 if ($callback OR $callable !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200732 if ($callback)
Barry Mienydd671972010-10-04 16:33:58 +0200733 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200734 if ( ! method_exists($this->CI, $rule))
735 {
736 log_message('debug', 'Unable to find callback validation rule: '.$rule);
737 $result = FALSE;
738 }
739 else
740 {
741 // Run the function and grab the result
742 $result = $this->CI->$rule($postdata, $param);
743 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200745 else
746 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200747 $result = is_array($rule)
Andrey Andreev60726ef2014-09-08 11:31:48 +0300748 ? $rule[0]->{$rule[1]}($postdata)
749 : $rule($postdata);
750
751 // Is $callable set to a rule name?
752 if ($callable !== FALSE)
753 {
754 $rule = $callable;
755 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200756 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000757
758 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200759 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300761 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 }
763 else
764 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300765 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 }
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200769 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000771 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 }
773 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300774 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200775 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300776 // If our own wrapper function doesn't exist we see if a native PHP function does.
777 // Users can use any native PHP function call that has one param.
778 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200780 // Native PHP functions issue warnings if you pass them more parameters than they use
Andrey Andreev320d37c2012-04-03 20:21:39 +0300781 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200782
Andrey Andreev78f55772012-04-03 19:59:08 +0300783 if ($_in_array === TRUE)
784 {
785 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 }
patwork02404a12011-04-08 15:45:46 +0200787 else
788 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300789 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200790 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200792 else
793 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300794 log_message('debug', 'Unable to find validation rule: '.$rule);
795 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200796 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300797 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 else
799 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 $result = $this->$rule($postdata, $param);
801
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200802 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300804 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
806 else
807 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300808 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 }
810 }
Barry Mienydd671972010-10-04 16:33:58 +0200811
Andrey Andreev901573c2012-01-11 01:40:48 +0200812 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200814 {
Andrey Andreev60726ef2014-09-08 11:31:48 +0300815 // Callable rules might not have named error messages
Andrey Andreev57f10052014-06-07 12:22:37 +0300816 if ( ! is_string($rule))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600817 {
Andrey Andreev38b5eb02015-06-10 17:38:26 +0300818 $line = $this->CI->lang->line('form_validation_error_message_not_set').'(Anonymous function)';
Andrey Andreev57f10052014-06-07 12:22:37 +0300819 }
Andrey Andreev57f10052014-06-07 12:22:37 +0300820 // Check if a custom message is defined
Andrey Andreev38b5eb02015-06-10 17:38:26 +0300821 elseif (isset($this->_field_data[$row['field']]['errors'][$rule]))
Andrey Andreev57f10052014-06-07 12:22:37 +0300822 {
823 $line = $this->_field_data[$row['field']]['errors'][$rule];
824 }
825 elseif ( ! isset($this->_error_messages[$rule]))
826 {
827 if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule))
828 // DEPRECATED support for non-prefixed keys
829 && FALSE === ($line = $this->CI->lang->line($rule, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 {
Andrey Andreevcc778882015-03-13 11:24:30 +0200831 $line = $this->CI->lang->line('form_validation_error_message_not_set').'('.$rule.')';
Barry Mienydd671972010-10-04 16:33:58 +0200832 }
Andrey Andreev57f10052014-06-07 12:22:37 +0300833 }
834 else
835 {
836 $line = $this->_error_messages[$rule];
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 }
Barry Mienydd671972010-10-04 16:33:58 +0200838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200840 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200841 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 {
Pascal Krietec1895832009-10-13 12:56:43 +0000843 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 }
Barry Mienydd671972010-10-04 16:33:58 +0200845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600847 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000848
849 // Save the error message
850 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200851
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 if ( ! isset($this->_error_array[$row['field']]))
853 {
854 $this->_error_array[$row['field']] = $message;
855 }
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 return;
858 }
859 }
860 }
861
862 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 /**
865 * Translate a field name
866 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 * @param string the field name
868 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200869 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100870 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
Andrey Andreev5d8e2a62015-04-14 16:56:28 +0300872 // Do we need to translate the field name? We look for the prefix 'lang:' to determine this
873 // If we find one, but there's no translation for the string - just return it
874 if (sscanf($fieldname, 'lang:%s', $line) === 1 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 {
Andrey Andreev5d8e2a62015-04-14 16:56:28 +0300876 return $line;
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 }
878
879 return $fieldname;
880 }
881
882 // --------------------------------------------------------------------
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200883
Eric Roberts41cc0902012-01-24 00:59:44 -0600884 /**
885 * Build an error message using the field and param.
886 *
887 * @param string The error message line
888 * @param string A field's human name
889 * @param mixed A rule's optional parameter
890 * @return string
891 */
892 protected function _build_error_msg($line, $field = '', $param = '')
893 {
894 // Check for %s in the string for legacy support.
Eric Roberts24a13f52012-12-12 07:09:42 -0600895 if (strpos($line, '%s') !== FALSE)
Eric Roberts41cc0902012-01-24 00:59:44 -0600896 {
897 return sprintf($line, $field, $param);
898 }
Andrew Podner4296a652012-12-17 07:51:15 -0500899
Eric Roberts41cc0902012-01-24 00:59:44 -0600900 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
901 }
902
903 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 /**
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530906 * Checks if the rule is present within the validator
907 *
908 * Permits you to check if a rule is present within the validator
909 *
910 * @param string the field name
911 * @return bool
912 */
913 public function has_rule($field)
914 {
915 return isset($this->_field_data[$field]);
916 }
917
918 // --------------------------------------------------------------------
919
920 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * Get the value from a form
922 *
923 * Permits you to repopulate a form field with the value it was submitted
924 * with, or, if that value doesn't exist, with the default
925 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @param string the field name
927 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200928 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200929 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100930 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200932 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
934 return $default;
935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Phil Sturgeon5c561802011-01-05 16:31:59 +0000937 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600938 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000939 if (is_array($this->_field_data[$field]['postdata']))
940 {
941 return array_shift($this->_field_data[$field]['postdata']);
942 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000943
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 return $this->_field_data[$field]['postdata'];
945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 /**
950 * Set Select
951 *
952 * Enables pull-down lists to be set to the value the user
953 * selected in the event of an error
954 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 * @param string
956 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400957 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200959 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100960 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200961 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200962 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200964 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300968 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 if (is_array($field))
970 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300971 // Note: in_array('', array(0)) returns TRUE, do not use it
972 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300974 if ($value === $v)
975 {
976 return ' selected="selected"';
977 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300979
980 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100982 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200984 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 }
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 return ' selected="selected"';
988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 /**
993 * Set Radio
994 *
995 * Enables radio buttons to be set to the value the user
996 * selected in the event of an error
997 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 * @param string
999 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001000 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001002 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001003 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 {
Andrey Andreev46ac8812012-02-28 14:32:54 +02001005 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 {
Andrey Andreev6de924c2012-01-20 13:18:18 +02001007 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 }
Barry Mienydd671972010-10-04 16:33:58 +02001009
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +03001011 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 if (is_array($field))
1013 {
Andrey Andreeva587a932013-10-23 19:57:46 +03001014 // Note: in_array('', array(0)) returns TRUE, do not use it
1015 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 {
Andrey Andreeva587a932013-10-23 19:57:46 +03001017 if ($value === $v)
1018 {
1019 return ' checked="checked"';
1020 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 }
Andrey Andreeva587a932013-10-23 19:57:46 +03001022
1023 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 }
Alex Bilbied261b1e2012-06-02 11:12:16 +01001025 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001027 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 }
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 return ' checked="checked"';
1031 }
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 * Set Checkbox
1037 *
1038 * Enables checkboxes to be set to the value the user
1039 * selected in the event of an error
1040 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 * @param string
1042 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001043 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001045 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001046 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001048 // Logic is exactly the same as for radio fields
1049 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 }
Barry Mienydd671972010-10-04 16:33:58 +02001051
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001053
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 /**
1055 * Required
1056 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 * @param string
1058 * @return bool
1059 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001060 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001062 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 }
Barry Mienydd671972010-10-04 16:33:58 +02001064
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001066
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001068 * Performs a Regular Expression match test.
1069 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001070 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001071 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001072 * @return bool
1073 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001074 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001075 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001076 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001077 }
1078
1079 // --------------------------------------------------------------------
1080
1081 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 * Match one field to another
1083 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001084 * @param string $str string to compare against
1085 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 * @return bool
1087 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001088 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001090 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
1091 ? ($str === $this->_field_data[$field]['postdata'])
1092 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001094
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001095 // --------------------------------------------------------------------
1096
1097 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -03001098 * Differs from another field
1099 *
1100 * @param string
1101 * @param string field
1102 * @return bool
1103 */
1104 public function differs($str, $field)
1105 {
1106 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001108
1109 // --------------------------------------------------------------------
1110
1111 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +02001112 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001113 *
Andrey Andreevd09d6502012-01-03 06:38:33 +02001114 * Check if the input value doesn't already exist
1115 * in the specified database field.
1116 *
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001117 * @param string $str
1118 * @param string $field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001119 * @return bool
1120 */
1121 public function is_unique($str, $field)
1122 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001123 sscanf($field, '%[^.].%[^.]', $table, $field);
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001124 return isset($this->CI->db)
1125 ? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
1126 : FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001127 }
Barry Mienydd671972010-10-04 16:33:58 +02001128
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001130
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 /**
1132 * Minimum Length
1133 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001135 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001137 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001138 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001140 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 {
1142 return FALSE;
1143 }
1144
bjjay8d6c8fe2015-03-09 13:46:06 +08001145 return ($val <= mb_strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 }
Barry Mienydd671972010-10-04 16:33:58 +02001147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001149
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 /**
1151 * Max Length
1152 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001154 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001156 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001157 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001159 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 {
1161 return FALSE;
1162 }
1163
bjjay8d6c8fe2015-03-09 13:46:06 +08001164 return ($val >= mb_strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 }
Barry Mienydd671972010-10-04 16:33:58 +02001166
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001168
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 /**
1170 * Exact Length
1171 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001173 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001175 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001176 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001178 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 {
1180 return FALSE;
1181 }
1182
bjjay8d6c8fe2015-03-09 13:46:06 +08001183 return (mb_strlen($str) === (int) $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 }
Barry Mienydd671972010-10-04 16:33:58 +02001185
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001187
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 /**
Andrey Andreevdaaca882012-11-26 22:16:12 +02001189 * Valid URL
1190 *
1191 * @param string $str
1192 * @return bool
1193 */
1194 public function valid_url($str)
1195 {
1196 if (empty($str))
1197 {
1198 return FALSE;
1199 }
1200 elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
1201 {
1202 if (empty($matches[2]))
1203 {
1204 return FALSE;
1205 }
1206 elseif ( ! in_array($matches[1], array('http', 'https'), TRUE))
1207 {
1208 return FALSE;
1209 }
1210
1211 $str = $matches[2];
1212 }
1213
1214 $str = 'http://'.$str;
1215
1216 // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the
1217 // underscore to be a valid hostname character instead of a dash.
1218 // Reference: https://bugs.php.net/bug.php?id=51192
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001219 if (version_compare(PHP_VERSION, '5.2.13', '==') OR version_compare(PHP_VERSION, '5.3.2', '=='))
Andrey Andreevdaaca882012-11-26 22:16:12 +02001220 {
1221 sscanf($str, 'http://%[^/]', $host);
1222 $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host));
1223 }
1224
1225 return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 }
1227
1228 // --------------------------------------------------------------------
1229
1230 /**
1231 * Valid Email
1232 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 * @param string
1234 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001235 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001236 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 {
Andrey Andreevf9201ae2014-06-01 12:57:30 +03001238 if (function_exists('idn_to_ascii') && $atpos = strpos($str, '@'))
Andrey Andreev95496662014-06-01 00:00:13 +03001239 {
Andrey Andreevf9201ae2014-06-01 12:57:30 +03001240 $str = substr($str, 0, ++$atpos).idn_to_ascii(substr($str, $atpos));
Andrey Andreev95496662014-06-01 00:00:13 +03001241 }
1242
Andrey Andreev580388b2012-06-27 15:43:46 +03001243 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 }
1245
1246 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001247
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 /**
1249 * Valid Emails
1250 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 * @param string
1252 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001253 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001254 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 {
1256 if (strpos($str, ',') === FALSE)
1257 {
1258 return $this->valid_email(trim($str));
1259 }
Barry Mienydd671972010-10-04 16:33:58 +02001260
Pascal Kriete14287f32011-02-14 13:39:34 -05001261 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001263 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 {
1265 return FALSE;
1266 }
1267 }
Barry Mienydd671972010-10-04 16:33:58 +02001268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 return TRUE;
1270 }
1271
1272 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001273
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 /**
1275 * Validate IP Address
1276 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001278 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001279 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001281 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001283 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 }
1285
1286 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001287
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 /**
1289 * Alpha
1290 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 * @param string
1292 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001293 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001294 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001296 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 }
Barry Mienydd671972010-10-04 16:33:58 +02001298
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001300
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 /**
1302 * Alpha-numeric
1303 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 * @param string
1305 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001306 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001307 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001309 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 }
Barry Mienydd671972010-10-04 16:33:58 +02001311
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 // --------------------------------------------------------------------
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001313
1314 /**
1315 * Alpha-numeric w/ spaces
1316 *
1317 * @param string
1318 * @return bool
1319 */
1320 public function alpha_numeric_spaces($str)
1321 {
Sajan Parikhdf3bfed2013-02-04 12:25:49 -06001322 return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001323 }
1324
1325 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001326
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 /**
1328 * Alpha-numeric with underscores and dashes
1329 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 * @param string
1331 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001332 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001333 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001335 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 }
Barry Mienydd671972010-10-04 16:33:58 +02001337
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001339
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 /**
1341 * Numeric
1342 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 * @param string
1344 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001345 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001346 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001348 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001349
1350 }
1351
1352 // --------------------------------------------------------------------
1353
Barry Mienydd671972010-10-04 16:33:58 +02001354 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 * Integer
1356 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 * @param string
1358 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001359 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001360 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001362 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1363 }
1364
1365 // --------------------------------------------------------------------
1366
1367 /**
1368 * Decimal number
1369 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001370 * @param string
1371 * @return bool
1372 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001373 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001374 {
1375 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1376 }
1377
1378 // --------------------------------------------------------------------
1379
1380 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001381 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001382 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001383 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001384 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001385 * @return bool
1386 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001387 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001388 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001389 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001390 }
1391
1392 // --------------------------------------------------------------------
1393
1394 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001395 * Equal to or Greater than
1396 *
Nick Busey98c347d2012-02-02 11:07:03 -07001397 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001398 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001399 * @return bool
1400 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001401 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001402 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001403 return is_numeric($str) ? ($str >= $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001404 }
1405
1406 // --------------------------------------------------------------------
1407
1408 /**
1409 * Less than
1410 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001411 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001412 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001413 * @return bool
1414 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001415 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001416 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001417 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001418 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001419
1420 // --------------------------------------------------------------------
1421
Barry Mienydd671972010-10-04 16:33:58 +02001422 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001423 * Equal to or Less than
1424 *
Nick Busey98c347d2012-02-02 11:07:03 -07001425 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001426 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001427 * @return bool
1428 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001429 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001430 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001431 return is_numeric($str) ? ($str <= $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 }
1433
1434 // --------------------------------------------------------------------
1435
Barry Mienydd671972010-10-04 16:33:58 +02001436 /**
Lance Vincenteca885d2015-01-28 17:43:23 +08001437 * Value should be within an array of values
1438 *
1439 * @param string
Andrey Andreev5b662e42015-01-29 00:13:54 +02001440 * @param string
Lance Vincenteca885d2015-01-28 17:43:23 +08001441 * @return bool
1442 */
Lance Vincent49f483d2015-01-28 22:46:19 +08001443 public function in_list($value, $list)
Lance Vincenteca885d2015-01-28 17:43:23 +08001444 {
Lance Vincent0b0117c2015-01-28 20:31:42 +08001445 return in_array($value, explode(',', $list), TRUE);
Lance Vincenteca885d2015-01-28 17:43:23 +08001446 }
1447
1448 // --------------------------------------------------------------------
1449
1450 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001451 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001452 *
Barry Mienydd671972010-10-04 16:33:58 +02001453 * @param string
1454 * @return bool
1455 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001456 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001457 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001458 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001459 }
1460
1461 // --------------------------------------------------------------------
1462
1463 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001464 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001465 *
Barry Mienydd671972010-10-04 16:33:58 +02001466 * @param string
1467 * @return bool
1468 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001469 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001470 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001471 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001472 }
1473
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 * Valid Base64
1478 *
1479 * Tests a string for characters outside of the Base64 alphabet
1480 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1481 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 * @param string
1483 * @return bool
1484 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001485 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 {
Andrey Andreevcd9797a2013-06-28 14:03:48 +03001487 return (base64_encode(base64_decode($str)) === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001488 }
Barry Mienydd671972010-10-04 16:33:58 +02001489
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001491
Derek Allard2067d1a2008-11-13 22:59:24 +00001492 /**
1493 * Prep data for form
1494 *
1495 * This function allows HTML to be safely shown in a form.
1496 * Special characters are converted.
1497 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001498 * @param string
1499 * @return string
1500 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001501 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001503 if ($this->_safe_form_data === FALSE OR empty($data))
1504 {
1505 return $data;
1506 }
1507
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 if (is_array($data))
1509 {
1510 foreach ($data as $key => $val)
1511 {
1512 $data[$key] = $this->prep_for_form($val);
1513 }
Barry Mienydd671972010-10-04 16:33:58 +02001514
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 return $data;
1516 }
Barry Mienydd671972010-10-04 16:33:58 +02001517
Andrey Andreev901573c2012-01-11 01:40:48 +02001518 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001519 }
Barry Mienydd671972010-10-04 16:33:58 +02001520
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001522
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 /**
1524 * Prep URL
1525 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 * @param string
1527 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001528 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001529 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001531 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001532 {
1533 return '';
1534 }
Barry Mienydd671972010-10-04 16:33:58 +02001535
Andrey Andreev901573c2012-01-11 01:40:48 +02001536 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001538 return 'http://'.$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 return $str;
1542 }
Barry Mienydd671972010-10-04 16:33:58 +02001543
Derek Allard2067d1a2008-11-13 22:59:24 +00001544 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001545
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 /**
1547 * Strip Image Tags
1548 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001549 * @param string
1550 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001551 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001552 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001554 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 }
Barry Mienydd671972010-10-04 16:33:58 +02001556
Derek Allard2067d1a2008-11-13 22:59:24 +00001557 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001558
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 * Convert PHP tags to entities
1561 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 * @param string
1563 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001564 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001565 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001566 {
Andrey Andreev838a9d62012-12-03 14:37:47 +02001567 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001568 }
1569
JonoB099c4782012-03-04 14:37:30 +00001570 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001571
1572 /**
1573 * Reset validation vars
1574 *
1575 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001576 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001577 *
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001578 * @return CI_Form_validation
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001579 */
JonoB883f80f2012-03-05 09:51:27 +00001580 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001581 {
JonoB099c4782012-03-04 14:37:30 +00001582 $this->_field_data = array();
1583 $this->_config_rules = array();
1584 $this->_error_array = array();
1585 $this->_error_messages = array();
1586 $this->error_string = '';
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001587 return $this;
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001588 }
1589
Derek Allard2067d1a2008-11-13 22:59:24 +00001590}