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