Changeset 136 for trunk/lib/FormValidator.inc.php
- Timestamp:
- Jun 3, 2006 7:47:48 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/FormValidator.inc.php
r121 r136 4 4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information 5 5 * 6 * The FormValidator ::class provides a method for validating input from6 * The FormValidator class provides a method for validating input from 7 7 * http requests and displaying errors. 8 8 * … … 10 10 * @version 1.8 11 11 * 12 * Examples of use: 13 * 14 * require_once 'codebase/lib/FormValidator.inc.php'; 15 * $fv = new FormValidator(); 16 * 17 * $fv->isEmpty('location_name', _("<strong>Location name</strong> cannot be blank.")); 18 * $fv->checkRegex('cc_exp', '/^\d{4}$|^$/', true, _("CC exp date must be in MMYY format.")); 19 * $fv->isInteger('client_id', _("<strong>Client id</strong> must be an integer.")); 20 * $fv->numericRange('client_id', -32768, 32767, _("<strong>Client id</strong> must be a number between -32768 and 32767.")); 21 * $fv->stringLength('zip', 0, 255, _("<strong>Zip</strong> must contain less than 256 characters.")); 22 * $fv->validateEmail('invoice_email'); 23 * $fv->validatePhone('phone1'); 12 * Example of use: 13 --------------------------------------------------------------------- 14 // The object that validates form input. 15 require_once 'codebase/lib/FormValidator.inc.php'; 16 $fv = new FormValidator(); 17 18 $fv->isEmpty('field_name', sprintf(_("%s cannot be blank."), _("Field name"))); 19 $fv->stringLength('field_name', 0, 255, sprintf(_("%s must be %f-to-%f characters in length."), _("Field name"), 0, 255)); 20 $fv->isInteger('field_name', sprintf(_("%s must be an integer."), _("Field name"))); 21 $fv->checkRegex('field_name', '/^\d{4}$|^$/', true, sprintf(_("%s must be in MMYY format."), _("Field name"))); 22 $fv->numericRange('field_name', 0, 65535, sprintf(_("%s must be a number between %f and %f."), _("Field name"), 0, 65535)); 23 $fv->validatePhone('field_name'); 24 $fv->validateEmail('field_name'); 25 $fv->validateStrDate('field_name', sprintf(_("%s must be a valid date in YYYY-MM-DD format."), _("Field name"))); 26 if (is_null($var)) { 27 $fv->addError('field_name', sprintf(_("%s is invalid."), _("Field name"))); 28 } 29 if ($fv->anyErrors()) { 30 // Errors! 31 } 32 --------------------------------------------------------------------- 24 33 */ 25 class FormValidator 26 { 34 class FormValidator { 27 35 28 36 // Array filling with error messages. … … 103 111 function printErrorMessages() 104 112 { 113 $app =& App::getInstance(); 105 114 if ($this->anyErrors()) { 106 115 ?><div class="sc-msg"><?php 107 116 $errors = $this->getErrorList(); 108 foreach ($errors as $e rr) {109 if ( !empty($err['message']) && is_string($err['message'])) {110 if (error_reporting() > 0 && isset($err['file']) && isset($err['line'])) {111 echo "\n<!-- [" . $e rr['file'] . ' : ' . $err['line'] . '] -->';117 foreach ($errors as $e) { 118 if ('' != $e['message'] && is_string($e['message'])) { 119 if (error_reporting() > 0 && $app->getParam('display_errors') && isset($e['file']) && isset($e['line'])) { 120 echo "\n<!-- [" . $e['file'] . ' : ' . $e['line'] . '] -->'; 112 121 } 113 switch ($e rr['type']) {122 switch ($e['type']) { 114 123 case MSG_ERR: 115 echo '<div class="sc-msg-error">' . $e rr['message'] . '</div>';124 echo '<div class="sc-msg-error">' . $e['message'] . '</div>'; 116 125 break; 117 126 118 127 case MSG_WARNING: 119 echo '<div class="sc-msg-warning">' . $e rr['message'] . '</div>';128 echo '<div class="sc-msg-warning">' . $e['message'] . '</div>'; 120 129 break; 121 130 122 131 case MSG_SUCCESS: 123 echo '<div class="sc-msg-success">' . $e rr['message'] . '</div>';132 echo '<div class="sc-msg-success">' . $e['message'] . '</div>'; 124 133 break; 125 134 126 135 case MSG_NOTICE: 127 136 default: 128 echo '<div class="sc-msg-notice">' . $e rr['message'] . '</div>';137 echo '<div class="sc-msg-notice">' . $e['message'] . '</div>'; 129 138 break; 130 139 } … … 389 398 function validateEmail($form_name) 390 399 { 400 $app =& App::getInstance(); 401 391 402 $email = getFormData($form_name); 392 403 if ('' == trim($email)) { … … 400 411 if (!preg_match($e->getParam('regex'), $email, $e_parts)) { 401 412 $this->addError($form_name, sprintf(_("<strong>%s</strong> is not a valid email address."), oTxt($email))); 402 App::logMsg(sprintf('The email address %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__);413 $app->logMsg(sprintf('The email address %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 403 414 return false; 404 415 } … … 412 423 if (strlen($local) > 64 || strlen($domain) > 191) { 413 424 $this->addError($form_name, sprintf(_("<strong>Email address</strong> must contain less than 256 characters."), oTxt($email))); 414 App::logMsg(sprintf('The email address %s must contain less than 256 characters.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__);425 $app->logMsg(sprintf('The email address %s must contain less than 256 characters.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 415 426 return false; 416 427 } … … 420 431 if ((ip2long($domain) == '-1' || ip2long($domain) === false) && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) { 421 432 $this->addError($form_name, sprintf(_("<strong>%s</strong> is not a valid email domain name"), oTxt($domain))); 422 App::logMsg(sprintf('The email address %s contains an invalid email domain name (%s).', getFormData($form_name), $domain), LOG_INFO, __FILE__, __LINE__);433 $app->logMsg(sprintf('The email address %s contains an invalid email domain name (%s).', getFormData($form_name), $domain), LOG_INFO, __FILE__, __LINE__); 423 434 return false; 424 435 } … … 454 465 function validateStrDate($form_name, $msg='') 455 466 { 467 $app =& App::getInstance(); 468 456 469 if (($timestamp = strtotime(getFormData($form_name, '0'))) === -1) { 457 470 $this->addError($form_name, $msg); 458 App::logMsg(sprintf('The string date %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__);471 $app->logMsg(sprintf('The string date %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); 459 472 return false; 460 473 } else { … … 530 543 for ($i=0; $i<$num; $i++) { 531 544 // Get each digit. 532 $digit = substr($cc_num, $i,1);545 $digit = substr($cc_num, $i, 1); 533 546 534 547 // If it's an odd digit, double it. … … 539 552 // If the result is two digits, add them. 540 553 if (strlen($digit) == 2) { 541 $digit = substr($digit, 0,1) + substr($digit,1,1);554 $digit = substr($digit, 0, 1) + substr($digit, 1, 1); 542 555 } 543 556 … … 556 569 557 570 /** 558 * Check whether uploaded file is valid.571 * Check whether a file was selected for uploading. If file is missing, it's an error. 559 572 * 560 573 * @param string $form_name the name of the incoming form variable … … 565 578 function validateFile($form_name, $msg='') 566 579 { 567 if ( isset($_FILES[$form_name]['tmp_name']) &&'' == trim($_FILES[$form_name]['tmp_name'])) {580 if (!isset($_FILES[$form_name]['tmp_name']) || '' == trim($_FILES[$form_name]['tmp_name'])) { 568 581 $this->addError($form_name, $msg); 569 582 return false;
Note: See TracChangeset
for help on using the changeset viewer.