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