Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Eric Barnes | cccde96 | 2011-12-04 00:01:17 -0500 | [diff] [blame] | 8 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2015, British Columbia Institute of Technology |
Eric Barnes | cccde96 | 2011-12-04 00:01:17 -0500 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
| 34 | * @link http://codeigniter.com |
| 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Form Validation Class |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Validation |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | * @link http://codeigniter.com/user_guide/libraries/form_validation.html |
| 48 | */ |
| 49 | class CI_Form_validation { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 50 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 51 | /** |
| 52 | * Reference to the CodeIgniter instance |
| 53 | * |
| 54 | * @var object |
| 55 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 56 | protected $CI; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 58 | /** |
| 59 | * Validation data for the current form submission |
| 60 | * |
| 61 | * @var array |
| 62 | */ |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 63 | protected $_field_data = array(); |
| 64 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 65 | /** |
| 66 | * Validation rules for the current form |
| 67 | * |
| 68 | * @var array |
| 69 | */ |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 70 | protected $_config_rules = array(); |
| 71 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 72 | /** |
| 73 | * Array of validation errors |
| 74 | * |
| 75 | * @var array |
| 76 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 77 | protected $_error_array = array(); |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 78 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 79 | /** |
| 80 | * Array of custom error messages |
| 81 | * |
| 82 | * @var array |
| 83 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 84 | protected $_error_messages = array(); |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 85 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 86 | /** |
| 87 | * Start tag for error wrapping |
| 88 | * |
| 89 | * @var string |
| 90 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 91 | protected $_error_prefix = '<p>'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 92 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 93 | /** |
| 94 | * End tag for error wrapping |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 95 | * |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 96 | * @var string |
| 97 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 98 | protected $_error_suffix = '</p>'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 99 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 100 | /** |
| 101 | * Custom error message |
| 102 | * |
| 103 | * @var string |
| 104 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 105 | protected $error_string = ''; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 106 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 107 | /** |
| 108 | * Whether the form data has been validated as safe |
| 109 | * |
| 110 | * @var bool |
| 111 | */ |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 112 | protected $_safe_form_data = FALSE; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 113 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 114 | /** |
| 115 | * Custom data to validate |
| 116 | * |
| 117 | * @var array |
| 118 | */ |
Andrey Andreev | cff3580 | 2012-11-26 15:49:52 +0200 | [diff] [blame] | 119 | public $validation_data = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 121 | /** |
| 122 | * Initialize Form_Validation class |
| 123 | * |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 124 | * @param array $rules |
| 125 | * @return void |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 126 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 127 | public function __construct($rules = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 128 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | $this->CI =& get_instance(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | |
Mike Funk | 326a5e7 | 2012-02-24 10:06:28 -0500 | [diff] [blame] | 131 | // applies delimiters set in config file. |
Mike Funk | 7f42d06 | 2012-03-08 09:00:57 -0500 | [diff] [blame] | 132 | if (isset($rules['error_prefix'])) |
| 133 | { |
| 134 | $this->_error_prefix = $rules['error_prefix']; |
| 135 | unset($rules['error_prefix']); |
| 136 | } |
| 137 | if (isset($rules['error_suffix'])) |
| 138 | { |
| 139 | $this->_error_suffix = $rules['error_suffix']; |
| 140 | unset($rules['error_suffix']); |
| 141 | } |
Andrey Andreev | 31cf46e | 2012-03-20 15:48:00 +0200 | [diff] [blame] | 142 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | // Validation rules can be stored in a config file. |
| 144 | $this->_config_rules = $rules; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | // Automatically load the form helper |
| 147 | $this->CI->load->helper('form'); |
| 148 | |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 149 | log_message('info', 'Form Validation Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | /** |
| 155 | * Set Rules |
| 156 | * |
| 157 | * This function takes an array of field names and validation |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 158 | * rules as input, any custom error messages, validates the info, |
Ahmedul Haque Abid | 42b4000 | 2014-01-09 01:10:25 +0600 | [diff] [blame] | 159 | * and stores it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | * |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 161 | * @param mixed $field |
| 162 | * @param string $label |
| 163 | * @param mixed $rules |
Ahmedul Haque Abid | 0742fad | 2014-01-09 07:51:10 +0600 | [diff] [blame] | 164 | * @param array $errors |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 165 | * @return CI_Form_validation |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | */ |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 167 | public function set_rules($field, $label = '', $rules = array(), $errors = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | { |
| 169 | // No reason to set rules if we have no POST data |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 170 | // or a validation array has not been specified |
Andrey Andreev | 3b2c508 | 2012-03-07 22:49:24 +0200 | [diff] [blame] | 171 | if ($this->CI->input->method() !== 'post' && empty($this->validation_data)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | { |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 173 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | |
tiyowan | c2acb23 | 2012-03-14 21:24:00 +0400 | [diff] [blame] | 176 | // If an array was passed via the first parameter instead of individual string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | // values we cycle through it and recursively call this function. |
| 178 | if (is_array($field)) |
| 179 | { |
| 180 | foreach ($field as $row) |
| 181 | { |
| 182 | // Houston, we have a problem... |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 183 | if ( ! isset($row['field'], $row['rules'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | { |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | // If the field label wasn't passed we use the field name |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 189 | $label = isset($row['label']) ? $row['label'] : $row['field']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | |
Ahmedul Haque Abid | 42b4000 | 2014-01-09 01:10:25 +0600 | [diff] [blame] | 191 | // Add the custom error message array |
Ahmedul Haque Abid | ea29488 | 2014-01-09 16:01:31 +0600 | [diff] [blame] | 192 | $errors = (isset($row['errors']) && is_array($row['errors'])) ? $row['errors'] : array(); |
Ahmedul Haque Abid | 42b4000 | 2014-01-09 01:10:25 +0600 | [diff] [blame] | 193 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | // Here we go! |
Ahmedul Haque Abid | bc1cbad | 2014-01-09 07:53:34 +0600 | [diff] [blame] | 195 | $this->set_rules($row['field'], $label, $row['rules'], $errors); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | } |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 197 | |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 198 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | // No fields? Nothing to do... |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 202 | if ( ! is_string($field) OR $field === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | { |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 204 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | } |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 206 | elseif ( ! is_array($rules)) |
| 207 | { |
| 208 | // BC: Convert pipe-separated rules string to an array |
| 209 | if (is_string($rules)) |
| 210 | { |
| 211 | $rules = explode('|', $rules); |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | return $this; |
| 216 | } |
| 217 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | |
| 219 | // If the field label wasn't passed we use the field name |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 220 | $label = ($label === '') ? $field : $label; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | |
Andrey Andreev | fde170c | 2014-03-10 19:55:11 +0200 | [diff] [blame] | 222 | $indexes = array(); |
| 223 | |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 224 | // Is the field name an array? If it is an array, we break it apart |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | // into its components so that we can fetch the corresponding POST data later |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 226 | if (($is_array = (bool) preg_match_all('/\[(.*?)\]/', $field, $matches)) === TRUE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 227 | { |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 228 | sscanf($field, '%[^[][', $indexes[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 230 | for ($i = 0, $c = count($matches[0]); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 232 | if ($matches[1][$i] !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 234 | $indexes[] = $matches[1][$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 238 | |
| 239 | // Build our master array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | $this->_field_data[$field] = array( |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 241 | 'field' => $field, |
| 242 | 'label' => $label, |
| 243 | 'rules' => $rules, |
Ahmedul Haque Abid | 7945d30 | 2014-01-09 16:50:23 +0600 | [diff] [blame] | 244 | 'errors' => $errors, |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 245 | 'is_array' => $is_array, |
| 246 | 'keys' => $indexes, |
| 247 | 'postdata' => NULL, |
| 248 | 'error' => '' |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 249 | ); |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 250 | |
| 251 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | /** |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 257 | * By default, form validation uses the $_POST array to validate |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 258 | * |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 259 | * If an array is set through this method, then this array will |
| 260 | * be used instead of the $_POST array |
Andrey Andreev | 3b2c508 | 2012-03-07 22:49:24 +0200 | [diff] [blame] | 261 | * |
| 262 | * Note that if you are validating multiple arrays, then the |
| 263 | * reset_validation() function should be called after validating |
JonoB | 883f80f | 2012-03-05 09:51:27 +0000 | [diff] [blame] | 264 | * each array due to the limitations of CI's singleton |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 265 | * |
| 266 | * @param array $data |
Andrey Andreev | a89c1da | 2014-02-08 19:03:35 +0200 | [diff] [blame] | 267 | * @return CI_Form_validation |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 268 | */ |
Andrey Andreev | a4712f5 | 2014-01-06 11:25:46 +0200 | [diff] [blame] | 269 | public function set_data(array $data) |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 270 | { |
Andrey Andreev | a4712f5 | 2014-01-06 11:25:46 +0200 | [diff] [blame] | 271 | if ( ! empty($data)) |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 272 | { |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 273 | $this->validation_data = $data; |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 274 | } |
Andrey Andreev | a89c1da | 2014-02-08 19:03:35 +0200 | [diff] [blame] | 275 | |
| 276 | return $this; |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 277 | } |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 278 | |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 279 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | |
| 281 | /** |
| 282 | * Set Error Message |
| 283 | * |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 284 | * Lets users set their own error messages on the fly. Note: |
| 285 | * The key name has to match the function name that it corresponds to. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | * |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 287 | * @param array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 289 | * @return CI_Form_validation |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 291 | public function set_message($lang, $val = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | { |
| 293 | if ( ! is_array($lang)) |
| 294 | { |
| 295 | $lang = array($lang => $val); |
| 296 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 297 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | $this->_error_messages = array_merge($this->_error_messages, $lang); |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 299 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 301 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 303 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | /** |
| 305 | * Set The Error Delimiter |
| 306 | * |
| 307 | * Permits a prefix/suffix to be added to each error message |
| 308 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | * @param string |
| 310 | * @param string |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 311 | * @return CI_Form_validation |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 312 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 313 | public function set_error_delimiters($prefix = '<p>', $suffix = '</p>') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | { |
| 315 | $this->_error_prefix = $prefix; |
| 316 | $this->_error_suffix = $suffix; |
Greg Aker | 9f9af60 | 2010-11-10 15:41:51 -0600 | [diff] [blame] | 317 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 321 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | /** |
| 323 | * Get Error Message |
| 324 | * |
| 325 | * Gets the error message associated with a particular field |
| 326 | * |
Andrey Andreev | a4712f5 | 2014-01-06 11:25:46 +0200 | [diff] [blame] | 327 | * @param string $field Field name |
| 328 | * @param string $prefix HTML start tag |
Andrey Andreev | 868301a | 2014-01-06 12:29:50 +0200 | [diff] [blame] | 329 | * @param string $suffix HTML end tag |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 330 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | */ |
Andrey Andreev | a4712f5 | 2014-01-06 11:25:46 +0200 | [diff] [blame] | 332 | public function error($field, $prefix = '', $suffix = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 334 | if (empty($this->_field_data[$field]['error'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | { |
| 336 | return ''; |
| 337 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 338 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 339 | if ($prefix === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | { |
| 341 | $prefix = $this->_error_prefix; |
| 342 | } |
| 343 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 344 | if ($suffix === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | { |
| 346 | $suffix = $this->_error_suffix; |
| 347 | } |
| 348 | |
| 349 | return $prefix.$this->_field_data[$field]['error'].$suffix; |
| 350 | } |
| 351 | |
| 352 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 353 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | /** |
Michiel Vugteveen | 676a0dd | 2012-03-02 10:10:34 +0100 | [diff] [blame] | 355 | * Get Array of Error Messages |
| 356 | * |
| 357 | * Returns the error messages as an array |
| 358 | * |
| 359 | * @return array |
| 360 | */ |
| 361 | public function error_array() |
| 362 | { |
| 363 | return $this->_error_array; |
| 364 | } |
| 365 | |
| 366 | // -------------------------------------------------------------------- |
| 367 | |
| 368 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | * Error String |
| 370 | * |
| 371 | * Returns the error messages as a string, wrapped in the error delimiters |
| 372 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | * @param string |
| 374 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 375 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 376 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 377 | public function error_string($prefix = '', $suffix = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | { |
vlakoff | 3567246 | 2013-02-15 01:36:04 +0100 | [diff] [blame] | 379 | // No errors, validation passes! |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | if (count($this->_error_array) === 0) |
| 381 | { |
| 382 | return ''; |
| 383 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 384 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 385 | if ($prefix === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | { |
| 387 | $prefix = $this->_error_prefix; |
| 388 | } |
| 389 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 390 | if ($suffix === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | { |
| 392 | $suffix = $this->_error_suffix; |
| 393 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 394 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | // Generate the error string |
| 396 | $str = ''; |
| 397 | foreach ($this->_error_array as $val) |
| 398 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 399 | if ($val !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | { |
| 401 | $str .= $prefix.$val.$suffix."\n"; |
| 402 | } |
| 403 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 404 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | return $str; |
| 406 | } |
| 407 | |
| 408 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 409 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | /** |
| 411 | * Run the Validator |
| 412 | * |
| 413 | * This function does all the work. |
| 414 | * |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 415 | * @param string $group |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 417 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 418 | public function run($group = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 420 | // Do we even have any data to process? Mm? |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 421 | $validation_array = empty($this->validation_data) ? $_POST : $this->validation_data; |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 422 | if (count($validation_array) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | { |
| 424 | return FALSE; |
| 425 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 426 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | // Does the _field_data array containing the validation rules exist? |
| 428 | // If not, we look to see if they were assigned via a config file |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 429 | if (count($this->_field_data) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 431 | // No validation rules? We're done... |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 432 | if (count($this->_config_rules) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | { |
| 434 | return FALSE; |
| 435 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 436 | |
Andrey Andreev | 3b2803e | 2014-01-07 14:46:38 +0200 | [diff] [blame] | 437 | if (empty($group)) |
| 438 | { |
| 439 | // Is there a validation rule for the particular URI being accessed? |
| 440 | $group = trim($this->CI->uri->ruri_string(), '/'); |
| 441 | isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method; |
| 442 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 443 | |
Andrey Andreev | 3b2803e | 2014-01-07 14:46:38 +0200 | [diff] [blame] | 444 | $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 446 | // Were we able to set the rules correctly? |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 447 | if (count($this->_field_data) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 449 | log_message('debug', 'Unable to find validation rules'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | return FALSE; |
| 451 | } |
| 452 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 453 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 454 | // Load the language file containing error messages |
| 455 | $this->CI->lang->load('form_validation'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 456 | |
Andrey Andreev | 751f247 | 2012-11-03 18:26:27 +0200 | [diff] [blame] | 457 | // Cycle through the rules for each field and match the corresponding $validation_data item |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | foreach ($this->_field_data as $field => $row) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 459 | { |
Andrey Andreev | 751f247 | 2012-11-03 18:26:27 +0200 | [diff] [blame] | 460 | // Fetch the data from the validation_data array item and cache it in the _field_data array. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | // Depending on whether the field name is an array or a string will determine where we get it from. |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 462 | if ($row['is_array'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | { |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 464 | $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | } |
Andrey Andreev | fff6c2a | 2012-05-13 22:15:23 +0300 | [diff] [blame] | 466 | elseif (isset($validation_array[$field]) && $validation_array[$field] !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 468 | $this->_field_data[$field]['postdata'] = $validation_array[$field]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | } |
Andrey Andreev | 751f247 | 2012-11-03 18:26:27 +0200 | [diff] [blame] | 470 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 471 | |
Andrey Andreev | 751f247 | 2012-11-03 18:26:27 +0200 | [diff] [blame] | 472 | // Execute validation rules |
| 473 | // Note: A second foreach (for now) is required in order to avoid false-positives |
| 474 | // for rules like 'matches', which correlate to other validation fields. |
| 475 | foreach ($this->_field_data as $field => $row) |
| 476 | { |
Andrey Andreev | 3d9cec9 | 2012-07-08 21:50:19 +0300 | [diff] [blame] | 477 | // Don't try to validate if we have no rules set |
| 478 | if (empty($row['rules'])) |
| 479 | { |
| 480 | continue; |
| 481 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 483 | $this->_execute($row, $row['rules'], $this->_field_data[$field]['postdata']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | // Did we end up with any errors? |
| 487 | $total_errors = count($this->_error_array); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | if ($total_errors > 0) |
| 489 | { |
| 490 | $this->_safe_form_data = TRUE; |
| 491 | } |
| 492 | |
| 493 | // Now we need to re-set the POST data with the new, processed data |
| 494 | $this->_reset_post_array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 495 | |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 496 | return ($total_errors === 0); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 500 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | /** |
| 502 | * Traverse a multidimensional $_POST array index until the data is found |
| 503 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | * @param array |
| 505 | * @param array |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 506 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 508 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 509 | protected function _reduce_array($array, $keys, $i = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 511 | if (is_array($array) && isset($keys[$i])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 512 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 513 | return isset($array[$keys[$i]]) ? $this->_reduce_array($array[$keys[$i]], $keys, ($i+1)) : NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 514 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 515 | |
Andrey Andreev | 2d48b4f | 2012-11-23 17:33:21 +0200 | [diff] [blame] | 516 | // NULL must be returned for empty fields |
Andrey Andreev | 44c3463 | 2012-11-23 18:46:34 +0200 | [diff] [blame] | 517 | return ($array === '') ? NULL : $array; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 521 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | /** |
| 523 | * Re-populate the _POST array with our finalized and processed data |
| 524 | * |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 525 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 526 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 527 | protected function _reset_post_array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 528 | { |
| 529 | foreach ($this->_field_data as $field => $row) |
| 530 | { |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 531 | if ($row['postdata'] !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 532 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 533 | if ($row['is_array'] === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 534 | { |
| 535 | if (isset($_POST[$row['field']])) |
| 536 | { |
Andrey Andreev | c226871 | 2013-02-08 22:10:23 +0200 | [diff] [blame] | 537 | $_POST[$row['field']] = $row['postdata']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | else |
| 541 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 542 | // start with a reference |
| 543 | $post_ref =& $_POST; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 544 | |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 545 | // before we assign values, make a reference to the right POST key |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 546 | if (count($row['keys']) === 1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 547 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 548 | $post_ref =& $post_ref[current($row['keys'])]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 549 | } |
| 550 | else |
| 551 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | foreach ($row['keys'] as $val) |
| 553 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 554 | $post_ref =& $post_ref[$val]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 557 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 558 | if (is_array($row['postdata'])) |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 559 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | $array = array(); |
| 561 | foreach ($row['postdata'] as $k => $v) |
| 562 | { |
Andrey Andreev | c226871 | 2013-02-08 22:10:23 +0200 | [diff] [blame] | 563 | $array[$k] = $v; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 564 | } |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 565 | |
| 566 | $post_ref = $array; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 567 | } |
| 568 | else |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 569 | { |
Andrey Andreev | c226871 | 2013-02-08 22:10:23 +0200 | [diff] [blame] | 570 | $post_ref = $row['postdata']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 571 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 578 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 579 | /** |
| 580 | * Executes the Validation routines |
| 581 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | * @param array |
| 583 | * @param array |
| 584 | * @param mixed |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 585 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 586 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 587 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 588 | protected function _execute($row, $rules, $postdata = NULL, $cycles = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 589 | { |
| 590 | // If the $_POST data is an array we will run a recursive call |
| 591 | if (is_array($postdata)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 592 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 593 | foreach ($postdata as $key => $val) |
| 594 | { |
Andrey Andreev | 8d3099d | 2012-06-21 16:00:20 +0300 | [diff] [blame] | 595 | $this->_execute($row, $rules, $val, $key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 596 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 597 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 598 | return; |
| 599 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 600 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 601 | // If the field is blank, but NOT required, no further tests are necessary |
| 602 | $callback = FALSE; |
Hashem Qolami | 05370bf | 2013-07-22 01:52:04 +0430 | [diff] [blame] | 603 | if ( ! in_array('required', $rules) && ($postdata === NULL OR $postdata === '')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 604 | { |
| 605 | // Before we bail out, does the rule contain a callback? |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 606 | foreach ($rules as &$rule) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 607 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 608 | if (is_string($rule)) |
| 609 | { |
| 610 | if (strncmp($rule, 'callback_', 9) === 0) |
| 611 | { |
| 612 | $callback = TRUE; |
| 613 | $rules = array(1 => $rule); |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | elseif (is_callable($rule)) |
| 618 | { |
| 619 | $callback = TRUE; |
| 620 | $rules = array(1 => $rule); |
| 621 | break; |
| 622 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 623 | } |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 624 | |
| 625 | if ( ! $callback) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 626 | { |
| 627 | return; |
| 628 | } |
| 629 | } |
| 630 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 631 | // Isset Test. Typically this rule will only apply to checkboxes. |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 632 | if (($postdata === NULL OR $postdata === '') && ! $callback) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 633 | { |
| 634 | if (in_array('isset', $rules, TRUE) OR in_array('required', $rules)) |
| 635 | { |
| 636 | // Set the message type |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 637 | $type = in_array('required', $rules) ? 'required' : 'isset'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 638 | |
Ahmedul Haque Abid | e9b0ccc | 2014-01-09 15:58:51 +0600 | [diff] [blame] | 639 | // Check if a custom message is defined |
Ahmedul Haque Abid | ea29488 | 2014-01-09 16:01:31 +0600 | [diff] [blame] | 640 | if (isset($this->_field_data[$row['field']]['errors'][$type])) |
Ahmedul Haque Abid | 42b4000 | 2014-01-09 01:10:25 +0600 | [diff] [blame] | 641 | { |
Ahmedul Haque Abid | ea29488 | 2014-01-09 16:01:31 +0600 | [diff] [blame] | 642 | $line = $this->_field_data[$row['field']]['errors'][$type]; |
Ahmedul Haque Abid | 42b4000 | 2014-01-09 01:10:25 +0600 | [diff] [blame] | 643 | } |
| 644 | elseif (isset($this->_error_messages[$type])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 645 | { |
| 646 | $line = $this->_error_messages[$type]; |
| 647 | } |
Andrey Andreev | d4eec9f | 2012-12-14 11:07:13 +0200 | [diff] [blame] | 648 | elseif (FALSE === ($line = $this->CI->lang->line('form_validation_'.$type)) |
| 649 | // DEPRECATED support for non-prefixed keys |
| 650 | && FALSE === ($line = $this->CI->lang->line($type, FALSE))) |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 651 | { |
| 652 | $line = 'The field was not set'; |
| 653 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 654 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | // Build the error message |
Eric Roberts | 41cc090 | 2012-01-24 00:59:44 -0600 | [diff] [blame] | 656 | $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label'])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 657 | |
| 658 | // Save the error message |
| 659 | $this->_field_data[$row['field']]['error'] = $message; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 660 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 661 | if ( ! isset($this->_error_array[$row['field']])) |
| 662 | { |
| 663 | $this->_error_array[$row['field']] = $message; |
| 664 | } |
| 665 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 666 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 667 | return; |
| 668 | } |
| 669 | |
| 670 | // -------------------------------------------------------------------- |
| 671 | |
| 672 | // Cycle through each rule and run it |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 673 | foreach ($rules as $rule) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 674 | { |
| 675 | $_in_array = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 676 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 677 | // We set the $postdata variable with the current data in our master array so that |
| 678 | // each cycle of the loop is dealing with the processed data from the last cycle |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 679 | if ($row['is_array'] === TRUE && is_array($this->_field_data[$row['field']]['postdata'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 680 | { |
| 681 | // We shouldn't need this safety, but just in case there isn't an array index |
| 682 | // associated with this cycle we'll bail out |
| 683 | if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles])) |
| 684 | { |
| 685 | continue; |
| 686 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 687 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 688 | $postdata = $this->_field_data[$row['field']]['postdata'][$cycles]; |
| 689 | $_in_array = TRUE; |
| 690 | } |
| 691 | else |
| 692 | { |
Andrey Andreev | 6ac5144 | 2012-06-18 13:05:17 +0300 | [diff] [blame] | 693 | // If we get an array field, but it's not expected - then it is most likely |
| 694 | // somebody messing with the form on the client side, so we'll just consider |
| 695 | // it an empty field |
| 696 | $postdata = is_array($this->_field_data[$row['field']]['postdata']) |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 697 | ? NULL |
| 698 | : $this->_field_data[$row['field']]['postdata']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 701 | // Is the rule a callback? |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 702 | $callback = $callable = FALSE; |
| 703 | if (is_string($rule)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 704 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 705 | if (strpos($rule, 'callback_') === 0) |
| 706 | { |
| 707 | $rule = substr($rule, 9); |
| 708 | $callback = TRUE; |
| 709 | } |
| 710 | } |
| 711 | elseif (is_callable($rule)) |
| 712 | { |
| 713 | $callable = TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 714 | } |
Andrey Andreev | 60726ef | 2014-09-08 11:31:48 +0300 | [diff] [blame] | 715 | elseif (is_array($rule) && isset($rule[0], $rule[1]) && is_callable($rule[1])) |
| 716 | { |
| 717 | // We have a "named" callable, so save the name |
| 718 | $callable = $rule[0]; |
| 719 | $rule = $rule[1]; |
| 720 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 721 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 722 | // Strip the parameter (if exists) from the rule |
| 723 | // Rules can contain a parameter: max_length[5] |
| 724 | $param = FALSE; |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 725 | if ( ! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 726 | { |
Andrey Andreev | ef758bd | 2012-11-15 12:24:52 +0200 | [diff] [blame] | 727 | $rule = $match[1]; |
| 728 | $param = $match[2]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 729 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 730 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 731 | // Call the function that corresponds to the rule |
Andrey Andreev | 60726ef | 2014-09-08 11:31:48 +0300 | [diff] [blame] | 732 | if ($callback OR $callable !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 733 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 734 | if ($callback) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 735 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 736 | if ( ! method_exists($this->CI, $rule)) |
| 737 | { |
| 738 | log_message('debug', 'Unable to find callback validation rule: '.$rule); |
| 739 | $result = FALSE; |
| 740 | } |
| 741 | else |
| 742 | { |
| 743 | // Run the function and grab the result |
| 744 | $result = $this->CI->$rule($postdata, $param); |
| 745 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 746 | } |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 747 | else |
| 748 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 749 | $result = is_array($rule) |
Andrey Andreev | 60726ef | 2014-09-08 11:31:48 +0300 | [diff] [blame] | 750 | ? $rule[0]->{$rule[1]}($postdata) |
| 751 | : $rule($postdata); |
| 752 | |
| 753 | // Is $callable set to a rule name? |
| 754 | if ($callable !== FALSE) |
| 755 | { |
| 756 | $rule = $callable; |
| 757 | } |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 758 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 759 | |
| 760 | // Re-assign the result to the master data array |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 761 | if ($_in_array === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 762 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 763 | $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 764 | } |
| 765 | else |
| 766 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 767 | $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 768 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 769 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 770 | // If the field isn't required and we just processed a callback we'll move on... |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 771 | if ( ! in_array('required', $rules, TRUE) && $result !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 772 | { |
Derek Allard | 4e5cf1c | 2009-07-06 20:53:41 +0000 | [diff] [blame] | 773 | continue; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 776 | elseif ( ! method_exists($this, $rule)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 777 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 778 | // If our own wrapper function doesn't exist we see if a native PHP function does. |
| 779 | // Users can use any native PHP function call that has one param. |
| 780 | if (function_exists($rule)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 781 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 782 | // Native PHP functions issue warnings if you pass them more parameters than they use |
Andrey Andreev | 320d37c | 2012-04-03 20:21:39 +0300 | [diff] [blame] | 783 | $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 784 | |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 785 | if ($_in_array === TRUE) |
| 786 | { |
| 787 | $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 788 | } |
patwork | 02404a1 | 2011-04-08 15:45:46 +0200 | [diff] [blame] | 789 | else |
| 790 | { |
Andrey Andreev | cec2ba5 | 2012-04-03 20:26:38 +0300 | [diff] [blame] | 791 | $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; |
patwork | 02404a1 | 2011-04-08 15:45:46 +0200 | [diff] [blame] | 792 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 793 | } |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 794 | else |
| 795 | { |
Andrey Andreev | cec2ba5 | 2012-04-03 20:26:38 +0300 | [diff] [blame] | 796 | log_message('debug', 'Unable to find validation rule: '.$rule); |
| 797 | $result = FALSE; |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 798 | } |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 799 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 800 | else |
| 801 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 802 | $result = $this->$rule($postdata, $param); |
| 803 | |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 804 | if ($_in_array === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 805 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 806 | $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 807 | } |
| 808 | else |
| 809 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 810 | $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 813 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 814 | // Did the rule test negatively? If so, grab the error. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 815 | if ($result === FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 816 | { |
Andrey Andreev | 60726ef | 2014-09-08 11:31:48 +0300 | [diff] [blame] | 817 | // Callable rules might not have named error messages |
Andrey Andreev | 57f1005 | 2014-06-07 12:22:37 +0300 | [diff] [blame] | 818 | if ( ! is_string($rule)) |
Ahmedul Haque Abid | 42b4000 | 2014-01-09 01:10:25 +0600 | [diff] [blame] | 819 | { |
Andrey Andreev | 57f1005 | 2014-06-07 12:22:37 +0300 | [diff] [blame] | 820 | return; |
| 821 | } |
| 822 | |
| 823 | // Check if a custom message is defined |
| 824 | if (isset($this->_field_data[$row['field']]['errors'][$rule])) |
| 825 | { |
| 826 | $line = $this->_field_data[$row['field']]['errors'][$rule]; |
| 827 | } |
| 828 | elseif ( ! isset($this->_error_messages[$rule])) |
| 829 | { |
| 830 | if (FALSE === ($line = $this->CI->lang->line('form_validation_'.$rule)) |
| 831 | // DEPRECATED support for non-prefixed keys |
| 832 | && FALSE === ($line = $this->CI->lang->line($rule, FALSE))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 833 | { |
Ahmad Anbar | f42e78c | 2015-01-12 12:18:17 +0200 | [diff] [blame] | 834 | $line = $this->CI->lang->line('form_validation_error_message_not_set'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 835 | } |
Andrey Andreev | 57f1005 | 2014-06-07 12:22:37 +0300 | [diff] [blame] | 836 | } |
| 837 | else |
| 838 | { |
| 839 | $line = $this->_error_messages[$rule]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 840 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 841 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 842 | // Is the parameter we are inserting into the error message the name |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 843 | // of another field? If so we need to grab its "field label" |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 844 | if (isset($this->_field_data[$param], $this->_field_data[$param]['label'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 845 | { |
Pascal Kriete | c189583 | 2009-10-13 12:56:43 +0000 | [diff] [blame] | 846 | $param = $this->_translate_fieldname($this->_field_data[$param]['label']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 847 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 848 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 849 | // Build the error message |
Eric Roberts | 41cc090 | 2012-01-24 00:59:44 -0600 | [diff] [blame] | 850 | $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 851 | |
| 852 | // Save the error message |
| 853 | $this->_field_data[$row['field']]['error'] = $message; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 854 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 855 | if ( ! isset($this->_error_array[$row['field']])) |
| 856 | { |
| 857 | $this->_error_array[$row['field']] = $message; |
| 858 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 859 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 860 | return; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 866 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 867 | /** |
| 868 | * Translate a field name |
| 869 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 870 | * @param string the field name |
| 871 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 872 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 873 | protected function _translate_fieldname($fieldname) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 874 | { |
| 875 | // Do we need to translate the field name? |
| 876 | // We look for the prefix lang: to determine this |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 877 | if (sscanf($fieldname, 'lang:%s', $line) === 1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 878 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 879 | // Were we able to translate the field name? If not we use $line |
Andrey Andreev | d4eec9f | 2012-12-14 11:07:13 +0200 | [diff] [blame] | 880 | if (FALSE === ($fieldname = $this->CI->lang->line('form_validation_'.$line)) |
| 881 | // DEPRECATED support for non-prefixed keys |
| 882 | && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 883 | { |
| 884 | return $line; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | return $fieldname; |
| 889 | } |
| 890 | |
| 891 | // -------------------------------------------------------------------- |
Andrey Andreev | d4eec9f | 2012-12-14 11:07:13 +0200 | [diff] [blame] | 892 | |
Eric Roberts | 41cc090 | 2012-01-24 00:59:44 -0600 | [diff] [blame] | 893 | /** |
| 894 | * Build an error message using the field and param. |
| 895 | * |
| 896 | * @param string The error message line |
| 897 | * @param string A field's human name |
| 898 | * @param mixed A rule's optional parameter |
| 899 | * @return string |
| 900 | */ |
| 901 | protected function _build_error_msg($line, $field = '', $param = '') |
| 902 | { |
| 903 | // Check for %s in the string for legacy support. |
Eric Roberts | 24a13f5 | 2012-12-12 07:09:42 -0600 | [diff] [blame] | 904 | if (strpos($line, '%s') !== FALSE) |
Eric Roberts | 41cc090 | 2012-01-24 00:59:44 -0600 | [diff] [blame] | 905 | { |
| 906 | return sprintf($line, $field, $param); |
| 907 | } |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 908 | |
Eric Roberts | 41cc090 | 2012-01-24 00:59:44 -0600 | [diff] [blame] | 909 | return str_replace(array('{field}', '{param}'), array($field, $param), $line); |
| 910 | } |
| 911 | |
| 912 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 913 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 914 | /** |
nisheeth-barthwal | a7447d2 | 2013-03-21 15:48:10 +0530 | [diff] [blame] | 915 | * Checks if the rule is present within the validator |
| 916 | * |
| 917 | * Permits you to check if a rule is present within the validator |
| 918 | * |
| 919 | * @param string the field name |
| 920 | * @return bool |
| 921 | */ |
| 922 | public function has_rule($field) |
| 923 | { |
| 924 | return isset($this->_field_data[$field]); |
| 925 | } |
| 926 | |
| 927 | // -------------------------------------------------------------------- |
| 928 | |
| 929 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 930 | * Get the value from a form |
| 931 | * |
| 932 | * Permits you to repopulate a form field with the value it was submitted |
| 933 | * with, or, if that value doesn't exist, with the default |
| 934 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 935 | * @param string the field name |
| 936 | * @param string |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 937 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 938 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 939 | public function set_value($field = '', $default = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 940 | { |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 941 | if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 942 | { |
| 943 | return $default; |
| 944 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 945 | |
Phil Sturgeon | 5c56180 | 2011-01-05 16:31:59 +0000 | [diff] [blame] | 946 | // If the data is an array output them one at a time. |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 947 | // E.g: form_input('name[]', set_value('name[]'); |
Phil Sturgeon | 5c56180 | 2011-01-05 16:31:59 +0000 | [diff] [blame] | 948 | if (is_array($this->_field_data[$field]['postdata'])) |
| 949 | { |
| 950 | return array_shift($this->_field_data[$field]['postdata']); |
| 951 | } |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 952 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 953 | return $this->_field_data[$field]['postdata']; |
| 954 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 955 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 956 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 957 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 958 | /** |
| 959 | * Set Select |
| 960 | * |
| 961 | * Enables pull-down lists to be set to the value the user |
| 962 | * selected in the event of an error |
| 963 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 964 | * @param string |
| 965 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 966 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 967 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 968 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 969 | public function set_select($field = '', $value = '', $default = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 970 | { |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 971 | if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 972 | { |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 973 | return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 974 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 975 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 976 | $field = $this->_field_data[$field]['postdata']; |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 977 | $value = (string) $value; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 978 | if (is_array($field)) |
| 979 | { |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 980 | // Note: in_array('', array(0)) returns TRUE, do not use it |
| 981 | foreach ($field as &$v) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 982 | { |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 983 | if ($value === $v) |
| 984 | { |
| 985 | return ' selected="selected"'; |
| 986 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 987 | } |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 988 | |
| 989 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 990 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 991 | elseif (($field === '' OR $value === '') OR ($field !== $value)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 992 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 993 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 994 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 995 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 996 | return ' selected="selected"'; |
| 997 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 998 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 999 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1000 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1001 | /** |
| 1002 | * Set Radio |
| 1003 | * |
| 1004 | * Enables radio buttons to be set to the value the user |
| 1005 | * selected in the event of an error |
| 1006 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1007 | * @param string |
| 1008 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1009 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1010 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1011 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1012 | public function set_radio($field = '', $value = '', $default = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1013 | { |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 1014 | if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1015 | { |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 1016 | return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1017 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1018 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1019 | $field = $this->_field_data[$field]['postdata']; |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 1020 | $value = (string) $value; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1021 | if (is_array($field)) |
| 1022 | { |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 1023 | // Note: in_array('', array(0)) returns TRUE, do not use it |
| 1024 | foreach ($field as &$v) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1025 | { |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 1026 | if ($value === $v) |
| 1027 | { |
| 1028 | return ' checked="checked"'; |
| 1029 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1030 | } |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 1031 | |
| 1032 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1033 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1034 | elseif (($field === '' OR $value === '') OR ($field !== $value)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1035 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1036 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1037 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1038 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1039 | return ' checked="checked"'; |
| 1040 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1041 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1042 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1043 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1044 | /** |
| 1045 | * Set Checkbox |
| 1046 | * |
| 1047 | * Enables checkboxes to be set to the value the user |
| 1048 | * selected in the event of an error |
| 1049 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1050 | * @param string |
| 1051 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1052 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1053 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1054 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1055 | public function set_checkbox($field = '', $value = '', $default = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1056 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1057 | // Logic is exactly the same as for radio fields |
| 1058 | return $this->set_radio($field, $value, $default); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1059 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1060 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1061 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1062 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1063 | /** |
| 1064 | * Required |
| 1065 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1066 | * @param string |
| 1067 | * @return bool |
| 1068 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1069 | public function required($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1070 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1071 | return is_array($str) ? (bool) count($str) : (trim($str) !== ''); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1072 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1073 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1074 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1075 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1076 | /** |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 1077 | * Performs a Regular Expression match test. |
| 1078 | * |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 1079 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1080 | * @param string regex |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 1081 | * @return bool |
| 1082 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1083 | public function regex_match($str, $regex) |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 1084 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1085 | return (bool) preg_match($regex, $str); |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | // -------------------------------------------------------------------- |
| 1089 | |
| 1090 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1091 | * Match one field to another |
| 1092 | * |
Andrey Andreev | a779b2c | 2012-10-26 16:25:47 +0300 | [diff] [blame] | 1093 | * @param string $str string to compare against |
| 1094 | * @param string $field |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1095 | * @return bool |
| 1096 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1097 | public function matches($str, $field) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1098 | { |
Andrey Andreev | a779b2c | 2012-10-26 16:25:47 +0300 | [diff] [blame] | 1099 | return isset($this->_field_data[$field], $this->_field_data[$field]['postdata']) |
| 1100 | ? ($str === $this->_field_data[$field]['postdata']) |
| 1101 | : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1102 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1103 | |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1104 | // -------------------------------------------------------------------- |
| 1105 | |
| 1106 | /** |
Raul Baldner Junior | f38564d | 2012-10-11 11:32:23 -0300 | [diff] [blame] | 1107 | * Differs from another field |
| 1108 | * |
| 1109 | * @param string |
| 1110 | * @param string field |
| 1111 | * @return bool |
| 1112 | */ |
| 1113 | public function differs($str, $field) |
| 1114 | { |
| 1115 | return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1116 | } |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1117 | |
| 1118 | // -------------------------------------------------------------------- |
| 1119 | |
| 1120 | /** |
Andrey Andreev | d09d650 | 2012-01-03 06:38:33 +0200 | [diff] [blame] | 1121 | * Is Unique |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1122 | * |
Andrey Andreev | d09d650 | 2012-01-03 06:38:33 +0200 | [diff] [blame] | 1123 | * Check if the input value doesn't already exist |
| 1124 | * in the specified database field. |
| 1125 | * |
Andrey Andreev | 9a0e0c7 | 2014-04-09 15:10:27 +0300 | [diff] [blame] | 1126 | * @param string $str |
| 1127 | * @param string $field |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1128 | * @return bool |
| 1129 | */ |
| 1130 | public function is_unique($str, $field) |
| 1131 | { |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 1132 | sscanf($field, '%[^.].%[^.]', $table, $field); |
Andrey Andreev | 9a0e0c7 | 2014-04-09 15:10:27 +0300 | [diff] [blame] | 1133 | return isset($this->CI->db) |
| 1134 | ? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0) |
| 1135 | : FALSE; |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 1136 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1137 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1138 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1140 | /** |
| 1141 | * Minimum Length |
| 1142 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1143 | * @param string |
Michiel Vugteveen | a8221ad | 2012-06-14 23:26:34 +0200 | [diff] [blame] | 1144 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1145 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1146 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1147 | public function min_length($str, $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1148 | { |
Michiel Vugteveen | ceaf887 | 2012-06-15 11:56:24 +0200 | [diff] [blame] | 1149 | if ( ! is_numeric($val)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1150 | { |
| 1151 | return FALSE; |
| 1152 | } |
| 1153 | |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1154 | return (MB_ENABLED === TRUE) |
| 1155 | ? ($val <= mb_strlen($str)) |
Michiel Vugteveen | ceaf887 | 2012-06-15 11:56:24 +0200 | [diff] [blame] | 1156 | : ($val <= strlen($str)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1157 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1159 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1160 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1161 | /** |
| 1162 | * Max Length |
| 1163 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1164 | * @param string |
Michiel Vugteveen | a8221ad | 2012-06-14 23:26:34 +0200 | [diff] [blame] | 1165 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1166 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1167 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1168 | public function max_length($str, $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1169 | { |
Michiel Vugteveen | ceaf887 | 2012-06-15 11:56:24 +0200 | [diff] [blame] | 1170 | if ( ! is_numeric($val)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1171 | { |
| 1172 | return FALSE; |
| 1173 | } |
| 1174 | |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1175 | return (MB_ENABLED === TRUE) |
| 1176 | ? ($val >= mb_strlen($str)) |
| 1177 | : ($val >= strlen($str)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1178 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1179 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1180 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1182 | /** |
| 1183 | * Exact Length |
| 1184 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1185 | * @param string |
Michiel Vugteveen | eccde13 | 2012-06-14 23:22:26 +0200 | [diff] [blame] | 1186 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1187 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1188 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1189 | public function exact_length($str, $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1190 | { |
Michiel Vugteveen | ceaf887 | 2012-06-15 11:56:24 +0200 | [diff] [blame] | 1191 | if ( ! is_numeric($val)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1192 | { |
| 1193 | return FALSE; |
| 1194 | } |
| 1195 | |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1196 | return (MB_ENABLED === TRUE) |
Andrey Andreev | 9a0e0c7 | 2014-04-09 15:10:27 +0300 | [diff] [blame] | 1197 | ? (mb_strlen($str) === (int) $val) |
| 1198 | : (strlen($str) === (int) $val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1199 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1200 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1201 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1202 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1203 | /** |
Andrey Andreev | daaca88 | 2012-11-26 22:16:12 +0200 | [diff] [blame] | 1204 | * Valid URL |
| 1205 | * |
| 1206 | * @param string $str |
| 1207 | * @return bool |
| 1208 | */ |
| 1209 | public function valid_url($str) |
| 1210 | { |
| 1211 | if (empty($str)) |
| 1212 | { |
| 1213 | return FALSE; |
| 1214 | } |
| 1215 | elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches)) |
| 1216 | { |
| 1217 | if (empty($matches[2])) |
| 1218 | { |
| 1219 | return FALSE; |
| 1220 | } |
| 1221 | elseif ( ! in_array($matches[1], array('http', 'https'), TRUE)) |
| 1222 | { |
| 1223 | return FALSE; |
| 1224 | } |
| 1225 | |
| 1226 | $str = $matches[2]; |
| 1227 | } |
| 1228 | |
| 1229 | $str = 'http://'.$str; |
| 1230 | |
| 1231 | // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the |
| 1232 | // underscore to be a valid hostname character instead of a dash. |
| 1233 | // Reference: https://bugs.php.net/bug.php?id=51192 |
Andrey Andreev | 9a0e0c7 | 2014-04-09 15:10:27 +0300 | [diff] [blame] | 1234 | if (version_compare(PHP_VERSION, '5.2.13', '==') OR version_compare(PHP_VERSION, '5.3.2', '==')) |
Andrey Andreev | daaca88 | 2012-11-26 22:16:12 +0200 | [diff] [blame] | 1235 | { |
| 1236 | sscanf($str, 'http://%[^/]', $host); |
| 1237 | $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host)); |
| 1238 | } |
| 1239 | |
| 1240 | return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | // -------------------------------------------------------------------- |
| 1244 | |
| 1245 | /** |
| 1246 | * Valid Email |
| 1247 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1248 | * @param string |
| 1249 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1250 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1251 | public function valid_email($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1252 | { |
Andrey Andreev | f9201ae | 2014-06-01 12:57:30 +0300 | [diff] [blame] | 1253 | if (function_exists('idn_to_ascii') && $atpos = strpos($str, '@')) |
Andrey Andreev | 9549666 | 2014-06-01 00:00:13 +0300 | [diff] [blame] | 1254 | { |
Andrey Andreev | f9201ae | 2014-06-01 12:57:30 +0300 | [diff] [blame] | 1255 | $str = substr($str, 0, ++$atpos).idn_to_ascii(substr($str, $atpos)); |
Andrey Andreev | 9549666 | 2014-06-01 00:00:13 +0300 | [diff] [blame] | 1256 | } |
| 1257 | |
Andrey Andreev | 580388b | 2012-06-27 15:43:46 +0300 | [diff] [blame] | 1258 | return (bool) filter_var($str, FILTER_VALIDATE_EMAIL); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1262 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1263 | /** |
| 1264 | * Valid Emails |
| 1265 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1266 | * @param string |
| 1267 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1268 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1269 | public function valid_emails($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1270 | { |
| 1271 | if (strpos($str, ',') === FALSE) |
| 1272 | { |
| 1273 | return $this->valid_email(trim($str)); |
| 1274 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1275 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 1276 | foreach (explode(',', $str) as $email) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1277 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1278 | if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1279 | { |
| 1280 | return FALSE; |
| 1281 | } |
| 1282 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1283 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1284 | return TRUE; |
| 1285 | } |
| 1286 | |
| 1287 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1288 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1289 | /** |
| 1290 | * Validate IP Address |
| 1291 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1292 | * @param string |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 1293 | * @param string 'ipv4' or 'ipv6' to validate a specific IP format |
Bo-Yi Wu | 013c895 | 2011-09-12 15:03:44 +0800 | [diff] [blame] | 1294 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1295 | */ |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 1296 | public function valid_ip($ip, $which = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1297 | { |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 1298 | return $this->CI->input->valid_ip($ip, $which); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1302 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1303 | /** |
| 1304 | * Alpha |
| 1305 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1306 | * @param string |
| 1307 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1308 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1309 | public function alpha($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1310 | { |
Andrey Andreev | acb962e | 2012-06-27 12:04:24 +0300 | [diff] [blame] | 1311 | return ctype_alpha($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1312 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1313 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1314 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1315 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1316 | /** |
| 1317 | * Alpha-numeric |
| 1318 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1319 | * @param string |
| 1320 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1321 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1322 | public function alpha_numeric($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1323 | { |
Andrey Andreev | acb962e | 2012-06-27 12:04:24 +0300 | [diff] [blame] | 1324 | return ctype_alnum((string) $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1325 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1326 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1327 | // -------------------------------------------------------------------- |
Sajan Parikh | 2d1608a | 2013-02-02 08:00:39 -0600 | [diff] [blame] | 1328 | |
| 1329 | /** |
| 1330 | * Alpha-numeric w/ spaces |
| 1331 | * |
| 1332 | * @param string |
| 1333 | * @return bool |
| 1334 | */ |
| 1335 | public function alpha_numeric_spaces($str) |
| 1336 | { |
Sajan Parikh | df3bfed | 2013-02-04 12:25:49 -0600 | [diff] [blame] | 1337 | return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str); |
Sajan Parikh | 2d1608a | 2013-02-02 08:00:39 -0600 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1341 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1342 | /** |
| 1343 | * Alpha-numeric with underscores and dashes |
| 1344 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1345 | * @param string |
| 1346 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1347 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1348 | public function alpha_dash($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1349 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1350 | return (bool) preg_match('/^[a-z0-9_-]+$/i', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1351 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1352 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1353 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1354 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1355 | /** |
| 1356 | * Numeric |
| 1357 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1358 | * @param string |
| 1359 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1360 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1361 | public function numeric($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1362 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1363 | return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1364 | |
| 1365 | } |
| 1366 | |
| 1367 | // -------------------------------------------------------------------- |
| 1368 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1369 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1370 | * Integer |
| 1371 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1372 | * @param string |
| 1373 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1374 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1375 | public function integer($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1376 | { |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1377 | return (bool) preg_match('/^[\-+]?[0-9]+$/', $str); |
| 1378 | } |
| 1379 | |
| 1380 | // -------------------------------------------------------------------- |
| 1381 | |
| 1382 | /** |
| 1383 | * Decimal number |
| 1384 | * |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1385 | * @param string |
| 1386 | * @return bool |
| 1387 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1388 | public function decimal($str) |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1389 | { |
| 1390 | return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str); |
| 1391 | } |
| 1392 | |
| 1393 | // -------------------------------------------------------------------- |
| 1394 | |
| 1395 | /** |
Andrey Andreev | c68905a | 2012-02-02 21:41:54 +0200 | [diff] [blame] | 1396 | * Greater than |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1397 | * |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1398 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1399 | * @param int |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1400 | * @return bool |
| 1401 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1402 | public function greater_than($str, $min) |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1403 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1404 | return is_numeric($str) ? ($str > $min) : FALSE; |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | // -------------------------------------------------------------------- |
| 1408 | |
| 1409 | /** |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1410 | * Equal to or Greater than |
| 1411 | * |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1412 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1413 | * @param int |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1414 | * @return bool |
| 1415 | */ |
Andrey Andreev | 3b2c508 | 2012-03-07 22:49:24 +0200 | [diff] [blame] | 1416 | public function greater_than_equal_to($str, $min) |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1417 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1418 | return is_numeric($str) ? ($str >= $min) : FALSE; |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | // -------------------------------------------------------------------- |
| 1422 | |
| 1423 | /** |
| 1424 | * Less than |
| 1425 | * |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1426 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1427 | * @param int |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1428 | * @return bool |
| 1429 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1430 | public function less_than($str, $max) |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1431 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1432 | return is_numeric($str) ? ($str < $max) : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1433 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1434 | |
| 1435 | // -------------------------------------------------------------------- |
| 1436 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1437 | /** |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1438 | * Equal to or Less than |
| 1439 | * |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1440 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1441 | * @param int |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1442 | * @return bool |
| 1443 | */ |
Andrey Andreev | 3b2c508 | 2012-03-07 22:49:24 +0200 | [diff] [blame] | 1444 | public function less_than_equal_to($str, $max) |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1445 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1446 | return is_numeric($str) ? ($str <= $max) : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | // -------------------------------------------------------------------- |
| 1450 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1451 | /** |
Lance Vincent | eca885d | 2015-01-28 17:43:23 +0800 | [diff] [blame] | 1452 | * Value should be within an array of values |
| 1453 | * |
| 1454 | * @param string |
| 1455 | * @param array |
| 1456 | * @return bool |
| 1457 | */ |
| 1458 | public function in($value, $list) |
| 1459 | { |
Lance Vincent | 0b0117c | 2015-01-28 20:31:42 +0800 | [diff] [blame^] | 1460 | return in_array($value, explode(',', $list), TRUE); |
Lance Vincent | eca885d | 2015-01-28 17:43:23 +0800 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | // -------------------------------------------------------------------- |
| 1464 | |
| 1465 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1466 | * Is a Natural number (0,1,2,3, etc.) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1467 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1468 | * @param string |
| 1469 | * @return bool |
| 1470 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1471 | public function is_natural($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1472 | { |
Andrey Andreev | acb962e | 2012-06-27 12:04:24 +0300 | [diff] [blame] | 1473 | return ctype_digit((string) $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | // -------------------------------------------------------------------- |
| 1477 | |
| 1478 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1479 | * Is a Natural number, but not a zero (1,2,3, etc.) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1480 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1481 | * @param string |
| 1482 | * @return bool |
| 1483 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1484 | public function is_natural_no_zero($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1485 | { |
Andrey Andreev | acb962e | 2012-06-27 12:04:24 +0300 | [diff] [blame] | 1486 | return ($str != 0 && ctype_digit((string) $str)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1487 | } |
| 1488 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1489 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1490 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1491 | /** |
| 1492 | * Valid Base64 |
| 1493 | * |
| 1494 | * Tests a string for characters outside of the Base64 alphabet |
| 1495 | * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045 |
| 1496 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1497 | * @param string |
| 1498 | * @return bool |
| 1499 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1500 | public function valid_base64($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1501 | { |
Andrey Andreev | cd9797a | 2013-06-28 14:03:48 +0300 | [diff] [blame] | 1502 | return (base64_encode(base64_decode($str)) === $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1503 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1504 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1505 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1506 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1507 | /** |
| 1508 | * Prep data for form |
| 1509 | * |
| 1510 | * This function allows HTML to be safely shown in a form. |
| 1511 | * Special characters are converted. |
| 1512 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1513 | * @param string |
| 1514 | * @return string |
| 1515 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1516 | public function prep_for_form($data = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1517 | { |
Andrey Andreev | 7c4d106 | 2012-11-01 15:14:34 +0200 | [diff] [blame] | 1518 | if ($this->_safe_form_data === FALSE OR empty($data)) |
| 1519 | { |
| 1520 | return $data; |
| 1521 | } |
| 1522 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1523 | if (is_array($data)) |
| 1524 | { |
| 1525 | foreach ($data as $key => $val) |
| 1526 | { |
| 1527 | $data[$key] = $this->prep_for_form($val); |
| 1528 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1529 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1530 | return $data; |
| 1531 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1532 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1533 | return str_replace(array("'", '"', '<', '>'), array(''', '"', '<', '>'), stripslashes($data)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1534 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1535 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1536 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1537 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1538 | /** |
| 1539 | * Prep URL |
| 1540 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1541 | * @param string |
| 1542 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1543 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1544 | public function prep_url($str = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1545 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1546 | if ($str === 'http://' OR $str === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1547 | { |
| 1548 | return ''; |
| 1549 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1550 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1551 | if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1552 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1553 | return 'http://'.$str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1554 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1555 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1556 | return $str; |
| 1557 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1558 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1559 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1560 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1561 | /** |
| 1562 | * Strip Image Tags |
| 1563 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1564 | * @param string |
| 1565 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1566 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1567 | public function strip_image_tags($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1568 | { |
Andrey Andreev | 1a24a9d | 2012-06-27 00:52:47 +0300 | [diff] [blame] | 1569 | return $this->CI->security->strip_image_tags($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1570 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1571 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1572 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1573 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1574 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1575 | * Convert PHP tags to entities |
| 1576 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1577 | * @param string |
| 1578 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1579 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1580 | public function encode_php_tags($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1581 | { |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 1582 | return str_replace(array('<?', '?>'), array('<?', '?>'), $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 1585 | // -------------------------------------------------------------------- |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1586 | |
| 1587 | /** |
| 1588 | * Reset validation vars |
| 1589 | * |
| 1590 | * Prevents subsequent validation routines from being affected by the |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 1591 | * results of any previous validation routine due to the CI singleton. |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1592 | * |
Andrey Andreev | a89c1da | 2014-02-08 19:03:35 +0200 | [diff] [blame] | 1593 | * @return CI_Form_validation |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1594 | */ |
JonoB | 883f80f | 2012-03-05 09:51:27 +0000 | [diff] [blame] | 1595 | public function reset_validation() |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1596 | { |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 1597 | $this->_field_data = array(); |
| 1598 | $this->_config_rules = array(); |
| 1599 | $this->_error_array = array(); |
| 1600 | $this->_error_messages = array(); |
| 1601 | $this->error_string = ''; |
Andrey Andreev | a89c1da | 2014-02-08 19:03:35 +0200 | [diff] [blame] | 1602 | return $this; |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1603 | } |
| 1604 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1605 | } |