├── .gitignore ├── README.md ├── Version └── application └── libraries └── Validation.php /.gitignore: -------------------------------------------------------------------------------- 1 | */config/development 2 | */logs/log-*.php 3 | */logs/!index.html 4 | */cache/* 5 | */cache/!index.html 6 | 7 | .DS_Store 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CodeIgniter Form Validation 2 | ================================= 3 | 4 | CodeIgniter library for fields form validation. It is independent of the standard library of CodeIgniter and can also be used for Ajax calls. 5 | 6 | ----- 7 | 8 | ###How to install 9 | 10 | 1. Add the file Validation.php to your /application/libraries folder. 11 | 12 | ###How to use 13 | 14 | 1. Load library: 15 | 16 | $this->load->library('validation'); 17 | 18 | 2. Load data from: 19 | 20 | a. POST (default): 21 | 22 | $this->validation->set_post(); 23 | 24 | b. GET: 25 | 26 | $this->validation->set_get(); 27 | 28 | c. Array data: 29 | 30 | $this->validation->set_data($data); 31 | 32 | 3. Load your rules: 33 | 34 | $this->validation->required… 35 | 36 | ####Public functions List 37 | 38 | - `set_data` Set fields data from array 39 | - `set_post` Set fields data from POST 40 | - `set_get` Set fields data from GET 41 | - `get_data` Get array data or a single field value (if key passed) 42 | - `get_error` Return error data: message and field (if exists) 43 | - `get_error_message` Return error message 44 | - `get_error_field` Return error field 45 | - `is_valid` Check if form is valid 46 | - `set_not_valid` Set form not valid ad assign message error 47 | - `required` Check required fields 48 | - `required_isset` Check required fields only if is set 49 | - `email` Check if email fields are valid 50 | - `regexp` Check that the fields meet a particular regular expression 51 | - `url` Check URL fields are valid 52 | - `maxlen` Check that fields are not longer than a defined value 53 | - `minlen` Check that fields are not shorter than a defined value 54 | - `alpha` Check that fields do not have characters other than letters of alphabet 55 | - `alpha_s` Check that fields do not have characters other than letters of alphabet and space 56 | - `num` Check that fields do not have characters other than numbers 57 | - `num_s` Check that fields do not have characters other than numbers and space 58 | - `alphanum` Check that fields do not have characters other than letters and numbers 59 | - `alphanum_s` Check that fields do not have characters other than letters of alphabet, numbers and space 60 | - `no_spaces` Check that fields do not have spaces 61 | - `num_gt` Check if a numeric field is greater than a certain value 62 | - `num_lt` Check if a numeric field is less than a certain value 63 | - `date` Check that the fields have a date 64 | - `date_gt` Check that a date is larger than other 65 | - `date_lt` Check that a date is smaller than other 66 | - `date_en` Check that the fields have a English date 67 | - `datetime` Check that the fields have a datetime value 68 | - `checked` Check that the field has been checked 69 | - `selected` Check that the field has been selected 70 | - `equal` Check that the two fields are equal 71 | - `callback` Check the correctness of the field $param through the method $method. 72 | 73 | ###How to work 74 | 75 | public function save() { 76 | $this->load->library('validation'); 77 | $this->validation->set_data($data); 78 | // $this->validation->set_post(); 79 | 80 | $this->validation->required(array('email', 'username', 'firstname', 'lastname', 'city', 'password'), 'Fields are required'), 81 | ->email('email', 'Email is not valid field') 82 | ->maxlen('username', 32, 'Username cannot be longer than 32 characters') 83 | ->minlen('username', 6, 'Username cannot be shorter than 6 characters') 84 | ->regxp('username', '/^([a-zA-Z0-9\-]*)$/i', 'Username cannot have characters other than letters, numbers and hyphens') 85 | ->callback('_unique', 'Username already exists.', 'username') 86 | ->callback(array($your_model, 'method_name'), 'Error message', array('parameter1', 'parameter2', 'parameter3')) 87 | ->callback('_other_func', 'Your error.'); 88 | 89 | if ($this->validation->is_valid()) { 90 | if ($data['username'] == 'admin') 91 | $this->validation->set_not_valid('Username is already registered'); 92 | 93 | } 94 | 95 | if ($this->validation->is_valid()) 96 | echo 'success!'; 97 | else 98 | echo $this->validation->get_error_message(); 99 | } 100 | 101 | public function _unique($key) { 102 | $value = $this->validation->get_data($key); 103 | $res = $this->db->query('…'); 104 | return $res->id != 0; 105 | } 106 | 107 | public function _other_func() { 108 | // Check something 109 | return $error; 110 | } 111 | -------------------------------------------------------------------------------- /Version: -------------------------------------------------------------------------------- 1 | 1.3 2 | -------------------------------------------------------------------------------- /application/libraries/Validation.php: -------------------------------------------------------------------------------- 1 | 6 | * @link http://innato.it 7 | * @version 1.3 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT 15 | * HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, 16 | * INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR 17 | * FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE 18 | * OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, 19 | * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.COPYRIGHT HOLDERS WILL NOT 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL 21 | * DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENTATION. 22 | * 23 | * You should have received a copy of the GNU General Public License 24 | * along with this program. If not, see . 25 | */ 26 | class Validation { 27 | 28 | public $CI; 29 | 30 | protected $data = array(); 31 | protected $config = array(); 32 | protected $validate = TRUE; 33 | protected $error_message = ''; 34 | protected $error_field = NULL; 35 | 36 | /** 37 | * Constructor. 38 | * 39 | * @access public 40 | * @return void 41 | */ 42 | public function __construct($config = NULL) { 43 | $this->CI =& get_instance(); 44 | 45 | if ( ! empty($config)) { 46 | $this->initialize($config); 47 | } 48 | $this->set_post(); // Default data 49 | } 50 | 51 | // ------------------------------------------------------------------------ 52 | 53 | /** 54 | * Initialize library. 55 | * 56 | * @access public 57 | * @param mixed $data 58 | * @return void 59 | */ 60 | public function initialize($config) { 61 | $this->config = $config; 62 | } 63 | 64 | // ------------------------------------------------------------------------ 65 | 66 | /** 67 | * Set fields data from array. 68 | * 69 | * @access public 70 | * @param mixed $data 71 | * @return void 72 | */ 73 | public function set_data($data) { 74 | $this->data = (array) $data; 75 | return $this; 76 | } 77 | 78 | // ------------------------------------------------------------------------ 79 | 80 | /** 81 | * Set fields data from POST. 82 | * 83 | * @access public 84 | * @return void 85 | */ 86 | public function set_post() { 87 | $this->set_data($this->CI->input->post()); 88 | return $this; 89 | } 90 | 91 | // ------------------------------------------------------------------------ 92 | 93 | /** 94 | * Set fields data from GET. 95 | * 96 | * @access public 97 | * @return void 98 | */ 99 | public function set_get() { 100 | $this->set_data($this->CI->input->get()); 101 | return $this; 102 | } 103 | 104 | // ------------------------------------------------------------------------ 105 | 106 | /** 107 | * Get array data or a single field value (if key passed). 108 | * 109 | * @method get_data 110 | * @param string $key 111 | * @return mixed 112 | */ 113 | public function get_data($key = NULL) { 114 | return is_null($key) 115 | ? $this->data 116 | : (isset($this->data[$key]) ? $this->data[$key] : NULL); 117 | } 118 | 119 | // ------------------------------------------------------------------------ 120 | 121 | /** 122 | * Exit due error. 123 | * 124 | * @access private 125 | * @param mixed $error 126 | * @param mixed $field (default: NULL) 127 | * @return void 128 | */ 129 | private function _error($error, $field = NULL) { 130 | if ($this->validate) { 131 | $this->error_message = is_null($field) 132 | ? $error 133 | : sprintf($error, $field); 134 | $this->error_field = $field; 135 | $this->validate = FALSE; 136 | } 137 | } 138 | 139 | // ------------------------------------------------------------------------ 140 | 141 | /** 142 | * Return error data: message and field (if exists). 143 | * 144 | * @access public 145 | * @return array 146 | */ 147 | public function get_error() { 148 | return array( 149 | 'message' => $this->error_message 150 | , 'field' => $this->error_field 151 | ); 152 | } 153 | 154 | // ------------------------------------------------------------------------ 155 | 156 | /** 157 | * Return error message. 158 | * 159 | * @access public 160 | * @return string 161 | */ 162 | public function get_error_message() { 163 | return $this->error_message; 164 | } 165 | 166 | // ------------------------------------------------------------------------ 167 | 168 | /** 169 | * Return error field. 170 | * 171 | * @access public 172 | * @return string 173 | */ 174 | public function get_error_field() { 175 | return $this->error_field; 176 | } 177 | 178 | // ------------------------------------------------------------------------ 179 | 180 | /** 181 | * Check if form is valid. 182 | * 183 | * @access public 184 | * @return void 185 | */ 186 | public function is_valid() { 187 | return $this->validate; 188 | } 189 | 190 | // ------------------------------------------------------------------------ 191 | 192 | /** 193 | * Set form as invalid by passing the string corresponding error. 194 | * 195 | * @access public 196 | * @param string $error (default: '') 197 | * @return void 198 | */ 199 | public function set_not_valid($error = '') { 200 | $this->_error($error); 201 | } 202 | 203 | // ------------------------------------------------------------------------ 204 | 205 | /** 206 | * If you pass string parameter and not array, puts it in an array. 207 | * 208 | * @access private 209 | * @param mixed &$param 210 | * @return void 211 | */ 212 | private function _parse( & $param) { 213 | if ( ! is_array($param)) { 214 | $param = array($param); 215 | } 216 | } 217 | 218 | // ------------------------------------------------------------------------ 219 | 220 | /** 221 | * Check required fields. 222 | * 223 | * @access public 224 | * @param mixed $fields 225 | * @param string $err_msg (default: '') 226 | * @return void 227 | */ 228 | public function required($fields, $err_msg = '') { 229 | $this->_parse($fields); 230 | foreach ($fields as $v) { 231 | if ($this->is_valid()) { 232 | $this->data[$v] = isset($this->data[$v]) ? trim($this->data[$v]) : ''; 233 | if (empty($this->data[$v]) && $this->data[$v] !== 0) { 234 | $this->_error($err_msg, $v); 235 | } 236 | } 237 | } 238 | return $this; 239 | } 240 | 241 | // ------------------------------------------------------------------------ 242 | 243 | /** 244 | * Check required fields only if isset. 245 | * 246 | * @access public 247 | * @param mixed $fields 248 | * @param string $err_msg (default: '') 249 | * @return void 250 | */ 251 | public function required_isset($fields, $err_msg = '') { 252 | $this->_parse($fields); 253 | foreach ($fields as $v) { 254 | if ($this->is_valid()) { 255 | if (isset($this->data[$v])) { 256 | $this->data[$v] = trim($this->data[$v]); 257 | if (empty($this->data[$v])) { 258 | $this->_error($err_msg, $v); 259 | } 260 | } 261 | 262 | } 263 | } 264 | return $this; 265 | } 266 | 267 | // ------------------------------------------------------------------------ 268 | 269 | /** 270 | * Check if email fields are valid. 271 | * 272 | * @access public 273 | * @param mixed $fields 274 | * @param string $err_msg (default: '') 275 | * @return void 276 | */ 277 | public function email($fields, $err_msg = '') { 278 | $this->_parse($fields); 279 | $this->CI->load->helper('email'); 280 | foreach ($fields as $v) { 281 | if ($this->is_valid()) { 282 | if ( ! empty($this->data[$v])) { 283 | if ( ! valid_email($this->data[$v])) { 284 | $this->_error($err_msg, $v); 285 | } 286 | } 287 | } 288 | } 289 | return $this; 290 | } 291 | 292 | // ------------------------------------------------------------------------ 293 | 294 | /** 295 | * Check that the fields meet a particular regular expression. 296 | * 297 | * @access public 298 | * @param mixed $fields 299 | * @param mixed $regexp 300 | * @param string $err_msg (default: '') 301 | * @return void 302 | */ 303 | public function regexp($fields, $regexp, $err_msg = '') { 304 | $this->_parse($fields); 305 | foreach ($fields as $v) { 306 | if ($this->is_valid()) { 307 | if ( ! empty($this->data[$v])) { 308 | if ( ! preg_match($regexp, $this->data[$v])) { 309 | $this->_error($err_msg, $v); 310 | } 311 | } 312 | } 313 | } 314 | return $this; 315 | } 316 | 317 | // ------------------------------------------------------------------------ 318 | 319 | /** 320 | * Check URL fields. 321 | * 322 | * @access public 323 | * @param mixed $fields 324 | * @param string $err_msg (default: '') 325 | * @return void 326 | */ 327 | public function url($fields, $err_msg = '') { 328 | $regexp = '/^(https?\:\/\/){0,1}(www\.){0,1}' 329 | .'([a-z0-9-_.]+)(\.{1})([a-z]{2,4})$/i'; 330 | return $this->regexp($fields, $regexp, $err_msg); 331 | } 332 | 333 | // ------------------------------------------------------------------------ 334 | 335 | /** 336 | * Check that fields are not longer than a defined value. 337 | * 338 | * @access public 339 | * @param mixed $fields 340 | * @param mixed $len 341 | * @param string $err_msg (default: '') 342 | * @return void 343 | */ 344 | public function maxlen($fields, $len, $err_msg = '') { 345 | $this->_parse($fields); 346 | foreach ($fields as $v) { 347 | if ($this->is_valid()) { 348 | if ( ! empty($this->data[$v])) { 349 | if (strlen($this->data[$v]) > $len) { 350 | $this->_error($err_msg, $v); 351 | } 352 | } 353 | } 354 | } 355 | return $this; 356 | } 357 | 358 | // ------------------------------------------------------------------------ 359 | 360 | /** 361 | * Check that fields are not shorter than a defined value. 362 | * 363 | * @access public 364 | * @param mixed $fields 365 | * @param mixed $len 366 | * @param string $err_msg (default: '') 367 | * @return void 368 | */ 369 | public function minlen($fields, $len, $err_msg = '') { 370 | $this->_parse($fields); 371 | foreach ($fields as $v) { 372 | if ($this->is_valid()) { 373 | if ( ! empty($this->data[$v])) { 374 | if (strlen($this->data[$v]) < $len) { 375 | $this->_error($err_msg); 376 | } 377 | } 378 | } 379 | } 380 | return $this; 381 | } 382 | 383 | // ------------------------------------------------------------------------ 384 | 385 | /** 386 | * Check that fields do not have characters other than letters of alphabet. 387 | * 388 | * @access public 389 | * @param mixed $fields 390 | * @param string $err_msg (default: '') 391 | * @return void 392 | */ 393 | public function alpha($fields, $err_msg = '') { 394 | $regexp = '/^([A-Za-z]+)$/'; 395 | return $this->regexp($fields, $regexp, $err_msg); 396 | } 397 | 398 | // ------------------------------------------------------------------------ 399 | 400 | /** 401 | * Check that fields do not have characters other than letters of 402 | * alphabet and space. 403 | * 404 | * @access public 405 | * @param mixed $fields 406 | * @param string $err_msg (default: '') 407 | * @return void 408 | */ 409 | public function alpha_s($fields, $err_msg = '') { 410 | $regexp = '/^([A-Za-z\ ]+)$/'; 411 | return $this->regexp($fields, $regexp, $err_msg); 412 | } 413 | 414 | // ------------------------------------------------------------------------ 415 | 416 | /** 417 | * Check that fields do not have characters other than numbers. 418 | * 419 | * @access public 420 | * @param mixed $fields 421 | * @param string $err_msg (default: '') 422 | * @return void 423 | */ 424 | public function num($fields, $err_msg = '') { 425 | $regexp = '/^([0-9]+)$/'; 426 | return $this->regexp($fields, $regexp, $err_msg); 427 | } 428 | 429 | // ------------------------------------------------------------------------ 430 | 431 | /** 432 | * Check that fields do not have characters other than numbers and space. 433 | * 434 | * @access public 435 | * @param mixed $fields 436 | * @param string $err_msg (default: '') 437 | * @return void 438 | */ 439 | public function num_s($fields, $err_msg = '') { 440 | $regexp = '/^([0-9\ ]+)$/'; 441 | return $this->regexp($fields, $regexp, $err_msg); 442 | } 443 | 444 | // ------------------------------------------------------------------------ 445 | 446 | /** 447 | * Check that fields do not have characters other than letters and numbers. 448 | * 449 | * @access public 450 | * @param mixed $fields 451 | * @param string $err_msg (default: '') 452 | * @return void 453 | */ 454 | public function alphanum($fields, $err_msg = '') { 455 | $regexp = '/^([A-Za-z0-9]+)$/'; 456 | return $this->regexp($fields, $regexp, $err_msg); 457 | } 458 | 459 | // ------------------------------------------------------------------------ 460 | 461 | /** 462 | * Check that fields do not have characters other than letters of 463 | * alphabet, numbers and space. 464 | * 465 | * @access public 466 | * @param mixed $fields 467 | * @param string $err_msg (default: '') 468 | * @return void 469 | */ 470 | public function alphanum_s($fields, $err_msg = '') { 471 | $regexp = '/^([A-Za-z0-9\ ]+)$/'; 472 | return $this->regexp($fields, $regexp, $err_msg); 473 | } 474 | 475 | // ------------------------------------------------------------------------ 476 | 477 | /** 478 | * Check that fields do not have spaces. 479 | * 480 | * @access public 481 | * @param mixed $fields 482 | * @param string $err_msg (default: '') 483 | * @return void 484 | */ 485 | public function no_spaces($fields, $err_msg = '') { 486 | $regexp = '/^([^\ ]+)$/'; 487 | return $this->regexp($fields, $regexp, $err_msg); 488 | } 489 | 490 | // ------------------------------------------------------------------------ 491 | 492 | /** 493 | * Check if a numeric field is greater than a certain value. 494 | * 495 | * @access public 496 | * @param mixed $fields 497 | * @param mixed $num 498 | * @param string $err_msg (default: '') 499 | * @return void 500 | */ 501 | public function num_gt($fields, $num, $err_msg = '') { 502 | $this->_parse($fields); 503 | foreach ($fields as $v) { 504 | $this->num($v, $err_msg); 505 | if ($this->is_valid()) { 506 | if ($this->data[$v] < $num) { 507 | $this->_error($err_msg, $v); 508 | } 509 | } 510 | } 511 | return $this; 512 | } 513 | 514 | // ------------------------------------------------------------------------ 515 | 516 | /** 517 | * Check if a numeric field is less than a certain value. 518 | * 519 | * @access public 520 | * @param mixed $fields 521 | * @param mixed $num 522 | * @param string $err_msg (default: '') 523 | * @return void 524 | */ 525 | public function num_lt($fields, $num, $err_msg = '') { 526 | $this->_parse($fields); 527 | foreach ($fields as $v) { 528 | $this->num($v, $err_msg); 529 | if ($this->is_valid()) { 530 | if ($this->data[$v] > $num) { 531 | $this->_error($err_msg, $v); 532 | } 533 | } 534 | } 535 | return $this; 536 | } 537 | 538 | // ------------------------------------------------------------------------ 539 | 540 | /** 541 | * Check that the fields have a date. 542 | * 543 | * @access public 544 | * @param mixed $fields 545 | * @param string $err_msg (default: '') 546 | * @return void 547 | */ 548 | public function date($fields, $err_msg = '') { 549 | $exp = '/^([0-9]{2})([^A-Za-z0-9]{1})([0-9]{2})' 550 | .'([^A-Za-z0-9]{1})([0-9]{4})$/'; 551 | $this->_parse($fields); 552 | foreach ($fields as $v) { 553 | if ($this->is_valid()) { 554 | if ( ! empty($this->data[$v])) { 555 | $match = array(); 556 | if ( ! preg_match($exp, $this->data[$v], $match)) { 557 | $this->_error($err_msg, $v); 558 | } elseif ( ! checkdate($match[3], $match[1], $match[5])) { 559 | $this->_error($err_msg, $v); 560 | } 561 | } 562 | } 563 | } 564 | return $this; 565 | } 566 | 567 | // ------------------------------------------------------------------------ 568 | 569 | /** 570 | * Check the difference between two dates. 571 | * 572 | * @access private 573 | * @param mixed $date_1 574 | * @param mixed $date_2 575 | * @return void 576 | */ 577 | private function date_diff($date_1, $date_2) { 578 | $d1 = strtotime($this->data[$date_1]); 579 | $d2 = strtotime($this->data[$date_2]); 580 | return round(($d1 - $d2)/60/60/24); 581 | } 582 | 583 | // ------------------------------------------------------------------------ 584 | 585 | /** 586 | * Check that a date is larger than other. 587 | * 588 | * @access public 589 | * @param mixed $date_1 590 | * @param mixed $date_2 591 | * @param string $err_msg (default: '') 592 | * @return void 593 | */ 594 | public function date_gt($date_1, $date_2, $err_msg = '') { 595 | if ($this->is_valid()) { 596 | if ( ! empty($date_1) && ! empty($date_2)) { 597 | if ($this->date_diff($date_1, $date_2, $err_msg) > 0) { 598 | $this->_error($err_msg); 599 | } 600 | } 601 | } 602 | return $this; 603 | } 604 | 605 | // ------------------------------------------------------------------------ 606 | 607 | /** 608 | * Check that a date is smaller than other. 609 | * 610 | * @access public 611 | * @param mixed $date_1 612 | * @param mixed $date_2 613 | * @param string $err_msg (default: '') 614 | * @return void 615 | */ 616 | public function date_lt($date_1, $date_2, $err_msg = '') { 617 | if ($this->is_valid()) { 618 | if ( ! empty($date_1) && ! empty($date_2)) { 619 | if ($this->date_diff($date_1, $date_2, $err_msg) < 0) { 620 | $this->_error($err_msg); 621 | } 622 | } 623 | } 624 | return $this; 625 | } 626 | 627 | // ------------------------------------------------------------------------ 628 | 629 | /** 630 | * Check that the fields have a English date. 631 | * 632 | * @access public 633 | * @param mixed $fields 634 | * @param string $err_msg (default: '') 635 | * @return void 636 | */ 637 | public function date_en($fields, $err_msg = '') { 638 | $exp = '/^([0-9]{4})([^A-Za-z0-9]{1})' 639 | .'([0-9]{2})([^A-Za-z0-9]{1})([0-9]{2})$/'; 640 | $this->_parse($fields); 641 | foreach ($fields as $v) { 642 | if ($this->is_valid()) { 643 | if ( ! empty($this->data[$v]) 644 | && $this->data[$v] != '0000-00-00') { 645 | $match = array(); 646 | if ( ! preg_match($exp, $this->data[$v], $match)) { 647 | $this->_error($err_msg, $v); 648 | } elseif ( ! checkdate($match[3], $match[5], $match[1])) { 649 | $this->_error($err_msg, $v); 650 | } 651 | } 652 | } 653 | } 654 | return $this; 655 | } 656 | 657 | // ------------------------------------------------------------------------ 658 | 659 | /** 660 | * Check that the fields have a datetime value. 661 | * 662 | * @access public 663 | * @param mixed $fields 664 | * @param string $err_msg (default: '') 665 | * @return void 666 | */ 667 | public function datetime($fields, $err_msg = '') { 668 | $this->_parse($fields); 669 | $exp = '/^([0-9]{4})([\-])([0-9]{2})([\-])([0-9]{2})[\ ]' 670 | .'([0-9]{2})[\:]([0-9]{2})[\:]([0-9]{2})$/'; 671 | foreach ($fields as $v) { 672 | if ($this->is_valid()) { 673 | if ( ! empty($this->data[$v]) 674 | && $this->data[$v] != '0000-00-00 00:00:00') { 675 | $match = array(); 676 | if ( ! preg_match($exp, $this->data[$v], $match)) { 677 | $this->_error($err_msg, $v); 678 | } elseif ( ! checkdate($match[3], $match[5], $match[1])) { 679 | $this->_error($err_msg, $v); 680 | } 681 | } 682 | } 683 | } 684 | return $this; 685 | } 686 | 687 | // ------------------------------------------------------------------------ 688 | 689 | /** 690 | * Check that the field has been checked. 691 | * 692 | * @access public 693 | * @param mixed $field 694 | * @param mixed $checked_value 695 | * @param string $err_msg (default: '') 696 | * @return void 697 | */ 698 | public function checked($field, $checked_value, $err_msg = '') { 699 | if ($this->is_valid()) { 700 | if (strcmp($this->data[$field], $checked_value) !== 0) { 701 | $this->_error($err_msg); 702 | } 703 | } 704 | return $this; 705 | } 706 | 707 | // ------------------------------------------------------------------------ 708 | 709 | /** 710 | * Check that the field has been selected. 711 | * 712 | * @access public 713 | * @param mixed $field 714 | * @param string $err_msg (default: '') 715 | * @param string $empty_value (default: '') 716 | * @return void 717 | */ 718 | public function selected($field, $err_msg = '', $empty_value = '') { 719 | if ($this->is_valid()) { 720 | if (strcmp($this->data[$field], $empty_value) !== 0) { 721 | $this->_error($err_msg); 722 | } 723 | } 724 | return $this; 725 | } 726 | 727 | // ------------------------------------------------------------------------ 728 | 729 | /** 730 | * Check that the two fields are equal. 731 | * 732 | * @access public 733 | * @param mixed $field_1 734 | * @param mixed $field_2 735 | * @param string $err_msg (default: '') 736 | * @return void 737 | */ 738 | public function equal($field_1, $field_2, $err_msg = '') { 739 | if ($this->is_valid()) { 740 | if (strcmp($this->data[$field_1], $this->data[$field_2]) !== 0) { 741 | $this->_error($err_msg); 742 | } 743 | } 744 | return $this; 745 | } 746 | 747 | // ------------------------------------------------------------------------ 748 | 749 | /** 750 | * Check the correctness of the field $param through the method $method 751 | * 752 | * @access public 753 | * @param mixed $callback 754 | * @param mixed $err_msg 755 | * @param mixed $parameters (default: array()) 756 | * @return void 757 | */ 758 | public function callback($callback, $err_msg, $parameters = array()) { 759 | if ($this->is_valid()) { 760 | 761 | // If $callback is a string, transform to array 762 | if ( ! is_array($callback)) { 763 | $callback = array($this->CI, $callback); 764 | } 765 | 766 | if ( ! method_exists($callback[0], $callback[1])) { 767 | $this->_error('Method `'. $callback[1] .'()` not exists.'); 768 | } else { 769 | 770 | // If method exists, call func with data parameters 771 | if ( ! is_array($parameters)) { 772 | $parameters = array($parameters); 773 | } 774 | 775 | // Call method 776 | if ( ! call_user_func_array($callback, $parameters)) { 777 | $this->_error($err_msg); 778 | } 779 | } 780 | } 781 | return $this; 782 | } 783 | } 784 | 785 | /* End of file Validation.php */ 786 | /* Location: ./application/libraries/Validation.php */ 787 | --------------------------------------------------------------------------------