blob: 3ec03ae16e1f2d6fb4fbbe9a4f44ad399921c0cf [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600147 * rules as input, any custom error messages, validates the info,
148 * 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 */
Ahmedul Haque Abid0742fad2014-01-09 07:51:10 +0600156 public function set_rules($field, $label = '', $rules = '', $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 Abid0742fad2014-01-09 07:51:10 +0600181 $errors = (isset($row['error_msg']) && is_array($row['error_msg']) ) ? $row['error_msg'] : 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
Kevin Wood-Friend6a56d502012-06-12 09:54:01 -0400190 // Convert an array of rules to a string
191 if (is_array($rules))
192 {
193 $rules = implode('|', $rules);
194 }
195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 // No fields? Nothing to do...
Alex Bilbied261b1e2012-06-02 11:12:16 +0100197 if ( ! is_string($field) OR ! is_string($rules) OR $field === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Greg Aker9f9af602010-11-10 15:41:51 -0600199 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
201
202 // If the field label wasn't passed we use the field name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100203 $label = ($label === '') ? $field : $label;
Derek Allard2067d1a2008-11-13 22:59:24 +0000204
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200205 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200206 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200207 $indexes = array();
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200208 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200209 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200210 sscanf($field, '%[^[][', $indexes[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000211
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200212 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100214 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200216 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 $is_array = TRUE;
221 }
222 else
223 {
Barry Mienydd671972010-10-04 16:33:58 +0200224 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
227 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300229 'field' => $field,
230 'label' => $label,
231 'rules' => $rules,
Ahmedul Haque Abid0742fad2014-01-09 07:51:10 +0600232 'error_msg' => $errors,
Andrey Andreev56454792012-05-17 14:32:19 +0300233 'is_array' => $is_array,
234 'keys' => $indexes,
235 'postdata' => NULL,
236 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000237 );
Greg Aker9f9af602010-11-10 15:41:51 -0600238
239 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241
242 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 /**
JonoB099c4782012-03-04 14:37:30 +0000245 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200246 *
JonoB099c4782012-03-04 14:37:30 +0000247 * If an array is set through this method, then this array will
248 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200249 *
250 * Note that if you are validating multiple arrays, then the
251 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000252 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200253 *
254 * @param array $data
255 * @return void
JonoB099c4782012-03-04 14:37:30 +0000256 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200257 public function set_data(array $data)
JonoB099c4782012-03-04 14:37:30 +0000258 {
Andrey Andreeva4712f52014-01-06 11:25:46 +0200259 if ( ! empty($data))
JonoB099c4782012-03-04 14:37:30 +0000260 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200261 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000262 }
263 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200264
JonoB099c4782012-03-04 14:37:30 +0000265 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000266
267 /**
268 * Set Error Message
269 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300270 * Lets users set their own error messages on the fly. Note:
271 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300273 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500275 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100277 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
279 if ( ! is_array($lang))
280 {
281 $lang = array($lang => $val);
282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600285 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 /**
291 * Set The Error Delimiter
292 *
293 * Permits a prefix/suffix to be added to each error message
294 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 * @param string
296 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500297 * @return CI_Form_validation
Barry Mienydd671972010-10-04 16:33:58 +0200298 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100299 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 $this->_error_prefix = $prefix;
302 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600303 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
305
306 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 /**
309 * Get Error Message
310 *
311 * Gets the error message associated with a particular field
312 *
Andrey Andreeva4712f52014-01-06 11:25:46 +0200313 * @param string $field Field name
314 * @param string $prefix HTML start tag
Andrey Andreev868301a2014-01-06 12:29:50 +0200315 * @param string $suffix HTML end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300316 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200317 */
Andrey Andreeva4712f52014-01-06 11:25:46 +0200318 public function error($field, $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200319 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300320 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
322 return '';
323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Alex Bilbied261b1e2012-06-02 11:12:16 +0100325 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
327 $prefix = $this->_error_prefix;
328 }
329
Alex Bilbied261b1e2012-06-02 11:12:16 +0100330 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 $suffix = $this->_error_suffix;
333 }
334
335 return $prefix.$this->_field_data[$field]['error'].$suffix;
336 }
337
338 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100341 * Get Array of Error Messages
342 *
343 * Returns the error messages as an array
344 *
345 * @return array
346 */
347 public function error_array()
348 {
349 return $this->_error_array;
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * Error String
356 *
357 * Returns the error messages as a string, wrapped in the error delimiters
358 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 * @param string
360 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300361 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200362 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100363 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
vlakoff35672462013-02-15 01:36:04 +0100365 // No errors, validation passes!
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 if (count($this->_error_array) === 0)
367 {
368 return '';
369 }
Barry Mienydd671972010-10-04 16:33:58 +0200370
Alex Bilbied261b1e2012-06-02 11:12:16 +0100371 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 $prefix = $this->_error_prefix;
374 }
375
Alex Bilbied261b1e2012-06-02 11:12:16 +0100376 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 $suffix = $this->_error_suffix;
379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 // Generate the error string
382 $str = '';
383 foreach ($this->_error_array as $val)
384 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100385 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
387 $str .= $prefix.$val.$suffix."\n";
388 }
389 }
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 return $str;
392 }
393
394 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 /**
397 * Run the Validator
398 *
399 * This function does all the work.
400 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400401 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200403 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100404 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500406 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300407 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000408 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
410 return FALSE;
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // Does the _field_data array containing the validation rules exist?
414 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200415 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500417 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200418 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
420 return FALSE;
421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200423 if (empty($group))
424 {
425 // Is there a validation rule for the particular URI being accessed?
426 $group = trim($this->CI->uri->ruri_string(), '/');
427 isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method;
428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Andrey Andreev3b2803e2014-01-07 14:46:38 +0200430 $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules);
Barry Mienydd671972010-10-04 16:33:58 +0200431
Andrey Andreev901573c2012-01-11 01:40:48 +0200432 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200433 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200435 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 return FALSE;
437 }
438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 // Load the language file containing error messages
441 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200442
Andrey Andreev751f2472012-11-03 18:26:27 +0200443 // Cycle through the rules for each field and match the corresponding $validation_data item
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200445 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200446 // Fetch the data from the validation_data array item and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 // 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 +0200448 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
JonoB099c4782012-03-04 14:37:30 +0000450 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
Andrey Andreevfff6c2a2012-05-13 22:15:23 +0300452 elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300454 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Andrey Andreev751f2472012-11-03 18:26:27 +0200458 // Execute validation rules
459 // Note: A second foreach (for now) is required in order to avoid false-positives
460 // for rules like 'matches', which correlate to other validation fields.
461 foreach ($this->_field_data as $field => $row)
462 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300463 // Don't try to validate if we have no rules set
464 if (empty($row['rules']))
465 {
466 continue;
467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
469 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
471
472 // Did we end up with any errors?
473 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 if ($total_errors > 0)
475 {
476 $this->_safe_form_data = TRUE;
477 }
478
479 // Now we need to re-set the POST data with the new, processed data
480 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200481
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200482 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
484
485 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 /**
488 * Traverse a multidimensional $_POST array index until the data is found
489 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @param array
491 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300492 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200494 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100495 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200497 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200499 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Barry Mienydd671972010-10-04 16:33:58 +0200501
Andrey Andreev2d48b4f2012-11-23 17:33:21 +0200502 // NULL must be returned for empty fields
Andrey Andreev44c34632012-11-23 18:46:34 +0200503 return ($array === '') ? NULL : $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
505
506 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 /**
509 * Re-populate the _POST array with our finalized and processed data
510 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200511 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200512 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100513 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
515 foreach ($this->_field_data as $field => $row)
516 {
vlakoff1228fe22013-01-14 01:30:09 +0100517 if ($row['postdata'] !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200519 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
521 if (isset($_POST[$row['field']]))
522 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200523 $_POST[$row['field']] = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
525 }
526 else
527 {
Derek Jones63eeae32009-02-10 19:08:56 +0000528 // start with a reference
529 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Jones63eeae32009-02-10 19:08:56 +0000531 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200532 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Derek Jones63eeae32009-02-10 19:08:56 +0000534 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536 else
537 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 foreach ($row['keys'] as $val)
539 {
Derek Jones63eeae32009-02-10 19:08:56 +0000540 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
542 }
Derek Jones63eeae32009-02-10 19:08:56 +0000543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000545 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 $array = array();
547 foreach ($row['postdata'] as $k => $v)
548 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200549 $array[$k] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
Derek Jones63eeae32009-02-10 19:08:56 +0000551
552 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
554 else
Derek Jones63eeae32009-02-10 19:08:56 +0000555 {
Andrey Andreevc2268712013-02-08 22:10:23 +0200556 $post_ref = $row['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 }
559 }
560 }
561 }
562
563 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 /**
566 * Executes the Validation routines
567 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 * @param array
569 * @param array
570 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300571 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200573 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100574 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 {
576 // If the $_POST data is an array we will run a recursive call
577 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200578 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 foreach ($postdata as $key => $val)
580 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300581 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 return;
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 // If the field is blank, but NOT required, no further tests are necessary
588 $callback = FALSE;
Hashem Qolami05370bf2013-07-22 01:52:04 +0430589 if ( ! in_array('required', $rules) && ($postdata === NULL OR $postdata === ''))
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
591 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200592 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
594 $callback = TRUE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300595 $rules = array(1 => $match[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 }
597 else
598 {
599 return;
600 }
601 }
602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 // Isset Test. Typically this rule will only apply to checkboxes.
Hashem Qolami05370bf2013-07-22 01:52:04 +0430604 if (($postdata === NULL OR $postdata === '') && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
606 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
607 {
608 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300609 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200610
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600611 // Check if a custom message defined, else use default one
612 if (isset($this->_field_data[$row['field']]['error_msg'][$type]))
613 {
614 $line = $this->_field_data[$row['field']]['error_msg'][$type];
615 }
616 elseif (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
618 $line = $this->_error_messages[$type];
619 }
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200620 elseif (FALSE === ($line = $this->CI->lang->line('form_validation_'.$type))
621 // DEPRECATED support for non-prefixed keys
622 && FALSE === ($line = $this->CI->lang->line($type, FALSE)))
Andrey Andreev56454792012-05-17 14:32:19 +0300623 {
624 $line = 'The field was not set';
625 }
Barry Mienydd671972010-10-04 16:33:58 +0200626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600628 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
Derek Allard2067d1a2008-11-13 22:59:24 +0000629
630 // Save the error message
631 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 if ( ! isset($this->_error_array[$row['field']]))
634 {
635 $this->_error_array[$row['field']] = $message;
636 }
637 }
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 return;
640 }
641
642 // --------------------------------------------------------------------
643
644 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300645 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
647 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 // We set the $postdata variable with the current data in our master array so that
650 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100651 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 {
653 // We shouldn't need this safety, but just in case there isn't an array index
654 // associated with this cycle we'll bail out
655 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
656 {
657 continue;
658 }
Barry Mienydd671972010-10-04 16:33:58 +0200659
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
661 $_in_array = TRUE;
662 }
663 else
664 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300665 // If we get an array field, but it's not expected - then it is most likely
666 // somebody messing with the form on the client side, so we'll just consider
667 // it an empty field
668 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
669 ? NULL
670 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
672
Barry Mienydd671972010-10-04 16:33:58 +0200673 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200675 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
677 $rule = substr($rule, 9);
678 $callback = TRUE;
679 }
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 // Strip the parameter (if exists) from the rule
682 // Rules can contain a parameter: max_length[5]
683 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200684 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
Andrey Andreevef758bd2012-11-15 12:24:52 +0200686 $rule = $match[1];
687 $param = $match[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 }
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 // Call the function that corresponds to the rule
691 if ($callback === TRUE)
692 {
693 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200694 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200695 log_message('debug', 'Unable to find callback validation rule: '.$rule);
696 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200698 else
699 {
700 // Run the function and grab the result
701 $result = $this->CI->$rule($postdata, $param);
702 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000703
704 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200705 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300707 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 }
709 else
710 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300711 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200715 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000717 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 }
719 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300720 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200721 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300722 // If our own wrapper function doesn't exist we see if a native PHP function does.
723 // Users can use any native PHP function call that has one param.
724 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 {
Andrey Andreev320d37c2012-04-03 20:21:39 +0300726 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200727
Andrey Andreev78f55772012-04-03 19:59:08 +0300728 if ($_in_array === TRUE)
729 {
730 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 }
patwork02404a12011-04-08 15:45:46 +0200732 else
733 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300734 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200735 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200737 else
738 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300739 log_message('debug', 'Unable to find validation rule: '.$rule);
740 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200741 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300742 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 else
744 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 $result = $this->$rule($postdata, $param);
746
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200747 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300749 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 }
751 else
752 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300753 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 }
755 }
Barry Mienydd671972010-10-04 16:33:58 +0200756
Andrey Andreev901573c2012-01-11 01:40:48 +0200757 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200759 {
Ahmedul Haque Abid42b40002014-01-09 01:10:25 +0600760 // Check if a custom message defined, else use default one
761 if(isset($this->_field_data[$row['field']]['error_msg'][$rule]))
762 {
763 $line = $this->_field_data[$row['field']]['error_msg'][$rule];
764 }
765 elseif ( ! isset($this->_error_messages[$rule]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 {
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200767 if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule))
768 // DEPRECATED support for non-prefixed keys
769 && FALSE === ($line = $this->CI->lang->line($rule, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 {
771 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200772 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 }
774 else
775 {
776 $line = $this->_error_messages[$rule];
777 }
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200780 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200781 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 {
Pascal Krietec1895832009-10-13 12:56:43 +0000783 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600787 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000788
789 // Save the error message
790 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 if ( ! isset($this->_error_array[$row['field']]))
793 {
794 $this->_error_array[$row['field']] = $message;
795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 return;
798 }
799 }
800 }
801
802 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 /**
805 * Translate a field name
806 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 * @param string the field name
808 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200809 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100810 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 {
812 // Do we need to translate the field name?
813 // We look for the prefix lang: to determine this
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200814 if (sscanf($fieldname, 'lang:%s', $line) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500816 // Were we able to translate the field name? If not we use $line
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200817 if (FALSE === ($fieldname = $this->CI->lang->line('form_validation_'.$line))
818 // DEPRECATED support for non-prefixed keys
819 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
821 return $line;
822 }
823 }
824
825 return $fieldname;
826 }
827
828 // --------------------------------------------------------------------
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200829
Eric Roberts41cc0902012-01-24 00:59:44 -0600830 /**
831 * Build an error message using the field and param.
832 *
833 * @param string The error message line
834 * @param string A field's human name
835 * @param mixed A rule's optional parameter
836 * @return string
837 */
838 protected function _build_error_msg($line, $field = '', $param = '')
839 {
840 // Check for %s in the string for legacy support.
Eric Roberts24a13f52012-12-12 07:09:42 -0600841 if (strpos($line, '%s') !== FALSE)
Eric Roberts41cc0902012-01-24 00:59:44 -0600842 {
843 return sprintf($line, $field, $param);
844 }
Andrew Podner4296a652012-12-17 07:51:15 -0500845
Eric Roberts41cc0902012-01-24 00:59:44 -0600846 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
847 }
848
849 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 /**
nisheeth-barthwala7447d22013-03-21 15:48:10 +0530852 * Checks if the rule is present within the validator
853 *
854 * Permits you to check if a rule is present within the validator
855 *
856 * @param string the field name
857 * @return bool
858 */
859 public function has_rule($field)
860 {
861 return isset($this->_field_data[$field]);
862 }
863
864 // --------------------------------------------------------------------
865
866 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 * Get the value from a form
868 *
869 * Permits you to repopulate a form field with the value it was submitted
870 * with, or, if that value doesn't exist, with the default
871 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 * @param string the field name
873 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200874 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200875 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100876 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200878 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
880 return $default;
881 }
Barry Mienydd671972010-10-04 16:33:58 +0200882
Phil Sturgeon5c561802011-01-05 16:31:59 +0000883 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600884 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000885 if (is_array($this->_field_data[$field]['postdata']))
886 {
887 return array_shift($this->_field_data[$field]['postdata']);
888 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000889
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 return $this->_field_data[$field]['postdata'];
891 }
Barry Mienydd671972010-10-04 16:33:58 +0200892
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200894
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 /**
896 * Set Select
897 *
898 * Enables pull-down lists to be set to the value the user
899 * selected in the event of an error
900 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 * @param string
902 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400903 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200905 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100906 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200907 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200908 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200910 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 }
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300914 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 if (is_array($field))
916 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300917 // Note: in_array('', array(0)) returns TRUE, do not use it
918 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300920 if ($value === $v)
921 {
922 return ' selected="selected"';
923 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300925
926 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100928 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200930 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 }
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 return ' selected="selected"';
934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200937
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 /**
939 * Set Radio
940 *
941 * Enables radio buttons to be set to the value the user
942 * selected in the event of an error
943 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 * @param string
945 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400946 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200948 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100949 public function set_radio($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200951 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200953 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
Barry Mienydd671972010-10-04 16:33:58 +0200955
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 $field = $this->_field_data[$field]['postdata'];
Andrey Andreeva587a932013-10-23 19:57:46 +0300957 $value = (string) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 if (is_array($field))
959 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300960 // Note: in_array('', array(0)) returns TRUE, do not use it
961 foreach ($field as &$v)
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300963 if ($value === $v)
964 {
965 return ' checked="checked"';
966 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300968
969 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100971 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200973 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 }
Barry Mienydd671972010-10-04 16:33:58 +0200975
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 return ' checked="checked"';
977 }
Barry Mienydd671972010-10-04 16:33:58 +0200978
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 /**
982 * Set Checkbox
983 *
984 * Enables checkboxes to be set to the value the user
985 * selected in the event of an error
986 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 * @param string
988 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400989 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200991 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100992 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200994 // Logic is exactly the same as for radio fields
995 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 /**
1001 * Required
1002 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 * @param string
1004 * @return bool
1005 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001006 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001008 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001014 * Performs a Regular Expression match test.
1015 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001016 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001017 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001018 * @return bool
1019 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001020 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001021 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001022 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -05001023 }
1024
1025 // --------------------------------------------------------------------
1026
1027 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 * Match one field to another
1029 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001030 * @param string $str string to compare against
1031 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 * @return bool
1033 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001034 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +03001036 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
1037 ? ($str === $this->_field_data[$field]['postdata'])
1038 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001040
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001041 // --------------------------------------------------------------------
1042
1043 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -03001044 * Differs from another field
1045 *
1046 * @param string
1047 * @param string field
1048 * @return bool
1049 */
1050 public function differs($str, $field)
1051 {
1052 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001054
1055 // --------------------------------------------------------------------
1056
1057 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +02001058 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001059 *
Andrey Andreevd09d6502012-01-03 06:38:33 +02001060 * Check if the input value doesn't already exist
1061 * in the specified database field.
1062 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001063 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001064 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001065 * @return bool
1066 */
1067 public function is_unique($str, $field)
1068 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001069 sscanf($field, '%[^.].%[^.]', $table, $field);
Eric Barnescccde962011-12-04 00:01:17 -05001070 if (isset($this->CI->db))
1071 {
1072 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
1073 return $query->num_rows() === 0;
1074 }
1075 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001076 }
Barry Mienydd671972010-10-04 16:33:58 +02001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001079
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 /**
1081 * Minimum Length
1082 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001084 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001086 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001087 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001089 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 {
1091 return FALSE;
1092 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001093 else
1094 {
1095 $val = (int) $val;
1096 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001097
Andrey Andreev78f55772012-04-03 19:59:08 +03001098 return (MB_ENABLED === TRUE)
1099 ? ($val <= mb_strlen($str))
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001100 : ($val <= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 }
Barry Mienydd671972010-10-04 16:33:58 +02001102
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001104
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 /**
1106 * Max Length
1107 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001109 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001111 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001112 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001114 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 {
1116 return FALSE;
1117 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001118 else
1119 {
1120 $val = (int) $val;
1121 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001122
Andrey Andreev78f55772012-04-03 19:59:08 +03001123 return (MB_ENABLED === TRUE)
1124 ? ($val >= mb_strlen($str))
1125 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 }
Barry Mienydd671972010-10-04 16:33:58 +02001127
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 /**
1131 * Exact Length
1132 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001134 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001136 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001137 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001139 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
1141 return FALSE;
1142 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001143 else
1144 {
1145 $val = (int) $val;
1146 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001147
Andrey Andreev78f55772012-04-03 19:59:08 +03001148 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001149 ? (mb_strlen($str) === $val)
1150 : (strlen($str) === $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 }
Barry Mienydd671972010-10-04 16:33:58 +02001152
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001154
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 /**
Andrey Andreevdaaca882012-11-26 22:16:12 +02001156 * Valid URL
1157 *
1158 * @param string $str
1159 * @return bool
1160 */
1161 public function valid_url($str)
1162 {
1163 if (empty($str))
1164 {
1165 return FALSE;
1166 }
1167 elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
1168 {
1169 if (empty($matches[2]))
1170 {
1171 return FALSE;
1172 }
1173 elseif ( ! in_array($matches[1], array('http', 'https'), TRUE))
1174 {
1175 return FALSE;
1176 }
1177
1178 $str = $matches[2];
1179 }
1180
1181 $str = 'http://'.$str;
1182
1183 // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the
1184 // underscore to be a valid hostname character instead of a dash.
1185 // Reference: https://bugs.php.net/bug.php?id=51192
1186 if (version_compare(PHP_VERSION, '5.2.13', '==') === 0 OR version_compare(PHP_VERSION, '5.3.2', '==') === 0)
1187 {
1188 sscanf($str, 'http://%[^/]', $host);
1189 $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host));
1190 }
1191
1192 return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 }
1194
1195 // --------------------------------------------------------------------
1196
1197 /**
1198 * Valid Email
1199 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 * @param string
1201 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001202 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001203 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001205 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 }
1207
1208 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001209
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 /**
1211 * Valid Emails
1212 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 * @param string
1214 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001215 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001216 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 {
1218 if (strpos($str, ',') === FALSE)
1219 {
1220 return $this->valid_email(trim($str));
1221 }
Barry Mienydd671972010-10-04 16:33:58 +02001222
Pascal Kriete14287f32011-02-14 13:39:34 -05001223 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001225 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 {
1227 return FALSE;
1228 }
1229 }
Barry Mienydd671972010-10-04 16:33:58 +02001230
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 return TRUE;
1232 }
1233
1234 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001235
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 /**
1237 * Validate IP Address
1238 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001240 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001241 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001243 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001245 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 }
1247
1248 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001249
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 /**
1251 * Alpha
1252 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 * @param string
1254 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001255 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001256 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001258 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 }
Barry Mienydd671972010-10-04 16:33:58 +02001260
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001262
Derek Allard2067d1a2008-11-13 22:59:24 +00001263 /**
1264 * Alpha-numeric
1265 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 * @param string
1267 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001268 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001269 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001271 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 }
Barry Mienydd671972010-10-04 16:33:58 +02001273
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 // --------------------------------------------------------------------
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001275
1276 /**
1277 * Alpha-numeric w/ spaces
1278 *
1279 * @param string
1280 * @return bool
1281 */
1282 public function alpha_numeric_spaces($str)
1283 {
Sajan Parikhdf3bfed2013-02-04 12:25:49 -06001284 return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
Sajan Parikh2d1608a2013-02-02 08:00:39 -06001285 }
1286
1287 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001288
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 /**
1290 * Alpha-numeric with underscores and dashes
1291 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 * @param string
1293 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001294 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001295 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001297 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 }
Barry Mienydd671972010-10-04 16:33:58 +02001299
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001301
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 /**
1303 * Numeric
1304 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 * @param string
1306 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001307 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001308 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001310 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001311
1312 }
1313
1314 // --------------------------------------------------------------------
1315
Barry Mienydd671972010-10-04 16:33:58 +02001316 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001317 * Integer
1318 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 * @param string
1320 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001321 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001322 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001324 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1325 }
1326
1327 // --------------------------------------------------------------------
1328
1329 /**
1330 * Decimal number
1331 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001332 * @param string
1333 * @return bool
1334 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001335 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001336 {
1337 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1338 }
1339
1340 // --------------------------------------------------------------------
1341
1342 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001343 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001344 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001345 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001346 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001347 * @return bool
1348 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001349 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001350 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001351 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001352 }
1353
1354 // --------------------------------------------------------------------
1355
1356 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001357 * Equal to or Greater than
1358 *
Nick Busey98c347d2012-02-02 11:07:03 -07001359 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001360 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001361 * @return bool
1362 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001363 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001364 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001365 return is_numeric($str) ? ($str >= $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001366 }
1367
1368 // --------------------------------------------------------------------
1369
1370 /**
1371 * Less than
1372 *
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 less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001378 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001379 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001381
1382 // --------------------------------------------------------------------
1383
Barry Mienydd671972010-10-04 16:33:58 +02001384 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001385 * Equal to or Less 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 less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001392 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001393 return is_numeric($str) ? ($str <= $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 }
1395
1396 // --------------------------------------------------------------------
1397
Barry Mienydd671972010-10-04 16:33:58 +02001398 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001399 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001400 *
Barry Mienydd671972010-10-04 16:33:58 +02001401 * @param string
1402 * @return bool
1403 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001404 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001405 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001406 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001407 }
1408
1409 // --------------------------------------------------------------------
1410
1411 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001412 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001413 *
Barry Mienydd671972010-10-04 16:33:58 +02001414 * @param string
1415 * @return bool
1416 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001417 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001418 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001419 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001420 }
1421
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001423
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 /**
1425 * Valid Base64
1426 *
1427 * Tests a string for characters outside of the Base64 alphabet
1428 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1429 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 * @param string
1431 * @return bool
1432 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001433 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001434 {
Andrey Andreevcd9797a2013-06-28 14:03:48 +03001435 return (base64_encode(base64_decode($str)) === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 }
Barry Mienydd671972010-10-04 16:33:58 +02001437
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001439
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 /**
1441 * Prep data for form
1442 *
1443 * This function allows HTML to be safely shown in a form.
1444 * Special characters are converted.
1445 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 * @param string
1447 * @return string
1448 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001449 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001451 if ($this->_safe_form_data === FALSE OR empty($data))
1452 {
1453 return $data;
1454 }
1455
Derek Allard2067d1a2008-11-13 22:59:24 +00001456 if (is_array($data))
1457 {
1458 foreach ($data as $key => $val)
1459 {
1460 $data[$key] = $this->prep_for_form($val);
1461 }
Barry Mienydd671972010-10-04 16:33:58 +02001462
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 return $data;
1464 }
Barry Mienydd671972010-10-04 16:33:58 +02001465
Andrey Andreev901573c2012-01-11 01:40:48 +02001466 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001467 }
Barry Mienydd671972010-10-04 16:33:58 +02001468
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001470
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 /**
1472 * Prep URL
1473 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 * @param string
1475 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001476 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001477 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001479 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 {
1481 return '';
1482 }
Barry Mienydd671972010-10-04 16:33:58 +02001483
Andrey Andreev901573c2012-01-11 01:40:48 +02001484 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001485 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001486 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001487 }
Barry Mienydd671972010-10-04 16:33:58 +02001488
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 return $str;
1490 }
Barry Mienydd671972010-10-04 16:33:58 +02001491
Derek Allard2067d1a2008-11-13 22:59:24 +00001492 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001493
Derek Allard2067d1a2008-11-13 22:59:24 +00001494 /**
1495 * Strip Image Tags
1496 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001497 * @param string
1498 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001499 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001500 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001501 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001502 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 }
Barry Mienydd671972010-10-04 16:33:58 +02001504
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001506
Derek Allard2067d1a2008-11-13 22:59:24 +00001507 /**
1508 * XSS Clean
1509 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 * @param string
1511 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001512 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001513 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 {
Derek Jones5640a712010-04-23 11:22:40 -05001515 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 }
Barry Mienydd671972010-10-04 16:33:58 +02001517
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001519
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 /**
1521 * Convert PHP tags to entities
1522 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 * @param string
1524 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001525 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001526 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001527 {
Andrey Andreev838a9d62012-12-03 14:37:47 +02001528 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001529 }
1530
JonoB099c4782012-03-04 14:37:30 +00001531 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001532
1533 /**
1534 * Reset validation vars
1535 *
1536 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001537 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001538 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001539 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001540 */
JonoB883f80f2012-03-05 09:51:27 +00001541 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001542 {
JonoB099c4782012-03-04 14:37:30 +00001543 $this->_field_data = array();
1544 $this->_config_rules = array();
1545 $this->_error_array = array();
1546 $this->_error_messages = array();
1547 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001548 }
1549
Derek Allard2067d1a2008-11-13 22:59:24 +00001550}
Derek Allard2067d1a2008-11-13 22:59:24 +00001551
1552/* End of file Form_validation.php */
Eric Roberts24a13f52012-12-12 07:09:42 -06001553/* Location: ./system/libraries/Form_validation.php */