blob: bbd0b523e1be6e47d9683126a0c6ea4f487e7cfb [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
147 * rules as input, validates the info, and stores it
148 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400149 * @param mixed $field
150 * @param string $label
151 * @param mixed $rules
Andrew Podner4296a652012-12-17 07:51:15 -0500152 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100154 public function set_rules($field, $label = '', $rules = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
156 // No reason to set rules if we have no POST data
JonoB099c4782012-03-04 14:37:30 +0000157 // or a validation array has not been specified
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200158 if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Greg Aker9f9af602010-11-10 15:41:51 -0600160 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
tiyowanc2acb232012-03-14 21:24:00 +0400163 // If an array was passed via the first parameter instead of individual string
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 // values we cycle through it and recursively call this function.
165 if (is_array($field))
166 {
167 foreach ($field as $row)
168 {
169 // Houston, we have a problem...
Andrey Andreev78f55772012-04-03 19:59:08 +0300170 if ( ! isset($row['field'], $row['rules']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
172 continue;
173 }
174
175 // If the field label wasn't passed we use the field name
Andrey Andreev78f55772012-04-03 19:59:08 +0300176 $label = isset($row['label']) ? $row['label'] : $row['field'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000177
178 // Here we go!
179 $this->set_rules($row['field'], $label, $row['rules']);
180 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300181
Greg Aker9f9af602010-11-10 15:41:51 -0600182 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Kevin Wood-Friend6a56d502012-06-12 09:54:01 -0400185 // Convert an array of rules to a string
186 if (is_array($rules))
187 {
188 $rules = implode('|', $rules);
189 }
190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // No fields? Nothing to do...
Alex Bilbied261b1e2012-06-02 11:12:16 +0100192 if ( ! is_string($field) OR ! is_string($rules) OR $field === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Greg Aker9f9af602010-11-10 15:41:51 -0600194 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
196
197 // If the field label wasn't passed we use the field name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100198 $label = ($label === '') ? $field : $label;
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200200 // Is the field name an array? If it is an array, we break it apart
Barry Mienydd671972010-10-04 16:33:58 +0200201 // into its components so that we can fetch the corresponding POST data later
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200202 $indexes = array();
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200203 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
Barry Mienydd671972010-10-04 16:33:58 +0200204 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200205 sscanf($field, '%[^[][', $indexes[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000206
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200207 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100209 if ($matches[1][$i] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200211 $indexes[] = $matches[1][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 $is_array = TRUE;
216 }
217 else
218 {
Barry Mienydd671972010-10-04 16:33:58 +0200219 $is_array = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
222 // Build our master array
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 $this->_field_data[$field] = array(
Andrey Andreev56454792012-05-17 14:32:19 +0300224 'field' => $field,
225 'label' => $label,
226 'rules' => $rules,
227 'is_array' => $is_array,
228 'keys' => $indexes,
229 'postdata' => NULL,
230 'error' => ''
Phil Sturgeonef112c02011-02-07 13:01:47 +0000231 );
Greg Aker9f9af602010-11-10 15:41:51 -0600232
233 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235
236 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 /**
JonoB099c4782012-03-04 14:37:30 +0000239 * By default, form validation uses the $_POST array to validate
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200240 *
JonoB099c4782012-03-04 14:37:30 +0000241 * If an array is set through this method, then this array will
242 * be used instead of the $_POST array
Andrey Andreev3b2c5082012-03-07 22:49:24 +0200243 *
244 * Note that if you are validating multiple arrays, then the
245 * reset_validation() function should be called after validating
JonoB883f80f2012-03-05 09:51:27 +0000246 * each array due to the limitations of CI's singleton
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200247 *
248 * @param array $data
249 * @return void
JonoB099c4782012-03-04 14:37:30 +0000250 */
251 public function set_data($data = '')
252 {
253 if ( ! empty($data) && is_array($data))
254 {
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200255 $this->validation_data = $data;
JonoB099c4782012-03-04 14:37:30 +0000256 }
257 }
Andrey Andreevc8da4fe2012-03-04 19:20:33 +0200258
JonoB099c4782012-03-04 14:37:30 +0000259 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000260
261 /**
262 * Set Error Message
263 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300264 * Lets users set their own error messages on the fly. Note:
265 * The key name has to match the function name that it corresponds to.
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 *
Andrey Andreev78f55772012-04-03 19:59:08 +0300267 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500269 * @return CI_Form_validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100271 public function set_message($lang, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 if ( ! is_array($lang))
274 {
275 $lang = array($lang => $val);
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 $this->_error_messages = array_merge($this->_error_messages, $lang);
Greg Aker9f9af602010-11-10 15:41:51 -0600279 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 /**
285 * Set The Error Delimiter
286 *
287 * Permits a prefix/suffix to be added to each error message
288 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 * @param string
290 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500291 * @return CI_Form_validation
Barry Mienydd671972010-10-04 16:33:58 +0200292 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100293 public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
295 $this->_error_prefix = $prefix;
296 $this->_error_suffix = $suffix;
Greg Aker9f9af602010-11-10 15:41:51 -0600297 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
299
300 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 /**
303 * Get Error Message
304 *
305 * Gets the error message associated with a particular field
306 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @param string the field name
Timothy Warren0688ac92012-04-20 10:25:04 -0400308 * @param string the html start tag
309 * @param strign the html end tag
Andrey Andreev78f55772012-04-03 19:59:08 +0300310 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200311 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100312 public function error($field = '', $prefix = '', $suffix = '')
Barry Mienydd671972010-10-04 16:33:58 +0200313 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300314 if (empty($this->_field_data[$field]['error']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
316 return '';
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Alex Bilbied261b1e2012-06-02 11:12:16 +0100319 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 $prefix = $this->_error_prefix;
322 }
323
Alex Bilbied261b1e2012-06-02 11:12:16 +0100324 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
326 $suffix = $this->_error_suffix;
327 }
328
329 return $prefix.$this->_field_data[$field]['error'].$suffix;
330 }
331
332 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 /**
Michiel Vugteveen676a0dd2012-03-02 10:10:34 +0100335 * Get Array of Error Messages
336 *
337 * Returns the error messages as an array
338 *
339 * @return array
340 */
341 public function error_array()
342 {
343 return $this->_error_array;
344 }
345
346 // --------------------------------------------------------------------
347
348 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * Error String
350 *
351 * Returns the error messages as a string, wrapped in the error delimiters
352 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 * @param string
354 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300355 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200356 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100357 public function error_string($prefix = '', $suffix = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 // No errrors, validation passes!
360 if (count($this->_error_array) === 0)
361 {
362 return '';
363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Alex Bilbied261b1e2012-06-02 11:12:16 +0100365 if ($prefix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
367 $prefix = $this->_error_prefix;
368 }
369
Alex Bilbied261b1e2012-06-02 11:12:16 +0100370 if ($suffix === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
372 $suffix = $this->_error_suffix;
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 // Generate the error string
376 $str = '';
377 foreach ($this->_error_array as $val)
378 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100379 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 $str .= $prefix.$val.$suffix."\n";
382 }
383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 return $str;
386 }
387
388 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 /**
391 * Run the Validator
392 *
393 * This function does all the work.
394 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400395 * @param string $group
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200397 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100398 public function run($group = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500400 // Do we even have any data to process? Mm?
Andrey Andreev78f55772012-04-03 19:59:08 +0300401 $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data;
JonoB099c4782012-03-04 14:37:30 +0000402 if (count($validation_array) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 return FALSE;
405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 // Does the _field_data array containing the validation rules exist?
408 // If not, we look to see if they were assigned via a config file
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200409 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500411 // No validation rules? We're done...
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200412 if (count($this->_config_rules) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 return FALSE;
415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 // Is there a validation rule for the particular URI being accessed?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100418 $uri = ($group === '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
Barry Mienydd671972010-10-04 16:33:58 +0200419
Alex Bilbied261b1e2012-06-02 11:12:16 +0100420 if ($uri !== '' && isset($this->_config_rules[$uri]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 $this->set_rules($this->_config_rules[$uri]);
423 }
424 else
425 {
426 $this->set_rules($this->_config_rules);
427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Andrey Andreev901573c2012-01-11 01:40:48 +0200429 // Were we able to set the rules correctly?
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200430 if (count($this->_field_data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200432 log_message('debug', 'Unable to find validation rules');
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 return FALSE;
434 }
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 // Load the language file containing error messages
438 $this->CI->lang->load('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200439
Andrey Andreev751f2472012-11-03 18:26:27 +0200440 // Cycle through the rules for each field and match the corresponding $validation_data item
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 foreach ($this->_field_data as $field => $row)
Barry Mienydd671972010-10-04 16:33:58 +0200442 {
Andrey Andreev751f2472012-11-03 18:26:27 +0200443 // Fetch the data from the validation_data array item and cache it in the _field_data array.
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // 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 +0200445 if ($row['is_array'] === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
JonoB099c4782012-03-04 14:37:30 +0000447 $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
Andrey Andreevfff6c2a2012-05-13 22:15:23 +0300449 elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300451 $this->_field_data[$field]['postdata'] = $validation_array[$field];
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
Andrey Andreev751f2472012-11-03 18:26:27 +0200453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Andrey Andreev751f2472012-11-03 18:26:27 +0200455 // Execute validation rules
456 // Note: A second foreach (for now) is required in order to avoid false-positives
457 // for rules like 'matches', which correlate to other validation fields.
458 foreach ($this->_field_data as $field => $row)
459 {
Andrey Andreev3d9cec92012-07-08 21:50:19 +0300460 // Don't try to validate if we have no rules set
461 if (empty($row['rules']))
462 {
463 continue;
464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
466 $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468
469 // Did we end up with any errors?
470 $total_errors = count($this->_error_array);
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 if ($total_errors > 0)
472 {
473 $this->_safe_form_data = TRUE;
474 }
475
476 // Now we need to re-set the POST data with the new, processed data
477 $this->_reset_post_array();
Barry Mienydd671972010-10-04 16:33:58 +0200478
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200479 return ($total_errors === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
481
482 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 /**
485 * Traverse a multidimensional $_POST array index until the data is found
486 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 * @param array
488 * @param array
Andrey Andreev78f55772012-04-03 19:59:08 +0300489 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200491 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100492 protected function _reduce_array($array, $keys, $i = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200494 if (is_array($array) && isset($keys[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200496 return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Andrey Andreev2d48b4f2012-11-23 17:33:21 +0200499 // NULL must be returned for empty fields
Andrey Andreev44c34632012-11-23 18:46:34 +0200500 return ($array === '') ? NULL : $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
502
503 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 /**
506 * Re-populate the _POST array with our finalized and processed data
507 *
Andrey Andreev6de924c2012-01-20 13:18:18 +0200508 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200509 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100510 protected function _reset_post_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
512 foreach ($this->_field_data as $field => $row)
513 {
vlakoff1228fe22013-01-14 01:30:09 +0100514 if ($row['postdata'] !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200516 if ($row['is_array'] === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 if (isset($_POST[$row['field']]))
519 {
520 $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
521 }
522 }
523 else
524 {
Derek Jones63eeae32009-02-10 19:08:56 +0000525 // start with a reference
526 $post_ref =& $_POST;
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Jones63eeae32009-02-10 19:08:56 +0000528 // before we assign values, make a reference to the right POST key
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200529 if (count($row['keys']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
Derek Jones63eeae32009-02-10 19:08:56 +0000531 $post_ref =& $post_ref[current($row['keys'])];
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
533 else
534 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 foreach ($row['keys'] as $val)
536 {
Derek Jones63eeae32009-02-10 19:08:56 +0000537 $post_ref =& $post_ref[$val];
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
539 }
Derek Jones63eeae32009-02-10 19:08:56 +0000540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 if (is_array($row['postdata']))
Derek Jones63eeae32009-02-10 19:08:56 +0000542 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 $array = array();
544 foreach ($row['postdata'] as $k => $v)
545 {
546 $array[$k] = $this->prep_for_form($v);
547 }
Derek Jones63eeae32009-02-10 19:08:56 +0000548
549 $post_ref = $array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
551 else
Derek Jones63eeae32009-02-10 19:08:56 +0000552 {
553 $post_ref = $this->prep_for_form($row['postdata']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 }
556 }
557 }
558 }
559
560 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 /**
563 * Executes the Validation routines
564 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 * @param array
566 * @param array
567 * @param mixed
Andrey Andreev78f55772012-04-03 19:59:08 +0300568 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200570 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100571 protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
573 // If the $_POST data is an array we will run a recursive call
574 if (is_array($postdata))
Barry Mienydd671972010-10-04 16:33:58 +0200575 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 foreach ($postdata as $key => $val)
577 {
Andrey Andreev8d3099d2012-06-21 16:00:20 +0300578 $this->_execute($row, $rules, $val, $key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 return;
582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 // If the field is blank, but NOT required, no further tests are necessary
585 $callback = FALSE;
vlakoff1228fe22013-01-14 01:30:09 +0100586 if ( ! in_array('required', $rules) && $postdata === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 // Before we bail out, does the rule contain a callback?
Andrey Andreev901573c2012-01-11 01:40:48 +0200589 if (preg_match('/(callback_\w+(\[.*?\])?)/', implode(' ', $rules), $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
591 $callback = TRUE;
Andrey Andreev78f55772012-04-03 19:59:08 +0300592 $rules = array(1 => $match[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 }
594 else
595 {
596 return;
597 }
598 }
599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // Isset Test. Typically this rule will only apply to checkboxes.
vlakoff1228fe22013-01-14 01:30:09 +0100601 if ($postdata === NULL && $callback === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 {
603 if (in_array('isset', $rules, TRUE) OR in_array('required', $rules))
604 {
605 // Set the message type
Andrey Andreev78f55772012-04-03 19:59:08 +0300606 $type = in_array('required', $rules) ? 'required' : 'isset';
Barry Mienydd671972010-10-04 16:33:58 +0200607
Andrey Andreev56454792012-05-17 14:32:19 +0300608 if (isset($this->_error_messages[$type]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 $line = $this->_error_messages[$type];
611 }
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200612 elseif (FALSE === ($line = $this->CI->lang->line('form_validation_'.$type))
613 // DEPRECATED support for non-prefixed keys
614 && FALSE === ($line = $this->CI->lang->line($type, FALSE)))
Andrey Andreev56454792012-05-17 14:32:19 +0300615 {
616 $line = 'The field was not set';
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600620 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']));
Derek Allard2067d1a2008-11-13 22:59:24 +0000621
622 // Save the error message
623 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 if ( ! isset($this->_error_array[$row['field']]))
626 {
627 $this->_error_array[$row['field']] = $message;
628 }
629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 return;
632 }
633
634 // --------------------------------------------------------------------
635
636 // Cycle through each rule and run it
Andrey Andreev78f55772012-04-03 19:59:08 +0300637 foreach ($rules as $rule)
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
639 $_in_array = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 // We set the $postdata variable with the current data in our master array so that
642 // each cycle of the loop is dealing with the processed data from the last cycle
Alex Bilbied261b1e2012-06-02 11:12:16 +0100643 if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 // We shouldn't need this safety, but just in case there isn't an array index
646 // associated with this cycle we'll bail out
647 if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
648 {
649 continue;
650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
653 $_in_array = TRUE;
654 }
655 else
656 {
Andrey Andreev6ac51442012-06-18 13:05:17 +0300657 // If we get an array field, but it's not expected - then it is most likely
658 // somebody messing with the form on the client side, so we'll just consider
659 // it an empty field
660 $postdata = is_array($this->_field_data[$row['field']]['postdata'])
661 ? NULL
662 : $this->_field_data[$row['field']]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 }
664
Barry Mienydd671972010-10-04 16:33:58 +0200665 // Is the rule a callback?
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 $callback = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200667 if (strpos($rule, 'callback_') === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
669 $rule = substr($rule, 9);
670 $callback = TRUE;
671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 // Strip the parameter (if exists) from the rule
674 // Rules can contain a parameter: max_length[5]
675 $param = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200676 if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
Andrey Andreevef758bd2012-11-15 12:24:52 +0200678 $rule = $match[1];
679 $param = $match[2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 }
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 // Call the function that corresponds to the rule
683 if ($callback === TRUE)
684 {
685 if ( ! method_exists($this->CI, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200686 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200687 log_message('debug', 'Unable to find callback validation rule: '.$rule);
688 $result = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200690 else
691 {
692 // Run the function and grab the result
693 $result = $this->CI->$rule($postdata, $param);
694 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000695
696 // Re-assign the result to the master data array
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200697 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300699 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 }
701 else
702 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300703 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 // If the field isn't required and we just processed a callback we'll move on...
Andrey Andreev6de924c2012-01-20 13:18:18 +0200707 if ( ! in_array('required', $rules, TRUE) && $result !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
Derek Allard4e5cf1c2009-07-06 20:53:41 +0000709 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 }
711 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300712 elseif ( ! method_exists($this, $rule))
Barry Mienydd671972010-10-04 16:33:58 +0200713 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300714 // If our own wrapper function doesn't exist we see if a native PHP function does.
715 // Users can use any native PHP function call that has one param.
716 if (function_exists($rule))
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 {
Andrey Andreev320d37c2012-04-03 20:21:39 +0300718 $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata);
Barry Mienydd671972010-10-04 16:33:58 +0200719
Andrey Andreev78f55772012-04-03 19:59:08 +0300720 if ($_in_array === TRUE)
721 {
722 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 }
patwork02404a12011-04-08 15:45:46 +0200724 else
725 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300726 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
patwork02404a12011-04-08 15:45:46 +0200727 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 }
Andrey Andreev901573c2012-01-11 01:40:48 +0200729 else
730 {
Andrey Andreevcec2ba52012-04-03 20:26:38 +0300731 log_message('debug', 'Unable to find validation rule: '.$rule);
732 $result = FALSE;
Andrey Andreev901573c2012-01-11 01:40:48 +0200733 }
Andrey Andreev78f55772012-04-03 19:59:08 +0300734 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 else
736 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 $result = $this->$rule($postdata, $param);
738
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200739 if ($_in_array === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300741 $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
743 else
744 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300745 $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 }
747 }
Barry Mienydd671972010-10-04 16:33:58 +0200748
Andrey Andreev901573c2012-01-11 01:40:48 +0200749 // Did the rule test negatively? If so, grab the error.
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 if ($result === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200751 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 if ( ! isset($this->_error_messages[$rule]))
753 {
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200754 if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule))
755 // DEPRECATED support for non-prefixed keys
756 && FALSE === ($line = $this->CI->lang->line($rule, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 {
758 $line = 'Unable to access an error message corresponding to your field name.';
Barry Mienydd671972010-10-04 16:33:58 +0200759 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 }
761 else
762 {
763 $line = $this->_error_messages[$rule];
764 }
Barry Mienydd671972010-10-04 16:33:58 +0200765
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 // Is the parameter we are inserting into the error message the name
Andrey Andreev901573c2012-01-11 01:40:48 +0200767 // of another field? If so we need to grab its "field label"
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200768 if (isset($this->_field_data[$param], $this->_field_data[$param]['label']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 {
Pascal Krietec1895832009-10-13 12:56:43 +0000770 $param = $this->_translate_fieldname($this->_field_data[$param]['label']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
Barry Mienydd671972010-10-04 16:33:58 +0200772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 // Build the error message
Eric Roberts41cc0902012-01-24 00:59:44 -0600774 $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param);
Derek Allard2067d1a2008-11-13 22:59:24 +0000775
776 // Save the error message
777 $this->_field_data[$row['field']]['error'] = $message;
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 if ( ! isset($this->_error_array[$row['field']]))
780 {
781 $this->_error_array[$row['field']] = $message;
782 }
Barry Mienydd671972010-10-04 16:33:58 +0200783
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 return;
785 }
786 }
787 }
788
789 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200790
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 /**
792 * Translate a field name
793 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 * @param string the field name
795 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200796 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100797 protected function _translate_fieldname($fieldname)
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 {
799 // Do we need to translate the field name?
800 // We look for the prefix lang: to determine this
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200801 if (sscanf($fieldname, 'lang:%s', $line) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500803 // Were we able to translate the field name? If not we use $line
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200804 if (FALSE === ($fieldname = $this->CI->lang->line('form_validation_'.$line))
805 // DEPRECATED support for non-prefixed keys
806 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 {
808 return $line;
809 }
810 }
811
812 return $fieldname;
813 }
814
815 // --------------------------------------------------------------------
Andrey Andreevd4eec9f2012-12-14 11:07:13 +0200816
Eric Roberts41cc0902012-01-24 00:59:44 -0600817 /**
818 * Build an error message using the field and param.
819 *
820 * @param string The error message line
821 * @param string A field's human name
822 * @param mixed A rule's optional parameter
823 * @return string
824 */
825 protected function _build_error_msg($line, $field = '', $param = '')
826 {
827 // Check for %s in the string for legacy support.
Eric Roberts24a13f52012-12-12 07:09:42 -0600828 if (strpos($line, '%s') !== FALSE)
Eric Roberts41cc0902012-01-24 00:59:44 -0600829 {
830 return sprintf($line, $field, $param);
831 }
Andrew Podner4296a652012-12-17 07:51:15 -0500832
Eric Roberts41cc0902012-01-24 00:59:44 -0600833 return str_replace(array('{field}', '{param}'), array($field, $param), $line);
834 }
835
836 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 /**
839 * Get the value from a form
840 *
841 * Permits you to repopulate a form field with the value it was submitted
842 * with, or, if that value doesn't exist, with the default
843 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 * @param string the field name
845 * @param string
Andrey Andreev46ac8812012-02-28 14:32:54 +0200846 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200847 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100848 public function set_value($field = '', $default = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200850 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
852 return $default;
853 }
Barry Mienydd671972010-10-04 16:33:58 +0200854
Phil Sturgeon5c561802011-01-05 16:31:59 +0000855 // If the data is an array output them one at a time.
Greg Aker03abee32011-12-25 00:31:29 -0600856 // E.g: form_input('name[]', set_value('name[]');
Phil Sturgeon5c561802011-01-05 16:31:59 +0000857 if (is_array($this->_field_data[$field]['postdata']))
858 {
859 return array_shift($this->_field_data[$field]['postdata']);
860 }
Phil Sturgeonc3828712011-01-19 12:31:47 +0000861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 return $this->_field_data[$field]['postdata'];
863 }
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 /**
868 * Set Select
869 *
870 * Enables pull-down lists to be set to the value the user
871 * selected in the event of an error
872 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 * @param string
874 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400875 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200877 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100878 public function set_select($field = '', $value = '', $default = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200879 {
Andrey Andreev46ac8812012-02-28 14:32:54 +0200880 if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200882 return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 if (is_array($field))
887 {
888 if ( ! in_array($value, $field))
889 {
890 return '';
891 }
892 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100893 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200895 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
Barry Mienydd671972010-10-04 16:33:58 +0200897
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 return ' selected="selected"';
899 }
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 /**
904 * Set Radio
905 *
906 * Enables radio buttons to be set to the value the user
907 * selected in the event of an error
908 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 * @param string
910 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400911 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200913 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100914 public function set_radio($field = '', $value = '', $default = FALSE)
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 {
Andrey Andreev6de924c2012-01-20 13:18:18 +0200918 return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 $field = $this->_field_data[$field]['postdata'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 if (is_array($field))
923 {
924 if ( ! in_array($value, $field))
925 {
926 return '';
927 }
928 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100929 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 {
Andrey Andreev901573c2012-01-11 01:40:48 +0200931 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 }
Barry Mienydd671972010-10-04 16:33:58 +0200933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 return ' checked="checked"';
935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 /**
940 * Set Checkbox
941 *
942 * Enables checkboxes to be set to the value the user
943 * selected in the event of an error
944 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 * @param string
946 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -0400947 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200949 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100950 public function set_checkbox($field = '', $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200952 // Logic is exactly the same as for radio fields
953 return $this->set_radio($field, $value, $default);
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
Barry Mienydd671972010-10-04 16:33:58 +0200955
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 /**
959 * Required
960 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 * @param string
962 * @return bool
963 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100964 public function required($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
Andrey Andreev78f55772012-04-03 19:59:08 +0300966 return is_array($str) ? (bool) count($str) : (trim($str) !== '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 /**
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500972 * Performs a Regular Expression match test.
973 *
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500974 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +0300975 * @param string regex
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500976 * @return bool
977 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100978 public function regex_match($str, $regex)
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500979 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +0200980 return (bool) preg_match($regex, $str);
Dan Horrigan2280e8e2010-12-15 10:16:38 -0500981 }
982
983 // --------------------------------------------------------------------
984
985 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 * Match one field to another
987 *
Andrey Andreeva779b2c2012-10-26 16:25:47 +0300988 * @param string $str string to compare against
989 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 * @return bool
991 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100992 public function matches($str, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Andrey Andreeva779b2c2012-10-26 16:25:47 +0300994 return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
995 ? ($str === $this->_field_data[$field]['postdata'])
996 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000998
Phil Sturgeon3837ae72011-05-09 21:12:26 +0100999 // --------------------------------------------------------------------
1000
1001 /**
Raul Baldner Juniorf38564d2012-10-11 11:32:23 -03001002 * Differs from another field
1003 *
1004 * @param string
1005 * @param string field
1006 * @return bool
1007 */
1008 public function differs($str, $field)
1009 {
1010 return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 }
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001012
1013 // --------------------------------------------------------------------
1014
1015 /**
Andrey Andreevd09d6502012-01-03 06:38:33 +02001016 * Is Unique
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001017 *
Andrey Andreevd09d6502012-01-03 06:38:33 +02001018 * Check if the input value doesn't already exist
1019 * in the specified database field.
1020 *
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001021 * @param string
Andrey Andreev78f55772012-04-03 19:59:08 +03001022 * @param string field
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001023 * @return bool
1024 */
1025 public function is_unique($str, $field)
1026 {
Andrey Andreev7a7ad782012-11-12 17:21:01 +02001027 sscanf($field, '%[^.].%[^.]', $table, $field);
Eric Barnescccde962011-12-04 00:01:17 -05001028 if (isset($this->CI->db))
1029 {
1030 $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
1031 return $query->num_rows() === 0;
1032 }
1033 return FALSE;
Greg Aker03abee32011-12-25 00:31:29 -06001034 }
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 * Minimum Length
1040 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001042 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001044 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001045 public function min_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001047 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
1049 return FALSE;
1050 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001051 else
1052 {
1053 $val = (int) $val;
1054 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001055
Andrey Andreev78f55772012-04-03 19:59:08 +03001056 return (MB_ENABLED === TRUE)
1057 ? ($val <= mb_strlen($str))
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001058 : ($val <= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 }
Barry Mienydd671972010-10-04 16:33:58 +02001060
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 /**
1064 * Max Length
1065 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 * @param string
Michiel Vugteveena8221ad2012-06-14 23:26:34 +02001067 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001069 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001070 public function max_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001072 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 {
1074 return FALSE;
1075 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001076 else
1077 {
1078 $val = (int) $val;
1079 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001080
Andrey Andreev78f55772012-04-03 19:59:08 +03001081 return (MB_ENABLED === TRUE)
1082 ? ($val >= mb_strlen($str))
1083 : ($val >= strlen($str));
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 }
Barry Mienydd671972010-10-04 16:33:58 +02001085
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001087
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 /**
1089 * Exact Length
1090 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 * @param string
Michiel Vugteveeneccde132012-06-14 23:22:26 +02001092 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001094 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001095 public function exact_length($str, $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 {
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001097 if ( ! is_numeric($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
1099 return FALSE;
1100 }
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001101 else
1102 {
1103 $val = (int) $val;
1104 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001105
Andrey Andreev78f55772012-04-03 19:59:08 +03001106 return (MB_ENABLED === TRUE)
Michiel Vugteveenceaf8872012-06-15 11:56:24 +02001107 ? (mb_strlen($str) === $val)
1108 : (strlen($str) === $val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 }
Barry Mienydd671972010-10-04 16:33:58 +02001110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001112
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 /**
Andrey Andreevdaaca882012-11-26 22:16:12 +02001114 * Valid URL
1115 *
1116 * @param string $str
1117 * @return bool
1118 */
1119 public function valid_url($str)
1120 {
1121 if (empty($str))
1122 {
1123 return FALSE;
1124 }
1125 elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches))
1126 {
1127 if (empty($matches[2]))
1128 {
1129 return FALSE;
1130 }
1131 elseif ( ! in_array($matches[1], array('http', 'https'), TRUE))
1132 {
1133 return FALSE;
1134 }
1135
1136 $str = $matches[2];
1137 }
1138
1139 $str = 'http://'.$str;
1140
1141 // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the
1142 // underscore to be a valid hostname character instead of a dash.
1143 // Reference: https://bugs.php.net/bug.php?id=51192
1144 if (version_compare(PHP_VERSION, '5.2.13', '==') === 0 OR version_compare(PHP_VERSION, '5.3.2', '==') === 0)
1145 {
1146 sscanf($str, 'http://%[^/]', $host);
1147 $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host));
1148 }
1149
1150 return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 }
1152
1153 // --------------------------------------------------------------------
1154
1155 /**
1156 * Valid Email
1157 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 * @param string
1159 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001160 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001161 public function valid_email($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
Andrey Andreev580388b2012-06-27 15:43:46 +03001163 return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
1165
1166 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001167
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 /**
1169 * Valid Emails
1170 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 * @param string
1172 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001173 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001174 public function valid_emails($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 {
1176 if (strpos($str, ',') === FALSE)
1177 {
1178 return $this->valid_email(trim($str));
1179 }
Barry Mienydd671972010-10-04 16:33:58 +02001180
Pascal Kriete14287f32011-02-14 13:39:34 -05001181 foreach (explode(',', $str) as $email)
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001183 if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 {
1185 return FALSE;
1186 }
1187 }
Barry Mienydd671972010-10-04 16:33:58 +02001188
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 return TRUE;
1190 }
1191
1192 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001193
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 /**
1195 * Validate IP Address
1196 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 * @param string
Andrey Andreev5a257182012-06-10 06:18:14 +03001198 * @param string 'ipv4' or 'ipv6' to validate a specific IP format
Bo-Yi Wu013c8952011-09-12 15:03:44 +08001199 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 */
Andrey Andreev5a257182012-06-10 06:18:14 +03001201 public function valid_ip($ip, $which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 {
Andrey Andreev5a257182012-06-10 06:18:14 +03001203 return $this->CI->input->valid_ip($ip, $which);
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 }
1205
1206 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001207
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 /**
1209 * Alpha
1210 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 * @param string
1212 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001213 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001214 public function alpha($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001216 return ctype_alpha($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 }
Barry Mienydd671972010-10-04 16:33:58 +02001218
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001220
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 /**
1222 * Alpha-numeric
1223 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 * @param string
1225 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001226 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001227 public function alpha_numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001229 return ctype_alnum((string) $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001233
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 /**
1235 * Alpha-numeric with underscores and dashes
1236 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 * @param string
1238 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001239 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001240 public function alpha_dash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001242 return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 }
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001246
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 /**
1248 * Numeric
1249 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 * @param string
1251 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001252 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001253 public function numeric($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 {
Andrey Andreev8dc532d2011-12-24 17:57:54 +02001255 return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001256
1257 }
1258
1259 // --------------------------------------------------------------------
1260
Barry Mienydd671972010-10-04 16:33:58 +02001261 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 * Integer
1263 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 * @param string
1265 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +02001266 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001267 public function integer($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 {
Phil Sturgeonef112c02011-02-07 13:01:47 +00001269 return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
1270 }
1271
1272 // --------------------------------------------------------------------
1273
1274 /**
1275 * Decimal number
1276 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001277 * @param string
1278 * @return bool
1279 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001280 public function decimal($str)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001281 {
1282 return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
1283 }
1284
1285 // --------------------------------------------------------------------
1286
1287 /**
Andrey Andreevc68905a2012-02-02 21:41:54 +02001288 * Greater than
Phil Sturgeonef112c02011-02-07 13:01:47 +00001289 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001290 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001291 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001292 * @return bool
1293 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001294 public function greater_than($str, $min)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001295 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001296 return is_numeric($str) ? ($str > $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001297 }
1298
1299 // --------------------------------------------------------------------
1300
1301 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001302 * Equal to or Greater than
1303 *
Nick Busey98c347d2012-02-02 11:07:03 -07001304 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001305 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001306 * @return bool
1307 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001308 public function greater_than_equal_to($str, $min)
Nick Busey98c347d2012-02-02 11:07:03 -07001309 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001310 return is_numeric($str) ? ($str >= $min) : FALSE;
Phil Sturgeonef112c02011-02-07 13:01:47 +00001311 }
1312
1313 // --------------------------------------------------------------------
1314
1315 /**
1316 * Less than
1317 *
Phil Sturgeonef112c02011-02-07 13:01:47 +00001318 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001319 * @param int
Phil Sturgeonef112c02011-02-07 13:01:47 +00001320 * @return bool
1321 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001322 public function less_than($str, $max)
Phil Sturgeonef112c02011-02-07 13:01:47 +00001323 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001324 return is_numeric($str) ? ($str < $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001326
1327 // --------------------------------------------------------------------
1328
Barry Mienydd671972010-10-04 16:33:58 +02001329 /**
Nick Busey98c347d2012-02-02 11:07:03 -07001330 * Equal to or Less than
1331 *
Nick Busey98c347d2012-02-02 11:07:03 -07001332 * @param string
Timothy Warren0688ac92012-04-20 10:25:04 -04001333 * @param int
Nick Busey98c347d2012-02-02 11:07:03 -07001334 * @return bool
1335 */
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001336 public function less_than_equal_to($str, $max)
Nick Busey98c347d2012-02-02 11:07:03 -07001337 {
Andrey Andreev78f55772012-04-03 19:59:08 +03001338 return is_numeric($str) ? ($str <= $max) : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 }
1340
1341 // --------------------------------------------------------------------
1342
Barry Mienydd671972010-10-04 16:33:58 +02001343 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001344 * Is a Natural number (0,1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001345 *
Barry Mienydd671972010-10-04 16:33:58 +02001346 * @param string
1347 * @return bool
1348 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001349 public function is_natural($str)
Barry Mienydd671972010-10-04 16:33:58 +02001350 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001351 return ctype_digit((string) $str);
Barry Mienydd671972010-10-04 16:33:58 +02001352 }
1353
1354 // --------------------------------------------------------------------
1355
1356 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001357 * Is a Natural number, but not a zero (1,2,3, etc.)
Barry Mienydd671972010-10-04 16:33:58 +02001358 *
Barry Mienydd671972010-10-04 16:33:58 +02001359 * @param string
1360 * @return bool
1361 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001362 public function is_natural_no_zero($str)
Barry Mienydd671972010-10-04 16:33:58 +02001363 {
Andrey Andreevacb962e2012-06-27 12:04:24 +03001364 return ($str != 0 && ctype_digit((string) $str));
Barry Mienydd671972010-10-04 16:33:58 +02001365 }
1366
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001368
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 /**
1370 * Valid Base64
1371 *
1372 * Tests a string for characters outside of the Base64 alphabet
1373 * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
1374 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 * @param string
1376 * @return bool
1377 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001378 public function valid_base64($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 {
Andrey Andreev56454792012-05-17 14:32:19 +03001380 return ! preg_match('/[^a-zA-Z0-9\/\+=]/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 }
Barry Mienydd671972010-10-04 16:33:58 +02001382
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001384
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 /**
1386 * Prep data for form
1387 *
1388 * This function allows HTML to be safely shown in a form.
1389 * Special characters are converted.
1390 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 * @param string
1392 * @return string
1393 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001394 public function prep_for_form($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +02001396 if ($this->_safe_form_data === FALSE OR empty($data))
1397 {
1398 return $data;
1399 }
1400
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 if (is_array($data))
1402 {
1403 foreach ($data as $key => $val)
1404 {
1405 $data[$key] = $this->prep_for_form($val);
1406 }
Barry Mienydd671972010-10-04 16:33:58 +02001407
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 return $data;
1409 }
Barry Mienydd671972010-10-04 16:33:58 +02001410
Andrey Andreev901573c2012-01-11 01:40:48 +02001411 return str_replace(array("'", '"', '<', '>'), array('&#39;', '&quot;', '&lt;', '&gt;'), stripslashes($data));
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 }
Barry Mienydd671972010-10-04 16:33:58 +02001413
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001415
Derek Allard2067d1a2008-11-13 22:59:24 +00001416 /**
1417 * Prep URL
1418 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 * @param string
1420 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001421 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001422 public function prep_url($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001423 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001424 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 {
1426 return '';
1427 }
Barry Mienydd671972010-10-04 16:33:58 +02001428
Andrey Andreev901573c2012-01-11 01:40:48 +02001429 if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 {
Andrey Andreev901573c2012-01-11 01:40:48 +02001431 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 }
Barry Mienydd671972010-10-04 16:33:58 +02001433
Derek Allard2067d1a2008-11-13 22:59:24 +00001434 return $str;
1435 }
Barry Mienydd671972010-10-04 16:33:58 +02001436
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001438
Derek Allard2067d1a2008-11-13 22:59:24 +00001439 /**
1440 * Strip Image Tags
1441 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 * @param string
1443 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001444 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001445 public function strip_image_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 {
Andrey Andreev1a24a9d2012-06-27 00:52:47 +03001447 return $this->CI->security->strip_image_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 }
Barry Mienydd671972010-10-04 16:33:58 +02001449
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 * XSS Clean
1454 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 * @param string
1456 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001457 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001458 public function xss_clean($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 {
Derek Jones5640a712010-04-23 11:22:40 -05001460 return $this->CI->security->xss_clean($str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 }
Barry Mienydd671972010-10-04 16:33:58 +02001462
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001464
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 /**
1466 * Convert PHP tags to entities
1467 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 * @param string
1469 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001470 */
Phil Sturgeon3837ae72011-05-09 21:12:26 +01001471 public function encode_php_tags($str)
Derek Allard2067d1a2008-11-13 22:59:24 +00001472 {
Andrey Andreev838a9d62012-12-03 14:37:47 +02001473 return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 }
1475
JonoB099c4782012-03-04 14:37:30 +00001476 // --------------------------------------------------------------------
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001477
1478 /**
1479 * Reset validation vars
1480 *
1481 * Prevents subsequent validation routines from being affected by the
JonoB099c4782012-03-04 14:37:30 +00001482 * results of any previous validation routine due to the CI singleton.
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001483 *
Andrey Andreev3b2c5082012-03-07 22:49:24 +02001484 * @return void
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001485 */
JonoB883f80f2012-03-05 09:51:27 +00001486 public function reset_validation()
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001487 {
JonoB099c4782012-03-04 14:37:30 +00001488 $this->_field_data = array();
1489 $this->_config_rules = array();
1490 $this->_error_array = array();
1491 $this->_error_messages = array();
1492 $this->error_string = '';
Andrey Andreevc8da4fe2012-03-04 19:20:33 +02001493 }
1494
Derek Allard2067d1a2008-11-13 22:59:24 +00001495}
Derek Allard2067d1a2008-11-13 22:59:24 +00001496
1497/* End of file Form_validation.php */
Eric Roberts24a13f52012-12-12 07:09:42 -06001498/* Location: ./system/libraries/Form_validation.php */