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 | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2016, 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 |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2016, 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 |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 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 |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/form_validation.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
rich | cc93635 | 2015-09-09 15:52:26 -0400 | [diff] [blame] | 214 | $rules = preg_split('/\|(?![^\[]*\])/', $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 | { |
Andrey Andreev | ce8fa5a | 2015-12-14 12:57:09 +0200 | [diff] [blame] | 418 | $validation_array = empty($this->validation_data) |
| 419 | ? $_POST |
| 420 | : $this->validation_data; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 421 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | // Does the _field_data array containing the validation rules exist? |
| 423 | // 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] | 424 | if (count($this->_field_data) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 426 | // No validation rules? We're done... |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 427 | if (count($this->_config_rules) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | { |
| 429 | return FALSE; |
| 430 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | |
Andrey Andreev | 3b2803e | 2014-01-07 14:46:38 +0200 | [diff] [blame] | 432 | if (empty($group)) |
| 433 | { |
| 434 | // Is there a validation rule for the particular URI being accessed? |
| 435 | $group = trim($this->CI->uri->ruri_string(), '/'); |
| 436 | isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method; |
| 437 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | |
Andrey Andreev | 3b2803e | 2014-01-07 14:46:38 +0200 | [diff] [blame] | 439 | $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] | 440 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 441 | // Were we able to set the rules correctly? |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 442 | if (count($this->_field_data) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 444 | log_message('debug', 'Unable to find validation rules'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | return FALSE; |
| 446 | } |
| 447 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 448 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | // Load the language file containing error messages |
| 450 | $this->CI->lang->load('form_validation'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 451 | |
Andrey Andreev | 751f247 | 2012-11-03 18:26:27 +0200 | [diff] [blame] | 452 | // Cycle through the rules for each field and match the corresponding $validation_data item |
Andrey Andreev | 55a40b7 | 2016-03-12 15:43:22 +0200 | [diff] [blame] | 453 | foreach ($this->_field_data as $field => &$row) |
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 | // 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] | 456 | // 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] | 457 | if ($row['is_array'] === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | { |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 459 | $this->_field_data[$field]['postdata'] = $this->_reduce_array($validation_array, $row['keys']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | } |
Andrey Andreev | b137d23 | 2015-04-29 11:44:38 +0300 | [diff] [blame] | 461 | elseif (isset($validation_array[$field])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 463 | $this->_field_data[$field]['postdata'] = $validation_array[$field]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | } |
Andrey Andreev | 751f247 | 2012-11-03 18:26:27 +0200 | [diff] [blame] | 465 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 466 | |
Andrey Andreev | 751f247 | 2012-11-03 18:26:27 +0200 | [diff] [blame] | 467 | // Execute validation rules |
| 468 | // Note: A second foreach (for now) is required in order to avoid false-positives |
| 469 | // for rules like 'matches', which correlate to other validation fields. |
Andrey Andreev | 55a40b7 | 2016-03-12 15:43:22 +0200 | [diff] [blame] | 470 | foreach ($this->_field_data as $field => &$row) |
Andrey Andreev | 751f247 | 2012-11-03 18:26:27 +0200 | [diff] [blame] | 471 | { |
Andrey Andreev | 3d9cec9 | 2012-07-08 21:50:19 +0300 | [diff] [blame] | 472 | // Don't try to validate if we have no rules set |
| 473 | if (empty($row['rules'])) |
| 474 | { |
| 475 | continue; |
| 476 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 477 | |
Andrey Andreev | 55a40b7 | 2016-03-12 15:43:22 +0200 | [diff] [blame] | 478 | $this->_execute($row, $row['rules'], $row['postdata']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | // Did we end up with any errors? |
| 482 | $total_errors = count($this->_error_array); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | if ($total_errors > 0) |
| 484 | { |
| 485 | $this->_safe_form_data = TRUE; |
| 486 | } |
| 487 | |
| 488 | // Now we need to re-set the POST data with the new, processed data |
Andrey Andreev | 02ac187 | 2016-03-16 12:19:34 +0200 | [diff] [blame] | 489 | empty($this->validation_data) && $this->_reset_post_array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 490 | |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 491 | return ($total_errors === 0); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 495 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | /** |
Andrey Andreev | 0fae625 | 2016-05-17 13:46:55 +0300 | [diff] [blame^] | 497 | * Prepare rules |
| 498 | * |
| 499 | * Re-orders the provided rules in order of importance, so that |
| 500 | * they can easily be executed later without weird checks ... |
| 501 | * |
| 502 | * "Callbacks" are given the highest priority (always called), |
| 503 | * followed by 'required' (called if callbacks didn't fail), |
| 504 | * and then every next rule depends on the previous one passing. |
| 505 | * |
| 506 | * @param array $rules |
| 507 | * @return array |
| 508 | */ |
| 509 | protected function _prepare_rules($rules) |
| 510 | { |
| 511 | $new_rules = array(); |
| 512 | $callbacks = array(); |
| 513 | |
| 514 | foreach ($rules as &$rule) |
| 515 | { |
| 516 | // Let 'required' always be the first (non-callback) rule |
| 517 | if ($rule === 'required') |
| 518 | { |
| 519 | array_unshift($new_rules, 'required'); |
| 520 | } |
| 521 | // 'isset' is a kind of a weird alias for 'required' ... |
| 522 | elseif ($rule === 'isset' && (empty($new_rules) OR $new_rules[0] !== 'required')) |
| 523 | { |
| 524 | array_unshift($new_rules, 'isset'); |
| 525 | } |
| 526 | // The old/classic 'callback_'-prefixed rules |
| 527 | elseif (is_string($rule) && strncmp('callback_', $rule, 9) === 0) |
| 528 | { |
| 529 | $callbacks[] = $rule; |
| 530 | } |
| 531 | // Proper callables |
| 532 | elseif (is_callable($rule)) |
| 533 | { |
| 534 | $callbacks[] = $rule; |
| 535 | } |
| 536 | // "Named" callables; i.e. array('name' => $callable) |
| 537 | elseif (is_array($rule) && isset($rule[0], $rule[1]) && is_callable($rule[1])) |
| 538 | { |
| 539 | $callbacks[] = $rule; |
| 540 | } |
| 541 | // Everything else goes at the end of the queue |
| 542 | else |
| 543 | { |
| 544 | $new_rules[] = $rule; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | return array_merge($callbacks, $new_rules); |
| 549 | } |
| 550 | |
| 551 | // -------------------------------------------------------------------- |
| 552 | |
| 553 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 554 | * Traverse a multidimensional $_POST array index until the data is found |
| 555 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 556 | * @param array |
| 557 | * @param array |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 558 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 559 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 560 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 561 | protected function _reduce_array($array, $keys, $i = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 563 | if (is_array($array) && isset($keys[$i])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 564 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 565 | 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] | 566 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 567 | |
Andrey Andreev | 2d48b4f | 2012-11-23 17:33:21 +0200 | [diff] [blame] | 568 | // NULL must be returned for empty fields |
Andrey Andreev | 44c3463 | 2012-11-23 18:46:34 +0200 | [diff] [blame] | 569 | return ($array === '') ? NULL : $array; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 573 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 574 | /** |
| 575 | * Re-populate the _POST array with our finalized and processed data |
| 576 | * |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 577 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 578 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 579 | protected function _reset_post_array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 580 | { |
| 581 | foreach ($this->_field_data as $field => $row) |
| 582 | { |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 583 | if ($row['postdata'] !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 585 | if ($row['is_array'] === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 586 | { |
Andrey Andreev | 5576d2c | 2016-03-16 12:16:45 +0200 | [diff] [blame] | 587 | isset($_POST[$field]) && $_POST[$field] = $row['postdata']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | } |
| 589 | else |
| 590 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 591 | // start with a reference |
| 592 | $post_ref =& $_POST; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 593 | |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 594 | // before we assign values, make a reference to the right POST key |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 595 | if (count($row['keys']) === 1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 596 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 597 | $post_ref =& $post_ref[current($row['keys'])]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 598 | } |
| 599 | else |
| 600 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 601 | foreach ($row['keys'] as $val) |
| 602 | { |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 603 | $post_ref =& $post_ref[$val]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
Derek Jones | 63eeae3 | 2009-02-10 19:08:56 +0000 | [diff] [blame] | 606 | |
Andrey Andreev | 5576d2c | 2016-03-16 12:16:45 +0200 | [diff] [blame] | 607 | $post_ref = $row['postdata']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 614 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 615 | /** |
| 616 | * Executes the Validation routines |
| 617 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 618 | * @param array |
| 619 | * @param array |
| 620 | * @param mixed |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 621 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 622 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 623 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 624 | protected function _execute($row, $rules, $postdata = NULL, $cycles = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 625 | { |
| 626 | // If the $_POST data is an array we will run a recursive call |
Andrey Andreev | 7243d0b | 2016-03-12 11:40:34 +0200 | [diff] [blame] | 627 | // |
| 628 | // Note: We MUST check if the array is empty or not! |
| 629 | // Otherwise empty arrays will always pass validation. |
| 630 | if (is_array($postdata) && ! empty($postdata)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 631 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | foreach ($postdata as $key => $val) |
| 633 | { |
Andrey Andreev | 8d3099d | 2012-06-21 16:00:20 +0300 | [diff] [blame] | 634 | $this->_execute($row, $rules, $val, $key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 635 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 636 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 637 | return; |
| 638 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 639 | |
Andrey Andreev | 0fae625 | 2016-05-17 13:46:55 +0300 | [diff] [blame^] | 640 | $rules = $this->_prepare_rules($rules); |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 641 | foreach ($rules as $rule) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 642 | { |
| 643 | $_in_array = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 644 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 645 | // We set the $postdata variable with the current data in our master array so that |
| 646 | // 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] | 647 | 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] | 648 | { |
| 649 | // We shouldn't need this safety, but just in case there isn't an array index |
| 650 | // associated with this cycle we'll bail out |
| 651 | if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles])) |
| 652 | { |
| 653 | continue; |
| 654 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 655 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 656 | $postdata = $this->_field_data[$row['field']]['postdata'][$cycles]; |
| 657 | $_in_array = TRUE; |
| 658 | } |
| 659 | else |
| 660 | { |
Andrey Andreev | 6ac5144 | 2012-06-18 13:05:17 +0300 | [diff] [blame] | 661 | // If we get an array field, but it's not expected - then it is most likely |
| 662 | // somebody messing with the form on the client side, so we'll just consider |
| 663 | // it an empty field |
| 664 | $postdata = is_array($this->_field_data[$row['field']]['postdata']) |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 665 | ? NULL |
| 666 | : $this->_field_data[$row['field']]['postdata']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 669 | // Is the rule a callback? |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 670 | $callback = $callable = FALSE; |
| 671 | if (is_string($rule)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 672 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 673 | if (strpos($rule, 'callback_') === 0) |
| 674 | { |
| 675 | $rule = substr($rule, 9); |
| 676 | $callback = TRUE; |
| 677 | } |
| 678 | } |
| 679 | elseif (is_callable($rule)) |
| 680 | { |
| 681 | $callable = TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 682 | } |
Andrey Andreev | 60726ef | 2014-09-08 11:31:48 +0300 | [diff] [blame] | 683 | elseif (is_array($rule) && isset($rule[0], $rule[1]) && is_callable($rule[1])) |
| 684 | { |
| 685 | // We have a "named" callable, so save the name |
| 686 | $callable = $rule[0]; |
| 687 | $rule = $rule[1]; |
| 688 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 689 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 690 | // Strip the parameter (if exists) from the rule |
| 691 | // Rules can contain a parameter: max_length[5] |
| 692 | $param = FALSE; |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 693 | if ( ! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 694 | { |
Andrey Andreev | ef758bd | 2012-11-15 12:24:52 +0200 | [diff] [blame] | 695 | $rule = $match[1]; |
| 696 | $param = $match[2]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 697 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 698 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 699 | // Call the function that corresponds to the rule |
Andrey Andreev | 60726ef | 2014-09-08 11:31:48 +0300 | [diff] [blame] | 700 | if ($callback OR $callable !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 701 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 702 | if ($callback) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 703 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 704 | if ( ! method_exists($this->CI, $rule)) |
| 705 | { |
| 706 | log_message('debug', 'Unable to find callback validation rule: '.$rule); |
| 707 | $result = FALSE; |
| 708 | } |
| 709 | else |
| 710 | { |
| 711 | // Run the function and grab the result |
| 712 | $result = $this->CI->$rule($postdata, $param); |
| 713 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 714 | } |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 715 | else |
| 716 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 717 | $result = is_array($rule) |
Andrey Andreev | 60726ef | 2014-09-08 11:31:48 +0300 | [diff] [blame] | 718 | ? $rule[0]->{$rule[1]}($postdata) |
| 719 | : $rule($postdata); |
| 720 | |
| 721 | // Is $callable set to a rule name? |
| 722 | if ($callable !== FALSE) |
| 723 | { |
| 724 | $rule = $callable; |
| 725 | } |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 726 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 727 | |
| 728 | // Re-assign the result to the master data array |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 729 | if ($_in_array === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 730 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 731 | $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 732 | } |
| 733 | else |
| 734 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 735 | $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 736 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 737 | } |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 738 | elseif ( ! method_exists($this, $rule)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 739 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 740 | // If our own wrapper function doesn't exist we see if a native PHP function does. |
| 741 | // Users can use any native PHP function call that has one param. |
| 742 | if (function_exists($rule)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 743 | { |
Andrey Andreev | 4b90a37 | 2014-03-10 10:24:24 +0200 | [diff] [blame] | 744 | // 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] | 745 | $result = ($param !== FALSE) ? $rule($postdata, $param) : $rule($postdata); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 746 | |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 747 | if ($_in_array === TRUE) |
| 748 | { |
| 749 | $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 750 | } |
patwork | 02404a1 | 2011-04-08 15:45:46 +0200 | [diff] [blame] | 751 | else |
| 752 | { |
Andrey Andreev | cec2ba5 | 2012-04-03 20:26:38 +0300 | [diff] [blame] | 753 | $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; |
patwork | 02404a1 | 2011-04-08 15:45:46 +0200 | [diff] [blame] | 754 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 755 | } |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 756 | else |
| 757 | { |
Andrey Andreev | cec2ba5 | 2012-04-03 20:26:38 +0300 | [diff] [blame] | 758 | log_message('debug', 'Unable to find validation rule: '.$rule); |
| 759 | $result = FALSE; |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 760 | } |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 761 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 762 | else |
| 763 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 764 | $result = $this->$rule($postdata, $param); |
| 765 | |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 766 | if ($_in_array === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 767 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 768 | $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 769 | } |
| 770 | else |
| 771 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 772 | $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 775 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 776 | // Did the rule test negatively? If so, grab the error. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 777 | if ($result === FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 778 | { |
Andrey Andreev | 60726ef | 2014-09-08 11:31:48 +0300 | [diff] [blame] | 779 | // Callable rules might not have named error messages |
Andrey Andreev | 57f1005 | 2014-06-07 12:22:37 +0300 | [diff] [blame] | 780 | if ( ! is_string($rule)) |
Ahmedul Haque Abid | 42b4000 | 2014-01-09 01:10:25 +0600 | [diff] [blame] | 781 | { |
Andrey Andreev | 38b5eb0 | 2015-06-10 17:38:26 +0300 | [diff] [blame] | 782 | $line = $this->CI->lang->line('form_validation_error_message_not_set').'(Anonymous function)'; |
Andrey Andreev | 57f1005 | 2014-06-07 12:22:37 +0300 | [diff] [blame] | 783 | } |
Andrey Andreev | 57f1005 | 2014-06-07 12:22:37 +0300 | [diff] [blame] | 784 | else |
| 785 | { |
Andrey Andreev | 98c14ae | 2016-02-09 23:43:55 +0200 | [diff] [blame] | 786 | $line = $this->_get_error_message($rule, $row['field']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 787 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 788 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 789 | // Is the parameter we are inserting into the error message the name |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 790 | // of another field? If so we need to grab its "field label" |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 791 | if (isset($this->_field_data[$param], $this->_field_data[$param]['label'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 792 | { |
Pascal Kriete | c189583 | 2009-10-13 12:56:43 +0000 | [diff] [blame] | 793 | $param = $this->_translate_fieldname($this->_field_data[$param]['label']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 794 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 795 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 796 | // Build the error message |
Eric Roberts | 41cc090 | 2012-01-24 00:59:44 -0600 | [diff] [blame] | 797 | $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 798 | |
| 799 | // Save the error message |
| 800 | $this->_field_data[$row['field']]['error'] = $message; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 801 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 802 | if ( ! isset($this->_error_array[$row['field']])) |
| 803 | { |
| 804 | $this->_error_array[$row['field']] = $message; |
| 805 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 806 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 807 | return; |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 813 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 814 | /** |
Andrey Andreev | 98c14ae | 2016-02-09 23:43:55 +0200 | [diff] [blame] | 815 | * Get the error message for the rule |
| 816 | * |
| 817 | * @param string $rule The rule name |
| 818 | * @param string $field The field name |
| 819 | * @return string |
| 820 | */ |
| 821 | protected function _get_error_message($rule, $field) |
| 822 | { |
| 823 | // check if a custom message is defined through validation config row. |
| 824 | if (isset($this->_field_data[$field]['errors'][$rule])) |
| 825 | { |
| 826 | return $this->_field_data[$field]['errors'][$rule]; |
| 827 | } |
| 828 | // check if a custom message has been set using the set_message() function |
| 829 | elseif (isset($this->_error_messages[$rule])) |
| 830 | { |
| 831 | return $this->_error_messages[$rule]; |
| 832 | } |
Andrey Andreev | b30a64a | 2016-02-09 23:46:25 +0200 | [diff] [blame] | 833 | elseif (FALSE !== ($line = $this->CI->lang->line('form_validation_'.$rule))) |
Andrey Andreev | 98c14ae | 2016-02-09 23:43:55 +0200 | [diff] [blame] | 834 | { |
Andrey Andreev | b30a64a | 2016-02-09 23:46:25 +0200 | [diff] [blame] | 835 | return $line; |
Andrey Andreev | 98c14ae | 2016-02-09 23:43:55 +0200 | [diff] [blame] | 836 | } |
| 837 | // DEPRECATED support for non-prefixed keys, lang file again |
Andrey Andreev | b30a64a | 2016-02-09 23:46:25 +0200 | [diff] [blame] | 838 | elseif (FALSE !== ($line = $this->CI->lang->line($rule, FALSE))) |
Andrey Andreev | 98c14ae | 2016-02-09 23:43:55 +0200 | [diff] [blame] | 839 | { |
Andrey Andreev | b30a64a | 2016-02-09 23:46:25 +0200 | [diff] [blame] | 840 | return $line; |
Andrey Andreev | 98c14ae | 2016-02-09 23:43:55 +0200 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | return $this->CI->lang->line('form_validation_error_message_not_set').'('.$rule.')'; |
| 844 | } |
| 845 | |
| 846 | // -------------------------------------------------------------------- |
| 847 | |
| 848 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 849 | * Translate a field name |
| 850 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 851 | * @param string the field name |
| 852 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 853 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 854 | protected function _translate_fieldname($fieldname) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 855 | { |
Andrey Andreev | 5d8e2a6 | 2015-04-14 16:56:28 +0300 | [diff] [blame] | 856 | // Do we need to translate the field name? We look for the prefix 'lang:' to determine this |
| 857 | // If we find one, but there's no translation for the string - just return it |
| 858 | if (sscanf($fieldname, 'lang:%s', $line) === 1 && FALSE === ($fieldname = $this->CI->lang->line($line, FALSE))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 859 | { |
Andrey Andreev | 5d8e2a6 | 2015-04-14 16:56:28 +0300 | [diff] [blame] | 860 | return $line; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | return $fieldname; |
| 864 | } |
| 865 | |
| 866 | // -------------------------------------------------------------------- |
Andrey Andreev | d4eec9f | 2012-12-14 11:07:13 +0200 | [diff] [blame] | 867 | |
Eric Roberts | 41cc090 | 2012-01-24 00:59:44 -0600 | [diff] [blame] | 868 | /** |
| 869 | * Build an error message using the field and param. |
| 870 | * |
| 871 | * @param string The error message line |
| 872 | * @param string A field's human name |
| 873 | * @param mixed A rule's optional parameter |
| 874 | * @return string |
| 875 | */ |
| 876 | protected function _build_error_msg($line, $field = '', $param = '') |
| 877 | { |
| 878 | // Check for %s in the string for legacy support. |
Eric Roberts | 24a13f5 | 2012-12-12 07:09:42 -0600 | [diff] [blame] | 879 | if (strpos($line, '%s') !== FALSE) |
Eric Roberts | 41cc090 | 2012-01-24 00:59:44 -0600 | [diff] [blame] | 880 | { |
| 881 | return sprintf($line, $field, $param); |
| 882 | } |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 883 | |
Eric Roberts | 41cc090 | 2012-01-24 00:59:44 -0600 | [diff] [blame] | 884 | return str_replace(array('{field}', '{param}'), array($field, $param), $line); |
| 885 | } |
| 886 | |
| 887 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 888 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 889 | /** |
nisheeth-barthwal | a7447d2 | 2013-03-21 15:48:10 +0530 | [diff] [blame] | 890 | * Checks if the rule is present within the validator |
| 891 | * |
| 892 | * Permits you to check if a rule is present within the validator |
| 893 | * |
| 894 | * @param string the field name |
| 895 | * @return bool |
| 896 | */ |
| 897 | public function has_rule($field) |
| 898 | { |
| 899 | return isset($this->_field_data[$field]); |
| 900 | } |
| 901 | |
| 902 | // -------------------------------------------------------------------- |
| 903 | |
| 904 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 905 | * Get the value from a form |
| 906 | * |
| 907 | * Permits you to repopulate a form field with the value it was submitted |
| 908 | * with, or, if that value doesn't exist, with the default |
| 909 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 910 | * @param string the field name |
| 911 | * @param string |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 912 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 913 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 914 | public function set_value($field = '', $default = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 915 | { |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 916 | if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 917 | { |
| 918 | return $default; |
| 919 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 920 | |
Phil Sturgeon | 5c56180 | 2011-01-05 16:31:59 +0000 | [diff] [blame] | 921 | // If the data is an array output them one at a time. |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 922 | // E.g: form_input('name[]', set_value('name[]'); |
Phil Sturgeon | 5c56180 | 2011-01-05 16:31:59 +0000 | [diff] [blame] | 923 | if (is_array($this->_field_data[$field]['postdata'])) |
| 924 | { |
| 925 | return array_shift($this->_field_data[$field]['postdata']); |
| 926 | } |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 927 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 928 | return $this->_field_data[$field]['postdata']; |
| 929 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 930 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 931 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 932 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 933 | /** |
| 934 | * Set Select |
| 935 | * |
| 936 | * Enables pull-down lists to be set to the value the user |
| 937 | * selected in the event of an error |
| 938 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 939 | * @param string |
| 940 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 941 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 942 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 943 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 944 | public function set_select($field = '', $value = '', $default = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 945 | { |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 946 | if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 947 | { |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 948 | return ($default === TRUE && count($this->_field_data) === 0) ? ' selected="selected"' : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 949 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 950 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 951 | $field = $this->_field_data[$field]['postdata']; |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 952 | $value = (string) $value; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 953 | if (is_array($field)) |
| 954 | { |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 955 | // Note: in_array('', array(0)) returns TRUE, do not use it |
| 956 | foreach ($field as &$v) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 957 | { |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 958 | if ($value === $v) |
| 959 | { |
| 960 | return ' selected="selected"'; |
| 961 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 962 | } |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 963 | |
| 964 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 965 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 966 | elseif (($field === '' OR $value === '') OR ($field !== $value)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 967 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 968 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 969 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 970 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 971 | return ' selected="selected"'; |
| 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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 975 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 976 | /** |
| 977 | * Set Radio |
| 978 | * |
| 979 | * Enables radio buttons to be set to the value the user |
| 980 | * selected in the event of an error |
| 981 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 982 | * @param string |
| 983 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 984 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 985 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 986 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 987 | public function set_radio($field = '', $value = '', $default = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 988 | { |
Andrey Andreev | 46ac881 | 2012-02-28 14:32:54 +0200 | [diff] [blame] | 989 | if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 990 | { |
Andrey Andreev | 6de924c | 2012-01-20 13:18:18 +0200 | [diff] [blame] | 991 | return ($default === TRUE && count($this->_field_data) === 0) ? ' checked="checked"' : ''; |
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 | $field = $this->_field_data[$field]['postdata']; |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 995 | $value = (string) $value; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 996 | if (is_array($field)) |
| 997 | { |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 998 | // Note: in_array('', array(0)) returns TRUE, do not use it |
| 999 | foreach ($field as &$v) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1000 | { |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 1001 | if ($value === $v) |
| 1002 | { |
| 1003 | return ' checked="checked"'; |
| 1004 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1005 | } |
Andrey Andreev | a587a93 | 2013-10-23 19:57:46 +0300 | [diff] [blame] | 1006 | |
| 1007 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1008 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1009 | elseif (($field === '' OR $value === '') OR ($field !== $value)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1010 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1011 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1012 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1013 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1014 | return ' checked="checked"'; |
| 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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1018 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1019 | /** |
| 1020 | * Set Checkbox |
| 1021 | * |
| 1022 | * Enables checkboxes to be set to the value the user |
| 1023 | * selected in the event of an error |
| 1024 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1025 | * @param string |
| 1026 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1027 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1028 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1029 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1030 | public function set_checkbox($field = '', $value = '', $default = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1031 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1032 | // Logic is exactly the same as for radio fields |
| 1033 | return $this->set_radio($field, $value, $default); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1034 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1035 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1036 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1037 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1038 | /** |
| 1039 | * Required |
| 1040 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1041 | * @param string |
| 1042 | * @return bool |
| 1043 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1044 | public function required($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1045 | { |
Andrey Andreev | 0fae625 | 2016-05-17 13:46:55 +0300 | [diff] [blame^] | 1046 | return is_array($str) |
| 1047 | ? (empty($str) === FALSE) |
| 1048 | : (trim($str) !== ''); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1049 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1050 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1051 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1052 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1053 | /** |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 1054 | * Performs a Regular Expression match test. |
| 1055 | * |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 1056 | * @param string |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1057 | * @param string regex |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 1058 | * @return bool |
| 1059 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1060 | public function regex_match($str, $regex) |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 1061 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1062 | return (bool) preg_match($regex, $str); |
Dan Horrigan | 2280e8e | 2010-12-15 10:16:38 -0500 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | // -------------------------------------------------------------------- |
| 1066 | |
| 1067 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1068 | * Match one field to another |
| 1069 | * |
Andrey Andreev | a779b2c | 2012-10-26 16:25:47 +0300 | [diff] [blame] | 1070 | * @param string $str string to compare against |
| 1071 | * @param string $field |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1072 | * @return bool |
| 1073 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1074 | public function matches($str, $field) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1075 | { |
Andrey Andreev | a779b2c | 2012-10-26 16:25:47 +0300 | [diff] [blame] | 1076 | return isset($this->_field_data[$field], $this->_field_data[$field]['postdata']) |
| 1077 | ? ($str === $this->_field_data[$field]['postdata']) |
| 1078 | : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1079 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1080 | |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1081 | // -------------------------------------------------------------------- |
| 1082 | |
| 1083 | /** |
Raul Baldner Junior | f38564d | 2012-10-11 11:32:23 -0300 | [diff] [blame] | 1084 | * Differs from another field |
| 1085 | * |
| 1086 | * @param string |
| 1087 | * @param string field |
| 1088 | * @return bool |
| 1089 | */ |
| 1090 | public function differs($str, $field) |
| 1091 | { |
| 1092 | return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1093 | } |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1094 | |
| 1095 | // -------------------------------------------------------------------- |
| 1096 | |
| 1097 | /** |
Andrey Andreev | d09d650 | 2012-01-03 06:38:33 +0200 | [diff] [blame] | 1098 | * Is Unique |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1099 | * |
Andrey Andreev | d09d650 | 2012-01-03 06:38:33 +0200 | [diff] [blame] | 1100 | * Check if the input value doesn't already exist |
| 1101 | * in the specified database field. |
| 1102 | * |
Andrey Andreev | 9a0e0c7 | 2014-04-09 15:10:27 +0300 | [diff] [blame] | 1103 | * @param string $str |
| 1104 | * @param string $field |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1105 | * @return bool |
| 1106 | */ |
| 1107 | public function is_unique($str, $field) |
| 1108 | { |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 1109 | sscanf($field, '%[^.].%[^.]', $table, $field); |
Andrey Andreev | 9a0e0c7 | 2014-04-09 15:10:27 +0300 | [diff] [blame] | 1110 | return isset($this->CI->db) |
| 1111 | ? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0) |
| 1112 | : FALSE; |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 1113 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1114 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1115 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1116 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1117 | /** |
| 1118 | * Minimum Length |
| 1119 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1120 | * @param string |
Michiel Vugteveen | a8221ad | 2012-06-14 23:26:34 +0200 | [diff] [blame] | 1121 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1122 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1123 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1124 | public function min_length($str, $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1125 | { |
Michiel Vugteveen | ceaf887 | 2012-06-15 11:56:24 +0200 | [diff] [blame] | 1126 | if ( ! is_numeric($val)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1127 | { |
| 1128 | return FALSE; |
| 1129 | } |
| 1130 | |
bjjay | 8d6c8fe | 2015-03-09 13:46:06 +0800 | [diff] [blame] | 1131 | return ($val <= mb_strlen($str)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1132 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1133 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [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 | /** |
| 1137 | * Max Length |
| 1138 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1139 | * @param string |
Michiel Vugteveen | a8221ad | 2012-06-14 23:26:34 +0200 | [diff] [blame] | 1140 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1141 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1142 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1143 | public function max_length($str, $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1144 | { |
Michiel Vugteveen | ceaf887 | 2012-06-15 11:56:24 +0200 | [diff] [blame] | 1145 | if ( ! is_numeric($val)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1146 | { |
| 1147 | return FALSE; |
| 1148 | } |
| 1149 | |
bjjay | 8d6c8fe | 2015-03-09 13:46:06 +0800 | [diff] [blame] | 1150 | return ($val >= mb_strlen($str)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1151 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1152 | |
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 | /** |
| 1156 | * Exact Length |
| 1157 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1158 | * @param string |
Michiel Vugteveen | eccde13 | 2012-06-14 23:22:26 +0200 | [diff] [blame] | 1159 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1160 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1161 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1162 | public function exact_length($str, $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1163 | { |
Michiel Vugteveen | ceaf887 | 2012-06-15 11:56:24 +0200 | [diff] [blame] | 1164 | if ( ! is_numeric($val)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1165 | { |
| 1166 | return FALSE; |
| 1167 | } |
| 1168 | |
bjjay | 8d6c8fe | 2015-03-09 13:46:06 +0800 | [diff] [blame] | 1169 | return (mb_strlen($str) === (int) $val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1170 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1171 | |
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 | /** |
Andrey Andreev | daaca88 | 2012-11-26 22:16:12 +0200 | [diff] [blame] | 1175 | * Valid URL |
| 1176 | * |
| 1177 | * @param string $str |
| 1178 | * @return bool |
| 1179 | */ |
| 1180 | public function valid_url($str) |
| 1181 | { |
| 1182 | if (empty($str)) |
| 1183 | { |
| 1184 | return FALSE; |
| 1185 | } |
| 1186 | elseif (preg_match('/^(?:([^:]*)\:)?\/\/(.+)$/', $str, $matches)) |
| 1187 | { |
| 1188 | if (empty($matches[2])) |
| 1189 | { |
| 1190 | return FALSE; |
| 1191 | } |
| 1192 | elseif ( ! in_array($matches[1], array('http', 'https'), TRUE)) |
| 1193 | { |
| 1194 | return FALSE; |
| 1195 | } |
| 1196 | |
| 1197 | $str = $matches[2]; |
| 1198 | } |
| 1199 | |
Andrey Andreev | 391d339 | 2016-01-30 22:43:41 +0200 | [diff] [blame] | 1200 | // PHP 7 accepts IPv6 addresses within square brackets as hostnames, |
| 1201 | // but it appears that the PR that came in with https://bugs.php.net/bug.php?id=68039 |
| 1202 | // was never merged into a PHP 5 branch ... https://3v4l.org/8PsSN |
| 1203 | if (preg_match('/^\[([^\]]+)\]/', $str, $matches) && ! is_php('7') && filter_var($matches[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== FALSE) |
| 1204 | { |
| 1205 | $str = 'ipv6.host'.substr($str, strlen($matches[1]) + 2); |
| 1206 | } |
| 1207 | |
Andrey Andreev | daaca88 | 2012-11-26 22:16:12 +0200 | [diff] [blame] | 1208 | $str = 'http://'.$str; |
| 1209 | |
| 1210 | // There's a bug affecting PHP 5.2.13, 5.3.2 that considers the |
| 1211 | // underscore to be a valid hostname character instead of a dash. |
| 1212 | // Reference: https://bugs.php.net/bug.php?id=51192 |
Andrey Andreev | 9a0e0c7 | 2014-04-09 15:10:27 +0300 | [diff] [blame] | 1213 | 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] | 1214 | { |
| 1215 | sscanf($str, 'http://%[^/]', $host); |
| 1216 | $str = substr_replace($str, strtr($host, array('_' => '-', '-' => '_')), 7, strlen($host)); |
| 1217 | } |
| 1218 | |
| 1219 | return (filter_var($str, FILTER_VALIDATE_URL) !== FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | // -------------------------------------------------------------------- |
| 1223 | |
| 1224 | /** |
| 1225 | * Valid Email |
| 1226 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1227 | * @param string |
| 1228 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1229 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1230 | public function valid_email($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1231 | { |
Andrey Andreev | f9201ae | 2014-06-01 12:57:30 +0300 | [diff] [blame] | 1232 | if (function_exists('idn_to_ascii') && $atpos = strpos($str, '@')) |
Andrey Andreev | 9549666 | 2014-06-01 00:00:13 +0300 | [diff] [blame] | 1233 | { |
Andrey Andreev | f9201ae | 2014-06-01 12:57:30 +0300 | [diff] [blame] | 1234 | $str = substr($str, 0, ++$atpos).idn_to_ascii(substr($str, $atpos)); |
Andrey Andreev | 9549666 | 2014-06-01 00:00:13 +0300 | [diff] [blame] | 1235 | } |
| 1236 | |
Andrey Andreev | 580388b | 2012-06-27 15:43:46 +0300 | [diff] [blame] | 1237 | return (bool) filter_var($str, FILTER_VALIDATE_EMAIL); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1241 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1242 | /** |
| 1243 | * Valid Emails |
| 1244 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1245 | * @param string |
| 1246 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1247 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1248 | public function valid_emails($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1249 | { |
| 1250 | if (strpos($str, ',') === FALSE) |
| 1251 | { |
| 1252 | return $this->valid_email(trim($str)); |
| 1253 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1254 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 1255 | foreach (explode(',', $str) as $email) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1256 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1257 | if (trim($email) !== '' && $this->valid_email(trim($email)) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1258 | { |
| 1259 | return FALSE; |
| 1260 | } |
| 1261 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1262 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1263 | return TRUE; |
| 1264 | } |
| 1265 | |
| 1266 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1267 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1268 | /** |
| 1269 | * Validate IP Address |
| 1270 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1271 | * @param string |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 1272 | * @param string 'ipv4' or 'ipv6' to validate a specific IP format |
Bo-Yi Wu | 013c895 | 2011-09-12 15:03:44 +0800 | [diff] [blame] | 1273 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1274 | */ |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 1275 | public function valid_ip($ip, $which = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1276 | { |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 1277 | return $this->CI->input->valid_ip($ip, $which); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1281 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1282 | /** |
| 1283 | * Alpha |
| 1284 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1285 | * @param string |
| 1286 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1287 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1288 | public function alpha($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1289 | { |
Andrey Andreev | acb962e | 2012-06-27 12:04:24 +0300 | [diff] [blame] | 1290 | return ctype_alpha($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1291 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1292 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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-numeric |
| 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_numeric($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_alnum((string) $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 | // -------------------------------------------------------------------- |
Sajan Parikh | 2d1608a | 2013-02-02 08:00:39 -0600 | [diff] [blame] | 1307 | |
| 1308 | /** |
| 1309 | * Alpha-numeric w/ spaces |
| 1310 | * |
| 1311 | * @param string |
| 1312 | * @return bool |
| 1313 | */ |
| 1314 | public function alpha_numeric_spaces($str) |
| 1315 | { |
Sajan Parikh | df3bfed | 2013-02-04 12:25:49 -0600 | [diff] [blame] | 1316 | return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str); |
Sajan Parikh | 2d1608a | 2013-02-02 08:00:39 -0600 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1320 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1321 | /** |
| 1322 | * Alpha-numeric with underscores and dashes |
| 1323 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1324 | * @param string |
| 1325 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1326 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1327 | public function alpha_dash($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1328 | { |
Andrey Andreev | 8dc532d | 2011-12-24 17:57:54 +0200 | [diff] [blame] | 1329 | return (bool) preg_match('/^[a-z0-9_-]+$/i', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1330 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1331 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | * Numeric |
| 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 numeric($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('/^[\-+]?[0-9]*\.?[0-9]+$/', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1343 | |
| 1344 | } |
| 1345 | |
| 1346 | // -------------------------------------------------------------------- |
| 1347 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1348 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1349 | * Integer |
| 1350 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1351 | * @param string |
| 1352 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1353 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1354 | public function integer($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1355 | { |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1356 | return (bool) preg_match('/^[\-+]?[0-9]+$/', $str); |
| 1357 | } |
| 1358 | |
| 1359 | // -------------------------------------------------------------------- |
| 1360 | |
| 1361 | /** |
| 1362 | * Decimal number |
| 1363 | * |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1364 | * @param string |
| 1365 | * @return bool |
| 1366 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1367 | public function decimal($str) |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1368 | { |
| 1369 | return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str); |
| 1370 | } |
| 1371 | |
| 1372 | // -------------------------------------------------------------------- |
| 1373 | |
| 1374 | /** |
Andrey Andreev | c68905a | 2012-02-02 21:41:54 +0200 | [diff] [blame] | 1375 | * Greater than |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1376 | * |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1377 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1378 | * @param int |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1379 | * @return bool |
| 1380 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1381 | public function greater_than($str, $min) |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1382 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1383 | return is_numeric($str) ? ($str > $min) : FALSE; |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | // -------------------------------------------------------------------- |
| 1387 | |
| 1388 | /** |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1389 | * Equal to or Greater than |
| 1390 | * |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1391 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1392 | * @param int |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1393 | * @return bool |
| 1394 | */ |
Andrey Andreev | 3b2c508 | 2012-03-07 22:49:24 +0200 | [diff] [blame] | 1395 | public function greater_than_equal_to($str, $min) |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1396 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1397 | return is_numeric($str) ? ($str >= $min) : FALSE; |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | // -------------------------------------------------------------------- |
| 1401 | |
| 1402 | /** |
| 1403 | * Less than |
| 1404 | * |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1405 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1406 | * @param int |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1407 | * @return bool |
| 1408 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1409 | public function less_than($str, $max) |
Phil Sturgeon | ef112c0 | 2011-02-07 13:01:47 +0000 | [diff] [blame] | 1410 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1411 | return is_numeric($str) ? ($str < $max) : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1412 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1413 | |
| 1414 | // -------------------------------------------------------------------- |
| 1415 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1416 | /** |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1417 | * Equal to or Less than |
| 1418 | * |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1419 | * @param string |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 1420 | * @param int |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1421 | * @return bool |
| 1422 | */ |
Andrey Andreev | 3b2c508 | 2012-03-07 22:49:24 +0200 | [diff] [blame] | 1423 | public function less_than_equal_to($str, $max) |
Nick Busey | 98c347d | 2012-02-02 11:07:03 -0700 | [diff] [blame] | 1424 | { |
Andrey Andreev | 78f5577 | 2012-04-03 19:59:08 +0300 | [diff] [blame] | 1425 | return is_numeric($str) ? ($str <= $max) : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | // -------------------------------------------------------------------- |
| 1429 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1430 | /** |
Lance Vincent | eca885d | 2015-01-28 17:43:23 +0800 | [diff] [blame] | 1431 | * Value should be within an array of values |
| 1432 | * |
| 1433 | * @param string |
Andrey Andreev | 5b662e4 | 2015-01-29 00:13:54 +0200 | [diff] [blame] | 1434 | * @param string |
Lance Vincent | eca885d | 2015-01-28 17:43:23 +0800 | [diff] [blame] | 1435 | * @return bool |
| 1436 | */ |
Lance Vincent | 49f483d | 2015-01-28 22:46:19 +0800 | [diff] [blame] | 1437 | public function in_list($value, $list) |
Lance Vincent | eca885d | 2015-01-28 17:43:23 +0800 | [diff] [blame] | 1438 | { |
Lance Vincent | 0b0117c | 2015-01-28 20:31:42 +0800 | [diff] [blame] | 1439 | return in_array($value, explode(',', $list), TRUE); |
Lance Vincent | eca885d | 2015-01-28 17:43:23 +0800 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | // -------------------------------------------------------------------- |
| 1443 | |
| 1444 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1445 | * Is a Natural number (0,1,2,3, etc.) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1446 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1447 | * @param string |
| 1448 | * @return bool |
| 1449 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1450 | public function is_natural($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1451 | { |
Andrey Andreev | acb962e | 2012-06-27 12:04:24 +0300 | [diff] [blame] | 1452 | return ctype_digit((string) $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | // -------------------------------------------------------------------- |
| 1456 | |
| 1457 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1458 | * Is a Natural number, but not a zero (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_no_zero($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 ($str != 0 && ctype_digit((string) $str)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1468 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1469 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1470 | /** |
| 1471 | * Valid Base64 |
| 1472 | * |
| 1473 | * Tests a string for characters outside of the Base64 alphabet |
| 1474 | * as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045 |
| 1475 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1476 | * @param string |
| 1477 | * @return bool |
| 1478 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1479 | public function valid_base64($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1480 | { |
Andrey Andreev | cd9797a | 2013-06-28 14:03:48 +0300 | [diff] [blame] | 1481 | return (base64_encode(base64_decode($str)) === $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1482 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1483 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1484 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1485 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1486 | /** |
| 1487 | * Prep data for form |
| 1488 | * |
| 1489 | * This function allows HTML to be safely shown in a form. |
| 1490 | * Special characters are converted. |
| 1491 | * |
Andrey Andreev | 4f55507 | 2016-03-12 17:21:55 +0200 | [diff] [blame] | 1492 | * @deprecated 3.0.6 Not used anywhere within the framework and pretty much useless |
| 1493 | * @param mixed $data Input data |
| 1494 | * @return mixed |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1495 | */ |
Andrey Andreev | 4f55507 | 2016-03-12 17:21:55 +0200 | [diff] [blame] | 1496 | public function prep_for_form($data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1497 | { |
Andrey Andreev | 7c4d106 | 2012-11-01 15:14:34 +0200 | [diff] [blame] | 1498 | if ($this->_safe_form_data === FALSE OR empty($data)) |
| 1499 | { |
| 1500 | return $data; |
| 1501 | } |
| 1502 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1503 | if (is_array($data)) |
| 1504 | { |
| 1505 | foreach ($data as $key => $val) |
| 1506 | { |
| 1507 | $data[$key] = $this->prep_for_form($val); |
| 1508 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1509 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1510 | return $data; |
| 1511 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1512 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1513 | return str_replace(array("'", '"', '<', '>'), array(''', '"', '<', '>'), stripslashes($data)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1514 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1515 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1516 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1517 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1518 | /** |
| 1519 | * Prep URL |
| 1520 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1521 | * @param string |
| 1522 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1523 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1524 | public function prep_url($str = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1525 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1526 | if ($str === 'http://' OR $str === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1527 | { |
| 1528 | return ''; |
| 1529 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1530 | |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1531 | if (strpos($str, 'http://') !== 0 && strpos($str, 'https://') !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1532 | { |
Andrey Andreev | 901573c | 2012-01-11 01:40:48 +0200 | [diff] [blame] | 1533 | return 'http://'.$str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1534 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1535 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1536 | return $str; |
| 1537 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1538 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1539 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1540 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1541 | /** |
| 1542 | * Strip Image Tags |
| 1543 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1544 | * @param string |
| 1545 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1546 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1547 | public function strip_image_tags($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1548 | { |
Andrey Andreev | 1a24a9d | 2012-06-27 00:52:47 +0300 | [diff] [blame] | 1549 | return $this->CI->security->strip_image_tags($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1550 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1551 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1552 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1553 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1554 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1555 | * Convert PHP tags to entities |
| 1556 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1557 | * @param string |
| 1558 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1559 | */ |
Phil Sturgeon | 3837ae7 | 2011-05-09 21:12:26 +0100 | [diff] [blame] | 1560 | public function encode_php_tags($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1561 | { |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 1562 | return str_replace(array('<?', '?>'), array('<?', '?>'), $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 1565 | // -------------------------------------------------------------------- |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1566 | |
| 1567 | /** |
| 1568 | * Reset validation vars |
| 1569 | * |
| 1570 | * Prevents subsequent validation routines from being affected by the |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 1571 | * results of any previous validation routine due to the CI singleton. |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1572 | * |
Andrey Andreev | a89c1da | 2014-02-08 19:03:35 +0200 | [diff] [blame] | 1573 | * @return CI_Form_validation |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1574 | */ |
JonoB | 883f80f | 2012-03-05 09:51:27 +0000 | [diff] [blame] | 1575 | public function reset_validation() |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1576 | { |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 1577 | $this->_field_data = array(); |
JonoB | 099c478 | 2012-03-04 14:37:30 +0000 | [diff] [blame] | 1578 | $this->_error_array = array(); |
| 1579 | $this->_error_messages = array(); |
| 1580 | $this->error_string = ''; |
Andrey Andreev | a89c1da | 2014-02-08 19:03:35 +0200 | [diff] [blame] | 1581 | return $this; |
Andrey Andreev | c8da4fe | 2012-03-04 19:20:33 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1584 | } |