blob: e4a5189579a8bf5da5eb71427884dc1f13a6f87f [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 Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/form_validation.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
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
richcc936352015-09-09 15:52:26 -0400214 $rules = preg_split('/\|(?![^\[]*\])/', $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 {
Andrey Andreevce8fa5a2015-12-14 12:57:09 +0200418 $validation_array = empty($this->validation_data)
419 ? $_POST
420 : $this->validation_data;
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // Does the _field_data array containing the validation rules exist?
423 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200424 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500426 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200427 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 return FALSE;
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200432 if (empty($group))
433 {
434 // Is there a validation rule for the particular URI being accessed?
435 $group = trim($this->CI->uri->ruri_string(), '/');
436 isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method;
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200439 $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules);
Barry Mienydd671972010-10-04 16:33:58 +0200440
Andrey Andreev901573c2012-01-11 01:40:48 +0200441 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200442 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200444 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 return FALSE;
446 }
447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 // Load the language file containing error messages
450 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200451
Andrey Andreev751f2472012-11-03 18:26:27 +0200452 // Cycle through the rules for each field and match the corresponding $validation_data item
Andrey Andreev55a40b72016-03-12 15:43:22 +0200453 foreach ($this->_field_data as $field => &$row)
Barry Mienydd671972010-10-04 16:33:58 +0200454 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200455 // Fetch the data from the validation_data array item and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 // 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 +0200457 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
JonoB099c4782012-03-04 14:37:30 +0000459 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
Andrey Andreevb137d232015-04-29 11:44:38 +0300461 elseif (isset($validation_array[$field]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300463 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Andrey Andreev751f2472012-11-03 18:26:27 +0200467 // Execute validation rules
468 // Note: A second foreach (for now) is required in order to avoid false-positives
469 // for rules like 'matches', which correlate to other validation fields.
Andrey Andreev55a40b72016-03-12 15:43:22 +0200470 foreach ($this->_field_data as $field => &$row)
Andrey Andreev751f2472012-11-03 18:26:27 +0200471 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300472 // Don't try to validate if we have no rules set
473 if (empty($row['rules']))
474 {
475 continue;
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Andrey Andreev55a40b72016-03-12 15:43:22 +0200478 $this->_execute($row, $row['rules'], $row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480
481 // Did we end up with any errors?
482 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 if ($total_errors > 0)
484 {
485 $this->_safe_form_data = TRUE;
486 }
487
488 // Now we need to re-set the POST data with the new, processed data
Andrey Andreev02ac1872016-03-16 12:19:34 +0200489 empty($this->validation_data) && $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200490
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200491 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493
494 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 /**
497 * Traverse a multidimensional $_POST array index until the data is found
498 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 * @param array
500 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300501 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200503 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100504 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200506 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200508 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 }
Barry Mienydd671972010-10-04 16:33:58 +0200510
Andrey Andreev2d48b4f2012-11-23 17:33:21 +0200511 // NULL must be returned for empty fields
Andrey Andreev44c34632012-11-23 18:46:34 +0200512 return ($array === '') ? NULL : $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514
515 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 /**
518 * Re-populate the _POST array with our finalized and processed data
519 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200520 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200521 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100522 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 foreach ($this->_field_data as $field => $row)
525 {
vlakoff1228fe22013-01-14 01:30:09 +0100526 if ($row['postdata'] !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200528 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
Andrey Andreev5576d2c2016-03-16 12:16:45 +0200530 isset($_POST[$field]) && $_POST[$field] = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
532 else
533 {
Derek Jones63eeae32009-02-10 19:08:56 +0000534 // start with a reference
535 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Jones63eeae32009-02-10 19:08:56 +0000537 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200538 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
Derek Jones63eeae32009-02-10 19:08:56 +0000540 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
542 else
543 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 foreach ($row['keys'] as $val)
545 {
Derek Jones63eeae32009-02-10 19:08:56 +0000546 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
548 }
Derek Jones63eeae32009-02-10 19:08:56 +0000549
Andrey Andreev5576d2c2016-03-16 12:16:45 +0200550 $post_ref = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
552 }
553 }
554 }
555
556 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 /**
559 * Executes the Validation routines
560 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @param array
562 * @param array
563 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300564 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200566 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100567 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
569 // If the $_POST data is an array we will run a recursive call
Andrey Andreev7243d0b2016-03-12 11:40:34 +0200570 //
571 // Note: We MUST check if the array is empty or not!
572 // Otherwise empty arrays will always pass validation.
573 if (is_array($postdata) && ! empty($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200574 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 foreach ($postdata as $key => $val)
576 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300577 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 return;
581 }
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 // If the field is blank, but NOT required, no further tests are necessary
584 $callback = FALSE;
Hashem Qolami05370bf2013-07-22 01:52:04 +0430585 if ( ! in_array('required', $rules) && ($postdata === NULL OR $postdata === ''))
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 // Before we bail out, does the rule contain a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200588 foreach ($rules as &$rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200590 if (is_string($rule))
591 {
592 if (strncmp($rule, 'callback_', 9) === 0)
593 {
594 $callback = TRUE;
595 $rules = array(1 => $rule);
596 break;
597 }
598 }
599 elseif (is_callable($rule))
600 {
601 $callback = TRUE;
602 $rules = array(1 => $rule);
603 break;
604 }
Andrey Andreev29de1652015-06-11 17:44:20 +0300605 elseif (is_array($rule) && isset($rule[0], $rule[1]) && is_callable($rule[1]))
606 {
607 $callback = TRUE;
Andrey Andreevbd1e6152015-06-15 18:28:36 +0300608 $rules = array(array($rule[0], $rule[1]));
Andrey Andreev29de1652015-06-11 17:44:20 +0300609 break;
610 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 }
Andrey Andreev4b90a372014-03-10 10:24:24 +0200612
613 if ( ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
615 return;
616 }
617 }
618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev4b90a372014-03-10 10:24:24 +0200620 if (($postdata === NULL OR $postdata === '') && ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
622 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
623 {
624 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300625 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200626
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200627 $line = $this->_get_error_message($type, $row['field']);
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600630 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
Derek Allard2067d1a2008-11-13 22:59:24 +0000631
632 // Save the error message
633 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 if ( ! isset($this->_error_array[$row['field']]))
636 {
637 $this->_error_array[$row['field']] = $message;
638 }
639 }
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 return;
642 }
643
644 // --------------------------------------------------------------------
645
646 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300647 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
649 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 // We set the $postdata variable with the current data in our master array so that
652 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100653 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
655 // We shouldn't need this safety, but just in case there isn't an array index
656 // associated with this cycle we'll bail out
657 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
658 {
659 continue;
660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
663 $_in_array = TRUE;
664 }
665 else
666 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300667 // If we get an array field, but it's not expected - then it is most likely
668 // somebody messing with the form on the client side, so we'll just consider
669 // it an empty field
670 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
Andrey Andreev4b90a372014-03-10 10:24:24 +0200671 ? NULL
672 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 }
674
Barry Mienydd671972010-10-04 16:33:58 +0200675 // Is the rule a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200676 $callback = $callable = FALSE;
677 if (is_string($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200679 if (strpos($rule, 'callback_') === 0)
680 {
681 $rule = substr($rule, 9);
682 $callback = TRUE;
683 }
684 }
685 elseif (is_callable($rule))
686 {
687 $callable = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 }
Andrey Andreev60726ef2014-09-08 11:31:48 +0300689 elseif (is_array($rule) && isset($rule[0], $rule[1]) && is_callable($rule[1]))
690 {
691 // We have a "named" callable, so save the name
692 $callable = $rule[0];
693 $rule = $rule[1];
694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 // Strip the parameter (if exists) from the rule
697 // Rules can contain a parameter: max_length[5]
698 $param = FALSE;
Andrey Andreev4b90a372014-03-10 10:24:24 +0200699 if ( ! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
Andrey Andreevef758bd2012-11-15 12:24:52 +0200701 $rule = $match[1];
702 $param = $match[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 }
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 // Call the function that corresponds to the rule
Andrey Andreev60726ef2014-09-08 11:31:48 +0300706 if ($callback OR $callable !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200708 if ($callback)
Barry Mienydd671972010-10-04 16:33:58 +0200709 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200710 if ( ! method_exists($this->CI, $rule))
711 {
712 log_message('debug', 'Unable to find callback validation rule: '.$rule);
713 $result = FALSE;
714 }
715 else
716 {
717 // Run the function and grab the result
718 $result = $this->CI->$rule($postdata, $param);
719 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200721 else
722 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200723 $result = is_array($rule)
Andrey Andreev60726ef2014-09-08 11:31:48 +0300724 ? $rule[0]->{$rule[1]}($postdata)
725 : $rule($postdata);
726
727 // Is $callable set to a rule name?
728 if ($callable !== FALSE)
729 {
730 $rule = $callable;
731 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200732 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000733
734 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200735 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300737 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
739 else
740 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300741 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
Barry Mienydd671972010-10-04 16:33:58 +0200743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200745 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000747 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 }
749 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300750 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200751 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300752 // If our own wrapper function doesn't exist we see if a native PHP function does.
753 // Users can use any native PHP function call that has one param.
754 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200756 // Native PHP functions issue warnings if you pass them more parameters than they use
Andrey Andreev320d37c2012-04-03 20:21:39 +0300757 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200758
Andrey Andreev78f55772012-04-03 19:59:08 +0300759 if ($_in_array === TRUE)
760 {
761 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 }
patwork02404a12011-04-08 15:45:46 +0200763 else
764 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300765 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200766 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200768 else
769 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300770 log_message('debug', 'Unable to find validation rule: '.$rule);
771 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200772 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300773 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 else
775 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 $result = $this->$rule($postdata, $param);
777
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200778 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300780 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 }
782 else
783 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300784 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 }
786 }
Barry Mienydd671972010-10-04 16:33:58 +0200787
Andrey Andreev901573c2012-01-11 01:40:48 +0200788 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200790 {
Andrey Andreev60726ef2014-09-08 11:31:48 +0300791 // Callable rules might not have named error messages
Andrey Andreev57f10052014-06-07 12:22:37 +0300792 if ( ! is_string($rule))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600793 {
Andrey Andreev38b5eb02015-06-10 17:38:26 +0300794 $line = $this->CI->lang->line('form_validation_error_message_not_set').'(Anonymous function)';
Andrey Andreev57f10052014-06-07 12:22:37 +0300795 }
Andrey Andreev57f10052014-06-07 12:22:37 +0300796 else
797 {
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200798 $line = $this->_get_error_message($rule, $row['field']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 }
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200802 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200803 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 {
Pascal Krietec1895832009-10-13 12:56:43 +0000805 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 }
Barry Mienydd671972010-10-04 16:33:58 +0200807
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600809 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000810
811 // Save the error message
812 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 if ( ! isset($this->_error_array[$row['field']]))
815 {
816 $this->_error_array[$row['field']] = $message;
817 }
Barry Mienydd671972010-10-04 16:33:58 +0200818
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 return;
820 }
821 }
822 }
823
824 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 /**
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200827 * Get the error message for the rule
828 *
829 * @param string $rule The rule name
830 * @param string $field The field name
831 * @return string
832 */
833 protected function _get_error_message($rule, $field)
834 {
835 // check if a custom message is defined through validation config row.
836 if (isset($this->_field_data[$field]['errors'][$rule]))
837 {
838 return $this->_field_data[$field]['errors'][$rule];
839 }
840 // check if a custom message has been set using the set_message() function
841 elseif (isset($this->_error_messages[$rule]))
842 {
843 return $this->_error_messages[$rule];
844 }
Andrey Andreevb30a64a2016-02-09 23:46:25 +0200845 elseif (FALSE !== ($line = $this->CI->lang->line('form_validation_'.$rule)))
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200846 {
Andrey Andreevb30a64a2016-02-09 23:46:25 +0200847 return $line;
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200848 }
849 // DEPRECATED support for non-prefixed keys, lang file again
Andrey Andreevb30a64a2016-02-09 23:46:25 +0200850 elseif (FALSE !== ($line = $this->CI->lang->line($rule, FALSE)))
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200851 {
Andrey Andreevb30a64a2016-02-09 23:46:25 +0200852 return $line;
Andrey Andreev98c14ae2016-02-09 23:43:55 +0200853 }
854
855 return $this->CI->lang->line('form_validation_error_message_not_set').'('.$rule.')';
856 }
857
858 // --------------------------------------------------------------------
859
860 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 * Translate a field name
862 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 * @param string the field name
864 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200865 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100866 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 {
Andrey Andreev5d8e2a62015-04-14 16:56:28 +0300868 // Do we need to translate the field name? We look for the prefix 'lang:' to determine this
869 // If we find one, but there's no translation for the string - just return it
870 if (sscanf($fieldname, 'lang:%s', $line) === 1 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
Andrey Andreev5d8e2a62015-04-14 16:56:28 +0300872 return $line;
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 }
874
875 return $fieldname;
876 }
877
878 // --------------------------------------------------------------------
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200879
Eric Roberts41cc0902012-01-24 00:59:44 -0600880 /**
881 * Build an error message using the field and param.
882 *
883 * @param string The error message line
884 * @param string A field's human name
885 * @param mixed A rule's optional parameter
886 * @return string
887 */
888 protected function _build_error_msg($line, $field = '', $param = '')
889 {
890 // Check for %s in the string for legacy support.
Eric Roberts24a13f52012-12-12 07:09:42 -0600891 if (strpos($line, '%s') !== FALSE)
Eric Roberts41cc0902012-01-24 00:59:44 -0600892 {
893 return sprintf($line, $field, $param);
894 }
Andrew Podner4296a652012-12-17 07:51:15 -0500895
Eric Roberts41cc0902012-01-24 00:59:44 -0600896 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
897 }
898
899 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 /**
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530902 * Checks if the rule is present within the validator
903 *
904 * Permits you to check if a rule is present within the validator
905 *
906 * @param string the field name
907 * @return bool
908 */
909 public function has_rule($field)
910 {
911 return isset($this->_field_data[$field]);
912 }
913
914 // --------------------------------------------------------------------
915
916 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 * Get the value from a form
918 *
919 * Permits you to repopulate a form field with the value it was submitted
920 * with, or, if that value doesn't exist, with the default
921 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 * @param string the field name
923 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200924 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200925 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100926 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200928 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
930 return $default;
931 }
Barry Mienydd671972010-10-04 16:33:58 +0200932
Phil Sturgeon5c561802011-01-05 16:31:59 +0000933 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600934 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000935 if (is_array($this->_field_data[$field]['postdata']))
936 {
937 return array_shift($this->_field_data[$field]['postdata']);
938 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 return $this->_field_data[$field]['postdata'];
941 }
Barry Mienydd671972010-10-04 16:33:58 +0200942
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200944
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 /**
946 * Set Select
947 *
948 * Enables pull-down lists to be set to the value the user
949 * selected in the event of an error
950 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 * @param string
952 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400953 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200955 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100956 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200957 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200958 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200960 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300964 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 if (is_array($field))
966 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300967 // Note: in_array('', array(0)) returns TRUE, do not use it
968 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300970 if ($value === $v)
971 {
972 return ' selected="selected"';
973 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300975
976 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100978 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200980 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 }
Barry Mienydd671972010-10-04 16:33:58 +0200982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 return ' selected="selected"';
984 }
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 /**
989 * Set Radio
990 *
991 * Enables radio buttons to be set to the value the user
992 * selected in the event of an error
993 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 * @param string
995 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400996 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200998 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100999 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Andrey Andreev46ac8812012-02-28 14:32:54 +02001001 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 {
Andrey Andreev6de924c2012-01-20 13:18:18 +02001003 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +03001007 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 if (is_array($field))
1009 {
Andrey Andreeva587a932013-10-23 19:57:46 +03001010 // Note: in_array('', array(0)) returns TRUE, do not use it
1011 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 {
Andrey Andreeva587a932013-10-23 19:57:46 +03001013 if ($value === $v)
1014 {
1015 return ' checked="checked"';
1016 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 }
Andrey Andreeva587a932013-10-23 19:57:46 +03001018
1019 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 }
Alex Bilbied261b1e2012-06-02 11:12:16 +01001021 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001023 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 }
Barry Mienydd671972010-10-04 16:33:58 +02001025
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 return ' checked="checked"';
1027 }
Barry Mienydd671972010-10-04 16:33:58 +02001028
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001030
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 /**
1032 * Set Checkbox
1033 *
1034 * Enables checkboxes to be set to the value the user
1035 * selected in the event of an error
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @param string
1038 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001039 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001041 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001042 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001044 // Logic is exactly the same as for radio fields
1045 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 }
Barry Mienydd671972010-10-04 16:33:58 +02001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 /**
1051 * Required
1052 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 * @param string
1054 * @return bool
1055 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001056 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001058 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 }
Barry Mienydd671972010-10-04 16:33:58 +02001060
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001064 * Performs a Regular Expression match test.
1065 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001066 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001067 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001068 * @return bool
1069 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001070 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001071 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001072 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001073 }
1074
1075 // --------------------------------------------------------------------
1076
1077 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 * Match one field to another
1079 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001080 * @param string $str string to compare against
1081 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 * @return bool
1083 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001084 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001086 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
1087 ? ($str === $this->_field_data[$field]['postdata'])
1088 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001090
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001091 // --------------------------------------------------------------------
1092
1093 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -03001094 * Differs from another field
1095 *
1096 * @param string
1097 * @param string field
1098 * @return bool
1099 */
1100 public function differs($str, $field)
1101 {
1102 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001104
1105 // --------------------------------------------------------------------
1106
1107 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +02001108 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001109 *
Andrey Andreevd09d6502012-01-03 06:38:33 +02001110 * Check if the input value doesn't already exist
1111 * in the specified database field.
1112 *
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001113 * @param string $str
1114 * @param string $field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001115 * @return bool
1116 */
1117 public function is_unique($str, $field)
1118 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001119 sscanf($field, '%[^.].%[^.]', $table, $field);
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001120 return isset($this->CI->db)
1121 ? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
1122 : FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001123 }
Barry Mienydd671972010-10-04 16:33:58 +02001124
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001126
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 /**
1128 * Minimum Length
1129 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001131 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001133 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001134 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001136 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 {
1138 return FALSE;
1139 }
1140
bjjay8d6c8fe2015-03-09 13:46:06 +08001141 return ($val <= mb_strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 }
Barry Mienydd671972010-10-04 16:33:58 +02001143
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001145
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 /**
1147 * Max Length
1148 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001150 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001152 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001153 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001155 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 {
1157 return FALSE;
1158 }
1159
bjjay8d6c8fe2015-03-09 13:46:06 +08001160 return ($val >= mb_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 }
1178
bjjay8d6c8fe2015-03-09 13:46:06 +08001179 return (mb_strlen($str) === (int) $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 }
Barry Mienydd671972010-10-04 16:33:58 +02001181
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001183
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 /**
Andrey Andreevdaaca882012-11-26 22:16:12 +02001185 * Valid URL
1186 *
1187 * @param string $str
1188 * @return bool
1189 */
1190 public function valid_url($str)
1191 {
1192 if (empty($str))
1193 {
1194 return FALSE;
1195 }
1196 elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
1197 {
1198 if (empty($matches[2]))
1199 {
1200 return FALSE;
1201 }
1202 elseif ( ! in_array($matches[1], array('http', 'https'), TRUE))
1203 {
1204 return FALSE;
1205 }
1206
1207 $str = $matches[2];
1208 }
1209
Andrey Andreev391d3392016-01-30 22:43:41 +02001210 // PHP 7 accepts IPv6 addresses within square brackets as hostnames,
1211 // but it appears that the PR that came in with https://bugs.php.net/bug.php?id=68039
1212 // was never merged into a PHP 5 branch ... https://3v4l.org/8PsSN
1213 if (preg_match('/^\[([^\]]+)\]/', $str, $matches) && ! is_php('7') && filter_var($matches[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== FALSE)
1214 {
1215 $str = 'ipv6.host'.substr($str, strlen($matches[1]) + 2);
1216 }
1217
Andrey Andreevdaaca882012-11-26 22:16:12 +02001218 $str = 'http://'.$str;
1219
1220 // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the
1221 // underscore to be a valid hostname character instead of a dash.
1222 // Reference: https://bugs.php.net/bug.php?id=51192
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001223 if (version_compare(PHP_VERSION, '5.2.13', '==') OR version_compare(PHP_VERSION, '5.3.2', '=='))
Andrey Andreevdaaca882012-11-26 22:16:12 +02001224 {
1225 sscanf($str, 'http://%[^/]', $host);
1226 $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host));
1227 }
1228
1229 return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 }
1231
1232 // --------------------------------------------------------------------
1233
1234 /**
1235 * Valid Email
1236 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 * @param string
1238 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001239 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001240 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 {
Andrey Andreevf9201ae2014-06-01 12:57:30 +03001242 if (function_exists('idn_to_ascii') && $atpos = strpos($str, '@'))
Andrey Andreev95496662014-06-01 00:00:13 +03001243 {
Andrey Andreevf9201ae2014-06-01 12:57:30 +03001244 $str = substr($str, 0, ++$atpos).idn_to_ascii(substr($str, $atpos));
Andrey Andreev95496662014-06-01 00:00:13 +03001245 }
1246
Andrey Andreev580388b2012-06-27 15:43:46 +03001247 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 }
1249
1250 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 /**
1253 * Valid Emails
1254 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 * @param string
1256 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001257 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001258 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 {
1260 if (strpos($str, ',') === FALSE)
1261 {
1262 return $this->valid_email(trim($str));
1263 }
Barry Mienydd671972010-10-04 16:33:58 +02001264
Pascal Kriete14287f32011-02-14 13:39:34 -05001265 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001267 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 {
1269 return FALSE;
1270 }
1271 }
Barry Mienydd671972010-10-04 16:33:58 +02001272
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 return TRUE;
1274 }
1275
1276 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001277
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 /**
1279 * Validate IP Address
1280 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001282 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001283 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001285 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001287 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 }
1289
1290 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001291
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 /**
1293 * Alpha
1294 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 * @param string
1296 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001297 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001298 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001300 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001301 }
Barry Mienydd671972010-10-04 16:33:58 +02001302
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001304
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 /**
1306 * Alpha-numeric
1307 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 * @param string
1309 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001310 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001311 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001313 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 }
Barry Mienydd671972010-10-04 16:33:58 +02001315
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 // --------------------------------------------------------------------
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001317
1318 /**
1319 * Alpha-numeric w/ spaces
1320 *
1321 * @param string
1322 * @return bool
1323 */
1324 public function alpha_numeric_spaces($str)
1325 {
Sajan Parikhdf3bfed2013-02-04 12:25:49 -06001326 return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001327 }
1328
1329 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001330
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 /**
1332 * Alpha-numeric with underscores and dashes
1333 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 * @param string
1335 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001336 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001337 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001339 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 }
Barry Mienydd671972010-10-04 16:33:58 +02001341
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001343
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 /**
1345 * Numeric
1346 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 * @param string
1348 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001349 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001350 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001352 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001353
1354 }
1355
1356 // --------------------------------------------------------------------
1357
Barry Mienydd671972010-10-04 16:33:58 +02001358 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 * Integer
1360 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 * @param string
1362 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001363 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001364 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001366 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1367 }
1368
1369 // --------------------------------------------------------------------
1370
1371 /**
1372 * Decimal number
1373 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001374 * @param string
1375 * @return bool
1376 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001377 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001378 {
1379 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1380 }
1381
1382 // --------------------------------------------------------------------
1383
1384 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001385 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001386 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001387 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001388 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001389 * @return bool
1390 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001391 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001392 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001393 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001394 }
1395
1396 // --------------------------------------------------------------------
1397
1398 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001399 * Equal to or Greater than
1400 *
Nick Busey98c347d2012-02-02 11:07:03 -07001401 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001402 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001403 * @return bool
1404 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001405 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001406 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001407 return is_numeric($str) ? ($str >= $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001408 }
1409
1410 // --------------------------------------------------------------------
1411
1412 /**
1413 * Less than
1414 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001415 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001416 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001417 * @return bool
1418 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001419 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001420 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001421 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001423
1424 // --------------------------------------------------------------------
1425
Barry Mienydd671972010-10-04 16:33:58 +02001426 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001427 * Equal to or Less than
1428 *
Nick Busey98c347d2012-02-02 11:07:03 -07001429 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001430 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001431 * @return bool
1432 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001433 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001434 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001435 return is_numeric($str) ? ($str <= $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 }
1437
1438 // --------------------------------------------------------------------
1439
Barry Mienydd671972010-10-04 16:33:58 +02001440 /**
Lance Vincenteca885d2015-01-28 17:43:23 +08001441 * Value should be within an array of values
1442 *
1443 * @param string
Andrey Andreev5b662e42015-01-29 00:13:54 +02001444 * @param string
Lance Vincenteca885d2015-01-28 17:43:23 +08001445 * @return bool
1446 */
Lance Vincent49f483d2015-01-28 22:46:19 +08001447 public function in_list($value, $list)
Lance Vincenteca885d2015-01-28 17:43:23 +08001448 {
Lance Vincent0b0117c2015-01-28 20:31:42 +08001449 return in_array($value, explode(',', $list), TRUE);
Lance Vincenteca885d2015-01-28 17:43:23 +08001450 }
1451
1452 // --------------------------------------------------------------------
1453
1454 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001455 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001456 *
Barry Mienydd671972010-10-04 16:33:58 +02001457 * @param string
1458 * @return bool
1459 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001460 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001461 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001462 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001463 }
1464
1465 // --------------------------------------------------------------------
1466
1467 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001468 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001469 *
Barry Mienydd671972010-10-04 16:33:58 +02001470 * @param string
1471 * @return bool
1472 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001473 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001474 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001475 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001476 }
1477
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001479
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 /**
1481 * Valid Base64
1482 *
1483 * Tests a string for characters outside of the Base64 alphabet
1484 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1485 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 * @param string
1487 * @return bool
1488 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001489 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 {
Andrey Andreevcd9797a2013-06-28 14:03:48 +03001491 return (base64_encode(base64_decode($str)) === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001492 }
Barry Mienydd671972010-10-04 16:33:58 +02001493
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001495
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 /**
1497 * Prep data for form
1498 *
1499 * This function allows HTML to be safely shown in a form.
1500 * Special characters are converted.
1501 *
Andrey Andreev4f555072016-03-12 17:21:55 +02001502 * @deprecated 3.0.6 Not used anywhere within the framework and pretty much useless
1503 * @param mixed $data Input data
1504 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 */
Andrey Andreev4f555072016-03-12 17:21:55 +02001506 public function prep_for_form($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001507 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001508 if ($this->_safe_form_data === FALSE OR empty($data))
1509 {
1510 return $data;
1511 }
1512
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 if (is_array($data))
1514 {
1515 foreach ($data as $key => $val)
1516 {
1517 $data[$key] = $this->prep_for_form($val);
1518 }
Barry Mienydd671972010-10-04 16:33:58 +02001519
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 return $data;
1521 }
Barry Mienydd671972010-10-04 16:33:58 +02001522
Andrey Andreev901573c2012-01-11 01:40:48 +02001523 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 }
Barry Mienydd671972010-10-04 16:33:58 +02001525
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001527
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 /**
1529 * Prep URL
1530 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 * @param string
1532 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001533 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001534 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001535 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001536 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
1538 return '';
1539 }
Barry Mienydd671972010-10-04 16:33:58 +02001540
Andrey Andreev901573c2012-01-11 01:40:48 +02001541 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001543 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001544 }
Barry Mienydd671972010-10-04 16:33:58 +02001545
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 return $str;
1547 }
Barry Mienydd671972010-10-04 16:33:58 +02001548
Derek Allard2067d1a2008-11-13 22:59:24 +00001549 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001550
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 /**
1552 * Strip Image Tags
1553 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001554 * @param string
1555 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001556 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001557 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001558 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001559 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 }
Barry Mienydd671972010-10-04 16:33:58 +02001561
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001563
Derek Allard2067d1a2008-11-13 22:59:24 +00001564 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 * Convert PHP tags to entities
1566 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001567 * @param string
1568 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001569 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001570 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 {
Andrey Andreev838a9d62012-12-03 14:37:47 +02001572 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001573 }
1574
JonoB099c4782012-03-04 14:37:30 +00001575 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001576
1577 /**
1578 * Reset validation vars
1579 *
1580 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001581 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001582 *
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001583 * @return CI_Form_validation
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001584 */
JonoB883f80f2012-03-05 09:51:27 +00001585 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001586 {
JonoB099c4782012-03-04 14:37:30 +00001587 $this->_field_data = array();
JonoB099c4782012-03-04 14:37:30 +00001588 $this->_error_array = array();
1589 $this->_error_messages = array();
1590 $this->error_string = '';
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001591 return $this;
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001592 }
1593
Derek Allard2067d1a2008-11-13 22:59:24 +00001594}