blob: 555a659596bc96f45f1e74ee0837c08d869f5d9d [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnescccde962011-12-04 00:01:17 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnescccde962011-12-04 00:01:17 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Form Validation Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Validation
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/form_validation.html
37 */
38class CI_Form_validation {
Barry Mienydd671972010-10-04 16:33:58 +020039
Timothy Warren0688ac92012-04-20 10:25:04 -040040 /**
41 * Reference to the CodeIgniter instance
42 *
43 * @var object
44 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +010045 protected $CI;
Derek Allard2067d1a2008-11-13 22:59:24 +000046
Timothy Warren0688ac92012-04-20 10:25:04 -040047 /**
48 * Validation data for the current form submission
49 *
50 * @var array
51 */
Andrey Andreev56454792012-05-17 14:32:19 +030052 protected $_field_data = array();
53
Timothy Warren0688ac92012-04-20 10:25:04 -040054 /**
55 * Validation rules for the current form
56 *
57 * @var array
58 */
Andrey Andreev56454792012-05-17 14:32:19 +030059 protected $_config_rules = array();
60
Timothy Warren0688ac92012-04-20 10:25:04 -040061 /**
62 * Array of validation errors
63 *
64 * @var array
65 */
Andrey Andreev78f55772012-04-03 19:59:08 +030066 protected $_error_array = array();
Andrey Andreev56454792012-05-17 14:32:19 +030067
Timothy Warren0688ac92012-04-20 10:25:04 -040068 /**
69 * Array of custom error messages
70 *
71 * @var array
72 */
Andrey Andreev78f55772012-04-03 19:59:08 +030073 protected $_error_messages = array();
Andrey Andreev56454792012-05-17 14:32:19 +030074
Timothy Warren0688ac92012-04-20 10:25:04 -040075 /**
76 * Start tag for error wrapping
77 *
78 * @var string
79 */
Andrey Andreev78f55772012-04-03 19:59:08 +030080 protected $_error_prefix = '<p>';
Andrey Andreev56454792012-05-17 14:32:19 +030081
Timothy Warren0688ac92012-04-20 10:25:04 -040082 /**
83 * End tag for error wrapping
Andrey Andreev56454792012-05-17 14:32:19 +030084 *
Timothy Warren0688ac92012-04-20 10:25:04 -040085 * @var string
86 */
Andrey Andreev78f55772012-04-03 19:59:08 +030087 protected $_error_suffix = '</p>';
Andrey Andreev56454792012-05-17 14:32:19 +030088
Timothy Warren0688ac92012-04-20 10:25:04 -040089 /**
90 * Custom error message
91 *
92 * @var string
93 */
Andrey Andreev78f55772012-04-03 19:59:08 +030094 protected $error_string = '';
Andrey Andreev56454792012-05-17 14:32:19 +030095
Timothy Warren0688ac92012-04-20 10:25:04 -040096 /**
97 * Whether the form data has been validated as safe
98 *
99 * @var bool
100 */
Andrey Andreev78f55772012-04-03 19:59:08 +0300101 protected $_safe_form_data = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +0300102
Timothy Warren0688ac92012-04-20 10:25:04 -0400103 /**
104 * Custom data to validate
105 *
106 * @var array
107 */
Andrey Andreevcff35802012-11-26 15:49:52 +0200108 public $validation_data = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000109
Timothy Warren0688ac92012-04-20 10:25:04 -0400110 /**
111 * Initialize Form_Validation class
112 *
Andrey Andreev56454792012-05-17 14:32:19 +0300113 * @param array $rules
114 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -0400115 */
Greg Akera9263282010-11-10 15:26:43 -0600116 public function __construct($rules = array())
Barry Mienydd671972010-10-04 16:33:58 +0200117 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200119
Mike Funk326a5e72012-02-24 10:06:28 -0500120 // applies delimiters set in config file.
Mike Funk7f42d062012-03-08 09:00:57 -0500121 if (isset($rules['error_prefix']))
122 {
123 $this->_error_prefix = $rules['error_prefix'];
124 unset($rules['error_prefix']);
125 }
126 if (isset($rules['error_suffix']))
127 {
128 $this->_error_suffix = $rules['error_suffix'];
129 unset($rules['error_suffix']);
130 }
Andrey Andreev31cf46e2012-03-20 15:48:00 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 // Validation rules can be stored in a config file.
133 $this->_config_rules = $rules;
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // Automatically load the form helper
136 $this->CI->load->helper('form');
137
Andrey Andreev901573c2012-01-11 01:40:48 +0200138 log_message('debug', 'Form Validation Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 /**
144 * Set Rules
145 *
146 * This function takes an array of field names and validation
Andrey Andreev4b90a372014-03-10 10:24:24 +0200147 * rules as input, any custom error messages, validates the info,
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600148 * and stores it
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400150 * @param mixed $field
151 * @param string $label
152 * @param mixed $rules
Ahmedul Haque Abid0742fad2014-01-09 07:51:10 +0600153 * @param array $errors
Andrew Podner4296a652012-12-17 07:51:15 -0500154 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 */
Andrey Andreev4b90a372014-03-10 10:24:24 +0200156 public function set_rules($field, $label = '', $rules = array(), $errors = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
158 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +0000159 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200160 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Greg Aker9f9af602010-11-10 15:41:51 -0600162 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
tiyowanc2acb232012-03-14 21:24:00 +0400165 // If an array was passed via the first parameter instead of individual string
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 // values we cycle through it and recursively call this function.
167 if (is_array($field))
168 {
169 foreach ($field as $row)
170 {
171 // Houston, we have a problem...
Andrey Andreev78f55772012-04-03 19:59:08 +0300172 if ( ! isset($row['field'], $row['rules']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
174 continue;
175 }
176
177 // If the field label wasn't passed we use the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300178 $label = isset($row['label']) ? $row['label'] : $row['field'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000179
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600180 // Add the custom error message array
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600181 $errors = (isset($row['errors']) && is_array($row['errors'])) ? $row['errors'] : array();
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // Here we go!
Ahmedul Haque Abidbc1cbad2014-01-09 07:53:34 +0600184 $this->set_rules($row['field'], $label, $row['rules'], $errors);
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300186
Greg Aker9f9af602010-11-10 15:41:51 -0600187 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // No fields? Nothing to do...
Andrey Andreev4b90a372014-03-10 10:24:24 +0200191 if ( ! is_string($field) OR $field === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Greg Aker9f9af602010-11-10 15:41:51 -0600193 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 }
Andrey Andreev4b90a372014-03-10 10:24:24 +0200195 elseif ( ! is_array($rules))
196 {
197 // BC: Convert pipe-separated rules string to an array
198 if (is_string($rules))
199 {
200 $rules = explode('|', $rules);
201 }
202 else
203 {
204 return $this;
205 }
206 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000207
208 // If the field label wasn't passed we use the field name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100209 $label = ($label === '') ? $field : $label;
Derek Allard2067d1a2008-11-13 22:59:24 +0000210
Andrey Andreevfde170c2014-03-10 19:55:11 +0200211 $indexes = array();
212
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200213 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200214 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev4b90a372014-03-10 10:24:24 +0200215 if (($is_array = (bool) preg_match_all('/\[(.*?)\]/', $field, $matches)) === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200216 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200217 sscanf($field, '%[^[][', $indexes[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200219 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100221 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200223 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
228 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300230 'field' => $field,
231 'label' => $label,
232 'rules' => $rules,
Ahmedul Haque Abid7945d302014-01-09 16:50:23 +0600233 'errors' => $errors,
Andrey Andreev56454792012-05-17 14:32:19 +0300234 'is_array' => $is_array,
235 'keys' => $indexes,
236 'postdata' => NULL,
237 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000238 );
Greg Aker9f9af602010-11-10 15:41:51 -0600239
240 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
242
243 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 /**
JonoB099c4782012-03-04 14:37:30 +0000246 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200247 *
JonoB099c4782012-03-04 14:37:30 +0000248 * If an array is set through this method, then this array will
249 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200250 *
251 * Note that if you are validating multiple arrays, then the
252 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000253 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200254 *
255 * @param array $data
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200256 * @return CI_Form_validation
JonoB099c4782012-03-04 14:37:30 +0000257 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200258 public function set_data(array $data)
JonoB099c4782012-03-04 14:37:30 +0000259 {
Andrey Andreeva4712f52014-01-06 11:25:46 +0200260 if ( ! empty($data))
JonoB099c4782012-03-04 14:37:30 +0000261 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200262 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000263 }
Andrey Andreeva89c1da2014-02-08 19:03:35 +0200264
265 return $this;
JonoB099c4782012-03-04 14:37:30 +0000266 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200267
JonoB099c4782012-03-04 14:37:30 +0000268 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000269
270 /**
271 * Set Error Message
272 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300273 * Lets users set their own error messages on the fly. Note:
274 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300276 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500278 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100280 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 if ( ! is_array($lang))
283 {
284 $lang = array($lang => $val);
285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600288 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 /**
294 * Set The Error Delimiter
295 *
296 * Permits a prefix/suffix to be added to each error message
297 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 * @param string
299 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500300 * @return CI_Form_validation
Barry Mienydd671972010-10-04 16:33:58 +0200301 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100302 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 $this->_error_prefix = $prefix;
305 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600306 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308
309 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 /**
312 * Get Error Message
313 *
314 * Gets the error message associated with a particular field
315 *
Andrey Andreeva4712f52014-01-06 11:25:46 +0200316 * @param string $field Field name
317 * @param string $prefix HTML start tag
Andrey Andreev868301a2014-01-06 12:29:50 +0200318 * @param string $suffix HTML end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300319 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200320 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200321 public function error($field, $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200322 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300323 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
325 return '';
326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Alex Bilbied261b1e2012-06-02 11:12:16 +0100328 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 $prefix = $this->_error_prefix;
331 }
332
Alex Bilbied261b1e2012-06-02 11:12:16 +0100333 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 $suffix = $this->_error_suffix;
336 }
337
338 return $prefix.$this->_field_data[$field]['error'].$suffix;
339 }
340
341 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100344 * Get Array of Error Messages
345 *
346 * Returns the error messages as an array
347 *
348 * @return array
349 */
350 public function error_array()
351 {
352 return $this->_error_array;
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 * Error String
359 *
360 * Returns the error messages as a string, wrapped in the error delimiters
361 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 * @param string
363 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300364 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200365 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100366 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
vlakoff35672462013-02-15 01:36:04 +0100368 // No errors, validation passes!
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 if (count($this->_error_array) === 0)
370 {
371 return '';
372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Alex Bilbied261b1e2012-06-02 11:12:16 +0100374 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 $prefix = $this->_error_prefix;
377 }
378
Alex Bilbied261b1e2012-06-02 11:12:16 +0100379 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 $suffix = $this->_error_suffix;
382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 // Generate the error string
385 $str = '';
386 foreach ($this->_error_array as $val)
387 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100388 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 $str .= $prefix.$val.$suffix."\n";
391 }
392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 return $str;
395 }
396
397 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 /**
400 * Run the Validator
401 *
402 * This function does all the work.
403 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400404 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200406 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100407 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500409 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300410 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000411 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
413 return FALSE;
414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 // Does the _field_data array containing the validation rules exist?
417 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200418 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500420 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200421 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 return FALSE;
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200426 if (empty($group))
427 {
428 // Is there a validation rule for the particular URI being accessed?
429 $group = trim($this->CI->uri->ruri_string(), '/');
430 isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method;
431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200433 $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules);
Barry Mienydd671972010-10-04 16:33:58 +0200434
Andrey Andreev901573c2012-01-11 01:40:48 +0200435 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200436 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200438 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 return FALSE;
440 }
441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 // Load the language file containing error messages
444 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200445
Andrey Andreev751f2472012-11-03 18:26:27 +0200446 // Cycle through the rules for each field and match the corresponding $validation_data item
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200448 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200449 // Fetch the data from the validation_data array item and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 // Depending on whether the field name is an array or a string will determine where we get it from.
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200451 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
JonoB099c4782012-03-04 14:37:30 +0000453 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
Andrey Andreevfff6c2a2012-05-13 22:15:23 +0300455 elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300457 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Andrey Andreev751f2472012-11-03 18:26:27 +0200461 // Execute validation rules
462 // Note: A second foreach (for now) is required in order to avoid false-positives
463 // for rules like 'matches', which correlate to other validation fields.
464 foreach ($this->_field_data as $field => $row)
465 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300466 // Don't try to validate if we have no rules set
467 if (empty($row['rules']))
468 {
469 continue;
470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Andrey Andreev4b90a372014-03-10 10:24:24 +0200472 $this->_execute($row, $row['rules'], $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474
475 // Did we end up with any errors?
476 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 if ($total_errors > 0)
478 {
479 $this->_safe_form_data = TRUE;
480 }
481
482 // Now we need to re-set the POST data with the new, processed data
483 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200484
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200485 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
487
488 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 /**
491 * Traverse a multidimensional $_POST array index until the data is found
492 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 * @param array
494 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300495 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200497 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100498 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200500 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200502 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Andrey Andreev2d48b4f2012-11-23 17:33:21 +0200505 // NULL must be returned for empty fields
Andrey Andreev44c34632012-11-23 18:46:34 +0200506 return ($array === '') ? NULL : $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508
509 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 /**
512 * Re-populate the _POST array with our finalized and processed data
513 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200514 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200515 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100516 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 foreach ($this->_field_data as $field => $row)
519 {
vlakoff1228fe22013-01-14 01:30:09 +0100520 if ($row['postdata'] !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200522 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 if (isset($_POST[$row['field']]))
525 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200526 $_POST[$row['field']] = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
528 }
529 else
530 {
Derek Jones63eeae32009-02-10 19:08:56 +0000531 // start with a reference
532 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Jones63eeae32009-02-10 19:08:56 +0000534 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200535 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
Derek Jones63eeae32009-02-10 19:08:56 +0000537 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
539 else
540 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 foreach ($row['keys'] as $val)
542 {
Derek Jones63eeae32009-02-10 19:08:56 +0000543 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
545 }
Derek Jones63eeae32009-02-10 19:08:56 +0000546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000548 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 $array = array();
550 foreach ($row['postdata'] as $k => $v)
551 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200552 $array[$k] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
Derek Jones63eeae32009-02-10 19:08:56 +0000554
555 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 }
557 else
Derek Jones63eeae32009-02-10 19:08:56 +0000558 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200559 $post_ref = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
562 }
563 }
564 }
565
566 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 /**
569 * Executes the Validation routines
570 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * @param array
572 * @param array
573 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300574 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200576 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100577 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
579 // If the $_POST data is an array we will run a recursive call
580 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200581 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 foreach ($postdata as $key => $val)
583 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300584 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 return;
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 // If the field is blank, but NOT required, no further tests are necessary
591 $callback = FALSE;
Hashem Qolami05370bf2013-07-22 01:52:04 +0430592 if ( ! in_array('required', $rules) && ($postdata === NULL OR $postdata === ''))
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
594 // Before we bail out, does the rule contain a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200595 foreach ($rules as &$rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200597 if (is_string($rule))
598 {
599 if (strncmp($rule, 'callback_', 9) === 0)
600 {
601 $callback = TRUE;
602 $rules = array(1 => $rule);
603 break;
604 }
605 }
606 elseif (is_callable($rule))
607 {
608 $callback = TRUE;
609 $rules = array(1 => $rule);
610 break;
611 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
Andrey Andreev4b90a372014-03-10 10:24:24 +0200613
614 if ( ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 {
616 return;
617 }
618 }
619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 // Isset Test. Typically this rule will only apply to checkboxes.
Andrey Andreev4b90a372014-03-10 10:24:24 +0200621 if (($postdata === NULL OR $postdata === '') && ! $callback)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
624 {
625 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300626 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200627
Ahmedul Haque Abide9b0ccc2014-01-09 15:58:51 +0600628 // Check if a custom message is defined
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600629 if (isset($this->_field_data[$row['field']]['errors'][$type]))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600630 {
Ahmedul Haque Abidea294882014-01-09 16:01:31 +0600631 $line = $this->_field_data[$row['field']]['errors'][$type];
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600632 }
633 elseif (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
635 $line = $this->_error_messages[$type];
636 }
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200637 elseif (FALSE === ($line = $this->CI->lang->line('form_validation_'.$type))
638 // DEPRECATED support for non-prefixed keys
639 && FALSE === ($line = $this->CI->lang->line($type, FALSE)))
Andrey Andreev56454792012-05-17 14:32:19 +0300640 {
641 $line = 'The field was not set';
642 }
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600645 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
Derek Allard2067d1a2008-11-13 22:59:24 +0000646
647 // Save the error message
648 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 if ( ! isset($this->_error_array[$row['field']]))
651 {
652 $this->_error_array[$row['field']] = $message;
653 }
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 return;
657 }
658
659 // --------------------------------------------------------------------
660
661 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300662 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
664 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 // We set the $postdata variable with the current data in our master array so that
667 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100668 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 {
670 // We shouldn't need this safety, but just in case there isn't an array index
671 // associated with this cycle we'll bail out
672 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
673 {
674 continue;
675 }
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
678 $_in_array = TRUE;
679 }
680 else
681 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300682 // If we get an array field, but it's not expected - then it is most likely
683 // somebody messing with the form on the client side, so we'll just consider
684 // it an empty field
685 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
Andrey Andreev4b90a372014-03-10 10:24:24 +0200686 ? NULL
687 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 }
689
Barry Mienydd671972010-10-04 16:33:58 +0200690 // Is the rule a callback?
Andrey Andreev4b90a372014-03-10 10:24:24 +0200691 $callback = $callable = FALSE;
692 if (is_string($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200694 if (strpos($rule, 'callback_') === 0)
695 {
696 $rule = substr($rule, 9);
697 $callback = TRUE;
698 }
699 }
700 elseif (is_callable($rule))
701 {
702 $callable = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 }
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 // Strip the parameter (if exists) from the rule
706 // Rules can contain a parameter: max_length[5]
707 $param = FALSE;
Andrey Andreev4b90a372014-03-10 10:24:24 +0200708 if ( ! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 {
Andrey Andreevef758bd2012-11-15 12:24:52 +0200710 $rule = $match[1];
711 $param = $match[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 // Call the function that corresponds to the rule
Andrey Andreev4b90a372014-03-10 10:24:24 +0200715 if ($callback OR $callable)
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200717 if ($callback)
Barry Mienydd671972010-10-04 16:33:58 +0200718 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200719 if ( ! method_exists($this->CI, $rule))
720 {
721 log_message('debug', 'Unable to find callback validation rule: '.$rule);
722 $result = FALSE;
723 }
724 else
725 {
726 // Run the function and grab the result
727 $result = $this->CI->$rule($postdata, $param);
728 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200730 else
731 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200732 $result = is_array($rule)
733 ? $rule[0]->{$rule[1]}($postdata, $param)
734 : $rule($postdata, $param);
Andrey Andreev901573c2012-01-11 01:40:48 +0200735 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000736
737 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200738 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300740 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 }
742 else
743 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300744 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 }
Barry Mienydd671972010-10-04 16:33:58 +0200746
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200748 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000750 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 }
752 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300753 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200754 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300755 // If our own wrapper function doesn't exist we see if a native PHP function does.
756 // Users can use any native PHP function call that has one param.
757 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
Andrey Andreev4b90a372014-03-10 10:24:24 +0200759 // Native PHP functions issue warnings if you pass them more parameters than they use
Andrey Andreev320d37c2012-04-03 20:21:39 +0300760 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200761
Andrey Andreev78f55772012-04-03 19:59:08 +0300762 if ($_in_array === TRUE)
763 {
764 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 }
patwork02404a12011-04-08 15:45:46 +0200766 else
767 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300768 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200769 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200771 else
772 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300773 log_message('debug', 'Unable to find validation rule: '.$rule);
774 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200775 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300776 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 else
778 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 $result = $this->$rule($postdata, $param);
780
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200781 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300783 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
785 else
786 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300787 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 }
789 }
Barry Mienydd671972010-10-04 16:33:58 +0200790
Andrey Andreev901573c2012-01-11 01:40:48 +0200791 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200793 {
Andrey Andreev69c56442014-05-20 09:58:08 +0300794 // Callable rules don't have named error messages
795 if ( ! is_callable($rule))
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600796 {
Andrey Andreev69c56442014-05-20 09:58:08 +0300797 // Check if a custom message is defined
798 if (isset($this->_field_data[$row['field']]['errors'][$rule]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
Andrey Andreev69c56442014-05-20 09:58:08 +0300800 $line = $this->_field_data[$row['field']]['errors'][$rule];
Barry Mienydd671972010-10-04 16:33:58 +0200801 }
Andrey Andreev69c56442014-05-20 09:58:08 +0300802 elseif ( ! isset($this->_error_messages[$rule]))
803 {
804 if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule))
805 // DEPRECATED support for non-prefixed keys
806 && FALSE === ($line = $this->CI->lang->line($rule, FALSE)))
807 {
808 $line = 'Unable to access an error message corresponding to your field name.';
809 }
810 }
811 else
812 {
813 $line = $this->_error_messages[$rule];
814 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 }
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200818 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200819 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
Pascal Krietec1895832009-10-13 12:56:43 +0000821 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 }
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600825 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000826
827 // Save the error message
828 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200829
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 if ( ! isset($this->_error_array[$row['field']]))
831 {
832 $this->_error_array[$row['field']] = $message;
833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 return;
836 }
837 }
838 }
839
840 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 /**
843 * Translate a field name
844 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 * @param string the field name
846 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200847 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100848 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 {
850 // Do we need to translate the field name?
851 // We look for the prefix lang: to determine this
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200852 if (sscanf($fieldname, 'lang:%s', $line) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500854 // Were we able to translate the field name? If not we use $line
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200855 if (FALSE === ($fieldname = $this->CI->lang->line('form_validation_'.$line))
856 // DEPRECATED support for non-prefixed keys
857 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 {
859 return $line;
860 }
861 }
862
863 return $fieldname;
864 }
865
866 // --------------------------------------------------------------------
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200867
Eric Roberts41cc0902012-01-24 00:59:44 -0600868 /**
869 * Build an error message using the field and param.
870 *
871 * @param string The error message line
872 * @param string A field's human name
873 * @param mixed A rule's optional parameter
874 * @return string
875 */
876 protected function _build_error_msg($line, $field = '', $param = '')
877 {
878 // Check for %s in the string for legacy support.
Eric Roberts24a13f52012-12-12 07:09:42 -0600879 if (strpos($line, '%s') !== FALSE)
Eric Roberts41cc0902012-01-24 00:59:44 -0600880 {
881 return sprintf($line, $field, $param);
882 }
Andrew Podner4296a652012-12-17 07:51:15 -0500883
Eric Roberts41cc0902012-01-24 00:59:44 -0600884 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
885 }
886
887 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 /**
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530890 * Checks if the rule is present within the validator
891 *
892 * Permits you to check if a rule is present within the validator
893 *
894 * @param string the field name
895 * @return bool
896 */
897 public function has_rule($field)
898 {
899 return isset($this->_field_data[$field]);
900 }
901
902 // --------------------------------------------------------------------
903
904 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 * Get the value from a form
906 *
907 * Permits you to repopulate a form field with the value it was submitted
908 * with, or, if that value doesn't exist, with the default
909 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 * @param string the field name
911 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200912 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200913 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100914 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200916 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 {
918 return $default;
919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920
Phil Sturgeon5c561802011-01-05 16:31:59 +0000921 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600922 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000923 if (is_array($this->_field_data[$field]['postdata']))
924 {
925 return array_shift($this->_field_data[$field]['postdata']);
926 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 return $this->_field_data[$field]['postdata'];
929 }
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 /**
934 * Set Select
935 *
936 * Enables pull-down lists to be set to the value the user
937 * selected in the event of an error
938 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * @param string
940 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400941 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200943 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100944 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200945 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200946 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200948 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300952 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 if (is_array($field))
954 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300955 // Note: in_array('', array(0)) returns TRUE, do not use it
956 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300958 if ($value === $v)
959 {
960 return ' selected="selected"';
961 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300963
964 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100966 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200968 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 return ' selected="selected"';
972 }
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200975
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 /**
977 * Set Radio
978 *
979 * Enables radio buttons to be set to the value the user
980 * selected in the event of an error
981 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 * @param string
983 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400984 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200986 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100987 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200989 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200991 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 }
Barry Mienydd671972010-10-04 16:33:58 +0200993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300995 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 if (is_array($field))
997 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300998 // Note: in_array('', array(0)) returns TRUE, do not use it
999 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Andrey Andreeva587a932013-10-23 19:57:46 +03001001 if ($value === $v)
1002 {
1003 return ' checked="checked"';
1004 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 }
Andrey Andreeva587a932013-10-23 19:57:46 +03001006
1007 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 }
Alex Bilbied261b1e2012-06-02 11:12:16 +01001009 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001011 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 }
Barry Mienydd671972010-10-04 16:33:58 +02001013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 return ' checked="checked"';
1015 }
Barry Mienydd671972010-10-04 16:33:58 +02001016
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 /**
1020 * Set Checkbox
1021 *
1022 * Enables checkboxes to be set to the value the user
1023 * selected in the event of an error
1024 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 * @param string
1026 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001027 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001029 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001030 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001032 // Logic is exactly the same as for radio fields
1033 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 }
Barry Mienydd671972010-10-04 16:33:58 +02001035
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 /**
1039 * Required
1040 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 * @param string
1042 * @return bool
1043 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001044 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001046 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 }
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001052 * Performs a Regular Expression match test.
1053 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001054 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001055 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001056 * @return bool
1057 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001058 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001059 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001060 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001061 }
1062
1063 // --------------------------------------------------------------------
1064
1065 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 * Match one field to another
1067 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001068 * @param string $str string to compare against
1069 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 * @return bool
1071 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001072 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001074 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
1075 ? ($str === $this->_field_data[$field]['postdata'])
1076 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001078
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001079 // --------------------------------------------------------------------
1080
1081 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -03001082 * Differs from another field
1083 *
1084 * @param string
1085 * @param string field
1086 * @return bool
1087 */
1088 public function differs($str, $field)
1089 {
1090 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001092
1093 // --------------------------------------------------------------------
1094
1095 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +02001096 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001097 *
Andrey Andreevd09d6502012-01-03 06:38:33 +02001098 * Check if the input value doesn't already exist
1099 * in the specified database field.
1100 *
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001101 * @param string $str
1102 * @param string $field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001103 * @return bool
1104 */
1105 public function is_unique($str, $field)
1106 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001107 sscanf($field, '%[^.].%[^.]', $table, $field);
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001108 return isset($this->CI->db)
1109 ? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
1110 : FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001111 }
Barry Mienydd671972010-10-04 16:33:58 +02001112
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 /**
1116 * Minimum Length
1117 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001119 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001121 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001122 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001124 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 {
1126 return FALSE;
1127 }
1128
Andrey Andreev78f55772012-04-03 19:59:08 +03001129 return (MB_ENABLED === TRUE)
1130 ? ($val <= mb_strlen($str))
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001131 : ($val <= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 }
Barry Mienydd671972010-10-04 16:33:58 +02001133
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001135
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 /**
1137 * Max Length
1138 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001140 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001142 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001143 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001145 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 {
1147 return FALSE;
1148 }
1149
Andrey Andreev78f55772012-04-03 19:59:08 +03001150 return (MB_ENABLED === TRUE)
1151 ? ($val >= mb_strlen($str))
1152 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 }
Barry Mienydd671972010-10-04 16:33:58 +02001154
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001156
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 /**
1158 * Exact Length
1159 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001161 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001163 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001164 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001166 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 {
1168 return FALSE;
1169 }
1170
Andrey Andreev78f55772012-04-03 19:59:08 +03001171 return (MB_ENABLED === TRUE)
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001172 ? (mb_strlen($str) === (int) $val)
1173 : (strlen($str) === (int) $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 }
Barry Mienydd671972010-10-04 16:33:58 +02001175
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001177
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 /**
Andrey Andreevdaaca882012-11-26 22:16:12 +02001179 * Valid URL
1180 *
1181 * @param string $str
1182 * @return bool
1183 */
1184 public function valid_url($str)
1185 {
1186 if (empty($str))
1187 {
1188 return FALSE;
1189 }
1190 elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
1191 {
1192 if (empty($matches[2]))
1193 {
1194 return FALSE;
1195 }
1196 elseif ( ! in_array($matches[1], array('http', 'https'), TRUE))
1197 {
1198 return FALSE;
1199 }
1200
1201 $str = $matches[2];
1202 }
1203
1204 $str = 'http://'.$str;
1205
1206 // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the
1207 // underscore to be a valid hostname character instead of a dash.
1208 // Reference: https://bugs.php.net/bug.php?id=51192
Andrey Andreev9a0e0c72014-04-09 15:10:27 +03001209 if (version_compare(PHP_VERSION, '5.2.13', '==') OR version_compare(PHP_VERSION, '5.3.2', '=='))
Andrey Andreevdaaca882012-11-26 22:16:12 +02001210 {
1211 sscanf($str, 'http://%[^/]', $host);
1212 $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host));
1213 }
1214
1215 return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 }
1217
1218 // --------------------------------------------------------------------
1219
1220 /**
1221 * Valid Email
1222 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 * @param string
1224 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001225 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001226 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 {
Andrey Andreev95496662014-06-01 00:00:13 +03001228 if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))
1229 {
1230 $email = substr($email, 0, ++$atpos).idn_to_ascii(substr($email, $atpos));
1231 }
1232
Andrey Andreev580388b2012-06-27 15:43:46 +03001233 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 }
1235
1236 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 /**
1239 * Valid Emails
1240 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 * @param string
1242 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001243 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001244 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 {
1246 if (strpos($str, ',') === FALSE)
1247 {
1248 return $this->valid_email(trim($str));
1249 }
Barry Mienydd671972010-10-04 16:33:58 +02001250
Pascal Kriete14287f32011-02-14 13:39:34 -05001251 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001253 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 {
1255 return FALSE;
1256 }
1257 }
Barry Mienydd671972010-10-04 16:33:58 +02001258
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 return TRUE;
1260 }
1261
1262 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001263
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 /**
1265 * Validate IP Address
1266 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001268 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001269 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001271 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001273 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 }
1275
1276 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001277
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 /**
1279 * Alpha
1280 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 * @param string
1282 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001283 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001284 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001286 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 }
Barry Mienydd671972010-10-04 16:33:58 +02001288
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 /**
1292 * Alpha-numeric
1293 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001294 * @param string
1295 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001296 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001297 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001299 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 }
Barry Mienydd671972010-10-04 16:33:58 +02001301
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 // --------------------------------------------------------------------
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001303
1304 /**
1305 * Alpha-numeric w/ spaces
1306 *
1307 * @param string
1308 * @return bool
1309 */
1310 public function alpha_numeric_spaces($str)
1311 {
Sajan Parikhdf3bfed2013-02-04 12:25:49 -06001312 return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001313 }
1314
1315 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001316
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 /**
1318 * Alpha-numeric with underscores and dashes
1319 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 * @param string
1321 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001322 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001323 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001325 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 }
Barry Mienydd671972010-10-04 16:33:58 +02001327
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001329
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 /**
1331 * Numeric
1332 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 * @param string
1334 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001335 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001336 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001338 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001339
1340 }
1341
1342 // --------------------------------------------------------------------
1343
Barry Mienydd671972010-10-04 16:33:58 +02001344 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 * Integer
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 integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001352 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1353 }
1354
1355 // --------------------------------------------------------------------
1356
1357 /**
1358 * Decimal number
1359 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001360 * @param string
1361 * @return bool
1362 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001363 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001364 {
1365 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1366 }
1367
1368 // --------------------------------------------------------------------
1369
1370 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001371 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001372 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001373 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001374 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001375 * @return bool
1376 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001377 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001378 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001379 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001380 }
1381
1382 // --------------------------------------------------------------------
1383
1384 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001385 * Equal to or Greater than
1386 *
Nick Busey98c347d2012-02-02 11:07:03 -07001387 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001388 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001389 * @return bool
1390 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001391 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001392 {
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 /**
1399 * Less than
1400 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001401 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001402 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001403 * @return bool
1404 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001405 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001406 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001407 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001409
1410 // --------------------------------------------------------------------
1411
Barry Mienydd671972010-10-04 16:33:58 +02001412 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001413 * Equal to or Less than
1414 *
Nick Busey98c347d2012-02-02 11:07:03 -07001415 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001416 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001417 * @return bool
1418 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001419 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001420 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001421 return is_numeric($str) ? ($str <= $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 }
1423
1424 // --------------------------------------------------------------------
1425
Barry Mienydd671972010-10-04 16:33:58 +02001426 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001427 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001428 *
Barry Mienydd671972010-10-04 16:33:58 +02001429 * @param string
1430 * @return bool
1431 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001432 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001433 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001434 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001435 }
1436
1437 // --------------------------------------------------------------------
1438
1439 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001440 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001441 *
Barry Mienydd671972010-10-04 16:33:58 +02001442 * @param string
1443 * @return bool
1444 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001445 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001446 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001447 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001448 }
1449
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001451
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 /**
1453 * Valid Base64
1454 *
1455 * Tests a string for characters outside of the Base64 alphabet
1456 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1457 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 * @param string
1459 * @return bool
1460 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001461 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001462 {
Andrey Andreevcd9797a2013-06-28 14:03:48 +03001463 return (base64_encode(base64_decode($str)) === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 }
Barry Mienydd671972010-10-04 16:33:58 +02001465
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001467
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 /**
1469 * Prep data for form
1470 *
1471 * This function allows HTML to be safely shown in a form.
1472 * Special characters are converted.
1473 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 * @param string
1475 * @return string
1476 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001477 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001479 if ($this->_safe_form_data === FALSE OR empty($data))
1480 {
1481 return $data;
1482 }
1483
Derek Allard2067d1a2008-11-13 22:59:24 +00001484 if (is_array($data))
1485 {
1486 foreach ($data as $key => $val)
1487 {
1488 $data[$key] = $this->prep_for_form($val);
1489 }
Barry Mienydd671972010-10-04 16:33:58 +02001490
Derek Allard2067d1a2008-11-13 22:59:24 +00001491 return $data;
1492 }
Barry Mienydd671972010-10-04 16:33:58 +02001493
Andrey Andreev901573c2012-01-11 01:40:48 +02001494 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001495 }
Barry Mienydd671972010-10-04 16:33:58 +02001496
Derek Allard2067d1a2008-11-13 22:59:24 +00001497 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001498
Derek Allard2067d1a2008-11-13 22:59:24 +00001499 /**
1500 * Prep URL
1501 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 * @param string
1503 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001504 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001505 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001507 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 {
1509 return '';
1510 }
Barry Mienydd671972010-10-04 16:33:58 +02001511
Andrey Andreev901573c2012-01-11 01:40:48 +02001512 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001514 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 }
Barry Mienydd671972010-10-04 16:33:58 +02001516
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 return $str;
1518 }
Barry Mienydd671972010-10-04 16:33:58 +02001519
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001521
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 /**
1523 * Strip Image Tags
1524 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 * @param string
1526 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001527 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001528 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001530 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 }
Barry Mienydd671972010-10-04 16:33:58 +02001532
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001534
Derek Allard2067d1a2008-11-13 22:59:24 +00001535 /**
1536 * XSS Clean
1537 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 * @param string
1539 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001540 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001541 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 {
Derek Jones5640a712010-04-23 11:22:40 -05001543 return $this->CI->security->xss_clean($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 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001547
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 /**
1549 * Convert PHP tags to entities
1550 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 * @param string
1552 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001553 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001554 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 {
Andrey Andreev838a9d62012-12-03 14:37:47 +02001556 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001557 }
1558
JonoB099c4782012-03-04 14:37:30 +00001559 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001560
1561 /**
1562 * Reset validation vars
1563 *
1564 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001565 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001566 *
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001567 * @return CI_Form_validation
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001568 */
JonoB883f80f2012-03-05 09:51:27 +00001569 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001570 {
JonoB099c4782012-03-04 14:37:30 +00001571 $this->_field_data = array();
1572 $this->_config_rules = array();
1573 $this->_error_array = array();
1574 $this->_error_messages = array();
1575 $this->error_string = '';
Andrey Andreeva89c1da2014-02-08 19:03:35 +02001576 return $this;
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001577 }
1578
Derek Allard2067d1a2008-11-13 22:59:24 +00001579}
Derek Allard2067d1a2008-11-13 22:59:24 +00001580
1581/* End of file Form_validation.php */
Eric Roberts24a13f52012-12-12 07:09:42 -06001582/* Location: ./system/libraries/Form_validation.php */