[1] | 1 | <?php |
---|
| 2 | /** |
---|
[42] | 3 | * FormValidator.inc.php |
---|
[1] | 4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
| 5 | * |
---|
[136] | 6 | * The FormValidator class provides a method for validating input from |
---|
[1] | 7 | * http requests and displaying errors. |
---|
| 8 | * |
---|
[144] | 9 | * @requires codebase/lib/Validator.inc.php |
---|
[1] | 10 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[106] | 11 | * @version 1.8 |
---|
[1] | 12 | * |
---|
[136] | 13 | * Example of use: |
---|
| 14 | --------------------------------------------------------------------- |
---|
| 15 | // The object that validates form input. |
---|
| 16 | require_once 'codebase/lib/FormValidator.inc.php'; |
---|
| 17 | $fv = new FormValidator(); |
---|
| 18 | |
---|
[144] | 19 | $fv->empty('field_name', sprintf(_("%s cannot be blank."), _("Field name"))); |
---|
[151] | 20 | $fv->stringLength('field_name', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Field name"), 0, 255)); |
---|
[136] | 21 | $fv->isInteger('field_name', sprintf(_("%s must be an integer."), _("Field name"))); |
---|
| 22 | $fv->checkRegex('field_name', '/^\d{4}$|^$/', true, sprintf(_("%s must be in MMYY format."), _("Field name"))); |
---|
[151] | 23 | $fv->numericRange('field_name', 0, 65535, sprintf(_("%s must be a number between %d and %d."), _("Field name"), 0, 65535)); |
---|
[136] | 24 | $fv->validatePhone('field_name'); |
---|
| 25 | $fv->validateEmail('field_name'); |
---|
| 26 | $fv->validateStrDate('field_name', sprintf(_("%s must be a valid date in YYYY-MM-DD format."), _("Field name"))); |
---|
| 27 | if (is_null($var)) { |
---|
| 28 | $fv->addError('field_name', sprintf(_("%s is invalid."), _("Field name"))); |
---|
| 29 | } |
---|
| 30 | if ($fv->anyErrors()) { |
---|
| 31 | // Errors! |
---|
| 32 | } |
---|
| 33 | --------------------------------------------------------------------- |
---|
[1] | 34 | */ |
---|
[42] | 35 | |
---|
[144] | 36 | // Credit card types are defined in class Validator. |
---|
| 37 | |
---|
| 38 | require_once 'codebase/lib/Validator.inc.php'; |
---|
| 39 | |
---|
| 40 | class FormValidator extends Validator { |
---|
| 41 | |
---|
[266] | 42 | // Class parameters. |
---|
| 43 | var $_params = array( |
---|
| 44 | 'error' => ' sc-msg-error ', |
---|
| 45 | 'warning' => ' sc-msg-warning ', |
---|
| 46 | 'notice' => ' sc-msg-notice ', |
---|
| 47 | 'success' => ' sc-msg-success ', |
---|
| 48 | ); |
---|
| 49 | |
---|
[100] | 50 | // Array filling with error messages. |
---|
[1] | 51 | var $errors = array(); |
---|
[96] | 52 | |
---|
[1] | 53 | /** |
---|
[266] | 54 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 55 | * |
---|
| 56 | * @access public |
---|
| 57 | * @param array $params Array of parameters (key => val pairs). |
---|
| 58 | */ |
---|
| 59 | function setParam($params) |
---|
| 60 | { |
---|
| 61 | $app =& App::getInstance(); |
---|
| 62 | |
---|
| 63 | if (isset($params) && is_array($params)) { |
---|
| 64 | // Merge new parameters with old overriding only those passed. |
---|
| 65 | $this->_params = array_merge($this->_params, $params); |
---|
| 66 | } else { |
---|
| 67 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * Return the value of a parameter, if it exists. |
---|
| 73 | * |
---|
| 74 | * @access public |
---|
| 75 | * @param string $param Which parameter to return. |
---|
| 76 | * @return mixed Configured parameter value. |
---|
| 77 | */ |
---|
| 78 | function getParam($param) |
---|
| 79 | { |
---|
| 80 | $app =& App::getInstance(); |
---|
| 81 | |
---|
| 82 | if (isset($this->_params[$param])) { |
---|
| 83 | return $this->_params[$param]; |
---|
| 84 | } else { |
---|
| 85 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 86 | return null; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
[1] | 91 | * Return the current list of errors. |
---|
| 92 | * |
---|
| 93 | * @return array an array of errors in the following arrangement: |
---|
| 94 | * keys: the name of the variable with an error |
---|
| 95 | * vals: the message to display for that error |
---|
| 96 | */ |
---|
| 97 | function getErrorList() |
---|
| 98 | { |
---|
| 99 | return $this->errors; |
---|
| 100 | } |
---|
[42] | 101 | |
---|
[1] | 102 | /** |
---|
| 103 | * Add an error to the errors stack. |
---|
| 104 | * |
---|
| 105 | * @param string $form_name The name of the incoming form variable. |
---|
| 106 | * @param string $msg The error message for that form. |
---|
| 107 | * @param int $type The type of message: MSG_NOTICE, |
---|
| 108 | * MSG_SUCCESS, MSG_WARNING, or MSG_ERR. |
---|
| 109 | * @param string $file __FILE__. |
---|
| 110 | * @param string $line __LINE__. |
---|
| 111 | */ |
---|
| 112 | function addError($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
| 113 | { |
---|
| 114 | $this->errors[] = array( |
---|
| 115 | 'name' => $form_name, |
---|
| 116 | 'message' => $msg, |
---|
| 117 | 'type' => $type, |
---|
| 118 | 'file' => $file, |
---|
| 119 | 'line' => $line |
---|
| 120 | ); |
---|
| 121 | } |
---|
[42] | 122 | |
---|
[1] | 123 | /** |
---|
| 124 | * Check whether any errors have been triggered. |
---|
| 125 | * |
---|
| 126 | * @param string $form_name the name of the incoming form variable |
---|
| 127 | * |
---|
[42] | 128 | * @return bool true if any errors were found, or if found for |
---|
[1] | 129 | * a variable of $form_name, false otherwise |
---|
| 130 | */ |
---|
| 131 | function anyErrors($form_name=null) |
---|
| 132 | { |
---|
| 133 | if (isset($form_name)) { |
---|
| 134 | foreach ($this->errors as $err) { |
---|
| 135 | if ($err['name'] == $form_name) { |
---|
[178] | 136 | return $err['type']; |
---|
[1] | 137 | } |
---|
| 138 | } |
---|
| 139 | return false; |
---|
[144] | 140 | } else { |
---|
| 141 | return (sizeof($this->errors) > 0); |
---|
[1] | 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * Reset the error list. |
---|
| 147 | */ |
---|
| 148 | function resetErrorList() |
---|
| 149 | { |
---|
| 150 | $this->errors = array(); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * Prints the HTML for displaying error messages. |
---|
| 155 | * |
---|
| 156 | * @access public |
---|
| 157 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 158 | * @since 15 Jul 2005 01:39:14 |
---|
| 159 | */ |
---|
| 160 | function printErrorMessages() |
---|
| 161 | { |
---|
[136] | 162 | $app =& App::getInstance(); |
---|
[1] | 163 | if ($this->anyErrors()) { |
---|
[166] | 164 | ?><div class="sc-msg" id="sc-msg-formvalidator"><?php |
---|
[333] | 165 | foreach ($this->getErrorList() as $e) { |
---|
[136] | 166 | if ('' != $e['message'] && is_string($e['message'])) { |
---|
| 167 | if (error_reporting() > 0 && $app->getParam('display_errors') && isset($e['file']) && isset($e['line'])) { |
---|
| 168 | echo "\n<!-- [" . $e['file'] . ' : ' . $e['line'] . '] -->'; |
---|
[1] | 169 | } |
---|
[136] | 170 | switch ($e['type']) { |
---|
[1] | 171 | case MSG_ERR: |
---|
[136] | 172 | echo '<div class="sc-msg-error">' . $e['message'] . '</div>'; |
---|
[1] | 173 | break; |
---|
[42] | 174 | |
---|
[1] | 175 | case MSG_WARNING: |
---|
[136] | 176 | echo '<div class="sc-msg-warning">' . $e['message'] . '</div>'; |
---|
[1] | 177 | break; |
---|
[42] | 178 | |
---|
[1] | 179 | case MSG_SUCCESS: |
---|
[136] | 180 | echo '<div class="sc-msg-success">' . $e['message'] . '</div>'; |
---|
[1] | 181 | break; |
---|
[42] | 182 | |
---|
[1] | 183 | case MSG_NOTICE: |
---|
| 184 | default: |
---|
[136] | 185 | echo '<div class="sc-msg-notice">' . $e['message'] . '</div>'; |
---|
[1] | 186 | break; |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | ?></div><?php |
---|
| 191 | } |
---|
| 192 | } |
---|
[42] | 193 | |
---|
[1] | 194 | /** |
---|
| 195 | * If this form has an error, print an error marker like "<<". |
---|
| 196 | * |
---|
| 197 | * @param string $form_name the name of the incoming form variable |
---|
| 198 | * @param string $marker A string to print if there is an error. if |
---|
| 199 | * not provided, use default. |
---|
| 200 | */ |
---|
| 201 | function err($form_name, $marker=null) |
---|
| 202 | { |
---|
[178] | 203 | if (false !== ($type = $this->anyErrors($form_name))) { |
---|
[1] | 204 | if (isset($marker)) { |
---|
| 205 | echo $marker; |
---|
| 206 | } else { |
---|
[178] | 207 | switch ($type) { |
---|
| 208 | case MSG_ERR: |
---|
| 209 | default: |
---|
[266] | 210 | echo $this->getParam('error'); |
---|
[178] | 211 | break; |
---|
| 212 | |
---|
| 213 | case MSG_WARNING: |
---|
[266] | 214 | echo $this->getParam('warning'); |
---|
[178] | 215 | break; |
---|
| 216 | |
---|
[266] | 217 | case MSG_NOTICE: |
---|
| 218 | echo $this->getParam('notice'); |
---|
[178] | 219 | break; |
---|
| 220 | |
---|
[266] | 221 | case MSG_SUCCESS: |
---|
| 222 | echo $this->getParam('success'); |
---|
[178] | 223 | break; |
---|
| 224 | } |
---|
[1] | 225 | } |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
[144] | 230 | * Ensure the length of string is non-zero. |
---|
[1] | 231 | * |
---|
| 232 | * @param string $form_name the name of the incoming form variable |
---|
| 233 | * @param string $msg the message to display on error |
---|
| 234 | * |
---|
| 235 | * @return bool true if form is not empty, false otherwise. |
---|
| 236 | */ |
---|
| 237 | function notEmpty($form_name, $msg='') |
---|
| 238 | { |
---|
[144] | 239 | if (parent::notEmpty(getFormData($form_name))) { |
---|
[1] | 240 | return true; |
---|
| 241 | } else { |
---|
[144] | 242 | $this->addError($form_name, $msg); |
---|
[1] | 243 | return false; |
---|
| 244 | } |
---|
| 245 | } |
---|
[144] | 246 | |
---|
| 247 | /* |
---|
| 248 | * We were using the isEmpty method *wrong* all these years and should have been using notEmpty. |
---|
| 249 | * But the fact is the only use is to ensure a value is not empty, so this function simply becomes |
---|
| 250 | * an alias of the one-true notEmpty() function. |
---|
| 251 | * @since 03 Jun 2006 22:56:46 |
---|
| 252 | */ |
---|
[1] | 253 | function isEmpty($form_name, $msg='') |
---|
| 254 | { |
---|
[241] | 255 | $this->notEmpty($form_name, $msg); |
---|
[1] | 256 | } |
---|
| 257 | |
---|
| 258 | /** |
---|
| 259 | * Check whether input is a string. |
---|
| 260 | * |
---|
| 261 | * @param string $form_name the name of the incoming form variable |
---|
| 262 | * @param string $msg the message to display on error |
---|
| 263 | * |
---|
| 264 | * @return bool true if form is a string, false otherwise. |
---|
| 265 | */ |
---|
| 266 | function isString($form_name, $msg='') |
---|
| 267 | { |
---|
[144] | 268 | if (parent::isString(getFormData($form_name))) { |
---|
| 269 | return true; |
---|
| 270 | } else { |
---|
[1] | 271 | $this->addError($form_name, $msg); |
---|
| 272 | return false; |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | /** |
---|
| 277 | * Check whether input is a number. Allows negative numbers. |
---|
| 278 | * |
---|
| 279 | * @param string $form_name the name of the incoming form variable |
---|
| 280 | * @param string $msg the message to display on error |
---|
| 281 | * |
---|
| 282 | * @return bool true if no errors found, false otherwise |
---|
| 283 | */ |
---|
| 284 | function isNumber($form_name, $msg='') |
---|
| 285 | { |
---|
[144] | 286 | if (parent::isNumber(getFormData($form_name))) { |
---|
| 287 | return true; |
---|
| 288 | } else { |
---|
[1] | 289 | $this->addError($form_name, $msg); |
---|
| 290 | return false; |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | /** |
---|
| 295 | * addError if input is NOT an integer. Don't just use is_int() because the |
---|
| 296 | * data coming from the user is *really* a string. |
---|
| 297 | * |
---|
| 298 | * @param string $form_name the name of the incoming form variable |
---|
| 299 | * @param string $msg the message to display on error |
---|
| 300 | * |
---|
| 301 | * @return bool true if value is an integer |
---|
| 302 | */ |
---|
| 303 | function isInteger($form_name, $msg='', $negative_ok=false) |
---|
| 304 | { |
---|
[144] | 305 | if (parent::isInteger(getFormData($form_name), $negative_ok)) { |
---|
| 306 | return true; |
---|
| 307 | } else { |
---|
[1] | 308 | $this->addError($form_name, $msg); |
---|
| 309 | return false; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | /** |
---|
| 314 | * Check whether input is a float. Don't just use is_float() because the |
---|
[42] | 315 | * data coming from the user is *really* a string. Integers will also |
---|
[1] | 316 | * pass this test. |
---|
| 317 | * |
---|
| 318 | * @param string $form_name the name of the incoming form variable |
---|
| 319 | * @param string $msg the message to display on error |
---|
| 320 | * |
---|
| 321 | * @return bool true if value is a float |
---|
| 322 | */ |
---|
| 323 | function isFloat($form_name, $msg='', $negative_ok=false) |
---|
| 324 | { |
---|
[144] | 325 | if (parent::isFloat(getFormData($form_name), $negative_ok)) { |
---|
| 326 | return true; |
---|
| 327 | } else { |
---|
[1] | 328 | $this->addError($form_name, $msg); |
---|
| 329 | return false; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | /** |
---|
| 334 | * Check whether input is an array. |
---|
| 335 | * |
---|
| 336 | * @param string $form_name the name of the incoming form variable |
---|
| 337 | * @param string $msg the message to display on error |
---|
| 338 | * |
---|
| 339 | * @return bool true if value is a float |
---|
| 340 | */ |
---|
| 341 | function isArray($form_name, $msg='') |
---|
| 342 | { |
---|
[144] | 343 | if (parent::isArray(getFormData($form_name))) { |
---|
| 344 | return true; |
---|
| 345 | } else { |
---|
[1] | 346 | $this->addError($form_name, $msg); |
---|
| 347 | return false; |
---|
| 348 | } |
---|
| 349 | } |
---|
[42] | 350 | |
---|
[1] | 351 | /** |
---|
| 352 | * Check whether input matches the specified perl regular expression |
---|
[42] | 353 | * pattern. |
---|
[1] | 354 | * |
---|
| 355 | * @param string $form_name the name of the incoming form variable |
---|
| 356 | * @param int $regex perl regex that the string must match |
---|
[144] | 357 | * @param bool $valid_on_match set to false to be valid if match, or true |
---|
[1] | 358 | * to be valid on no match |
---|
| 359 | * @param string $msg the message to display on error |
---|
| 360 | * |
---|
| 361 | * @return bool true if value passes regex test |
---|
| 362 | */ |
---|
[144] | 363 | function checkRegex($form_name, $regex, $valid_on_match, $msg='') |
---|
[1] | 364 | { |
---|
[144] | 365 | if (parent::checkRegex(getFormData($form_name), $regex, $valid_on_match)) { |
---|
| 366 | return true; |
---|
[1] | 367 | } else { |
---|
[144] | 368 | $this->addError($form_name, $msg); |
---|
| 369 | return false; |
---|
[1] | 370 | } |
---|
| 371 | } |
---|
[42] | 372 | |
---|
[1] | 373 | /** |
---|
| 374 | * Tests if the string length is between specified values. Whitespace excluded for min. |
---|
| 375 | * |
---|
| 376 | * @param string $form_name the name of the incoming form variable |
---|
| 377 | * @param int $min minimum length of string, inclusive |
---|
| 378 | * @param int $max maximum length of string, inclusive |
---|
| 379 | * @param string $msg the message to display on error |
---|
| 380 | * |
---|
| 381 | * @return bool true if string length is within given boundaries |
---|
| 382 | */ |
---|
| 383 | function stringLength($form_name, $min, $max, $msg='') |
---|
| 384 | { |
---|
[144] | 385 | if (parent::stringLength(getFormData($form_name), $min, $max)) { |
---|
| 386 | return true; |
---|
| 387 | } else { |
---|
[1] | 388 | $this->addError($form_name, $msg); |
---|
| 389 | return false; |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | /** |
---|
| 394 | * Check whether input is within a valid numeric range. |
---|
| 395 | * |
---|
| 396 | * @param string $form_name the name of the incoming form variable |
---|
| 397 | * @param int $min minimum value of number, inclusive |
---|
| 398 | * @param int $max maximum value of number, inclusive |
---|
| 399 | * @param string $msg the message to display on error |
---|
| 400 | * |
---|
| 401 | * @return bool true if no errors found, false otherwise |
---|
| 402 | */ |
---|
| 403 | function numericRange($form_name, $min, $max, $msg='') |
---|
| 404 | { |
---|
[144] | 405 | if (parent::numericRange(getFormData($form_name), $min, $max)) { |
---|
[1] | 406 | return true; |
---|
| 407 | } else { |
---|
[144] | 408 | $this->addError($form_name, $msg); |
---|
[1] | 409 | return false; |
---|
| 410 | } |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | /** |
---|
[23] | 414 | * Validates an email address based on the recommendations in RFC 3696. |
---|
[42] | 415 | * Is more loose than restrictive, to allow the many valid variants of |
---|
[23] | 416 | * email addresses while catching the most common mistakes. |
---|
| 417 | * http://www.faqs.org/rfcs/rfc822.html |
---|
| 418 | * http://www.faqs.org/rfcs/rfc2822.html |
---|
| 419 | * http://www.faqs.org/rfcs/rfc3696.html |
---|
| 420 | * http://www.faqs.org/rfcs/rfc1035.html |
---|
[1] | 421 | * |
---|
[23] | 422 | * @access public |
---|
| 423 | * @param string $form_name The name of the incoming form variable. |
---|
| 424 | * @return bool Validity of address. |
---|
| 425 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[1] | 426 | */ |
---|
[23] | 427 | function validateEmail($form_name) |
---|
[1] | 428 | { |
---|
[136] | 429 | $app =& App::getInstance(); |
---|
[144] | 430 | |
---|
[1] | 431 | $email = getFormData($form_name); |
---|
[144] | 432 | |
---|
[1] | 433 | if ('' == trim($email)) { |
---|
[305] | 434 | // No email address provided, and that's okay. |
---|
[144] | 435 | return true; |
---|
[1] | 436 | } |
---|
[23] | 437 | |
---|
[144] | 438 | // Validator::validateEmail() returns a value that relates to the VALIDATE_EMAIL_* constants (defined in Validator.inc.php). |
---|
| 439 | switch (parent::validateEmail($email)) { |
---|
| 440 | case VALIDATE_EMAIL_REGEX_FAIL: |
---|
| 441 | // Failed regex match. |
---|
[305] | 442 | $this->addError($form_name, sprintf(_("The email address <em>%s</em> is formatted incorrectly."), oTxt($email))); |
---|
| 443 | $app->logMsg(sprintf('The email address %s is not valid.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[23] | 444 | return false; |
---|
[144] | 445 | break; |
---|
[305] | 446 | |
---|
[144] | 447 | case VALIDATE_EMAIL_LENGTH_FAIL : |
---|
| 448 | // Failed length requirements. |
---|
[305] | 449 | $this->addError($form_name, sprintf(_("The email address <em>%s</em> is too long (email addresses must have fewer than 256 characters)."), oTxt($email))); |
---|
| 450 | $app->logMsg(sprintf('The email address %s must contain less than 256 characters.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 451 | return false; |
---|
[144] | 452 | break; |
---|
[305] | 453 | |
---|
[144] | 454 | case VALIDATE_EMAIL_MX_FAIL : |
---|
| 455 | // Failed MX record test. |
---|
[314] | 456 | $this->addError($form_name, sprintf(_("The email address <em>%s</em> does not have a valid domain name"), oTxt($email))); |
---|
[305] | 457 | $app->logMsg(sprintf('The email address %s does not have a valid domain name.', oTxt($email)), LOG_INFO, __FILE__, __LINE__); |
---|
[23] | 458 | return false; |
---|
[144] | 459 | break; |
---|
[305] | 460 | |
---|
[144] | 461 | case VALIDATE_EMAIL_SUCCESS : |
---|
| 462 | default : |
---|
| 463 | return true; |
---|
| 464 | break; |
---|
[1] | 465 | } |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | /** |
---|
| 469 | * Check whether input is a valid phone number. Notice: it is now set |
---|
| 470 | * to allow characters like - or () or + so people can type in a phone |
---|
[144] | 471 | * number that looks like: +1 (530) 555-1212 |
---|
[1] | 472 | * |
---|
| 473 | * @param string $form_name the name of the incoming form variable |
---|
| 474 | * |
---|
| 475 | * @return bool true if no errors found, false otherwise |
---|
| 476 | */ |
---|
| 477 | function validatePhone($form_name) |
---|
| 478 | { |
---|
| 479 | $phone = getFormData($form_name); |
---|
[42] | 480 | |
---|
[144] | 481 | return ( |
---|
| 482 | $this->checkRegex($form_name, '/^[0-9 +().-]*$/', true, sprintf(_("The phone number <em>%s</em> is not valid."), $phone)) |
---|
| 483 | && $this->stringLength($form_name, 0, 25, sprintf(_("The phone number <em>%s</em> is too long"), $phone)) |
---|
| 484 | ); |
---|
[1] | 485 | } |
---|
| 486 | |
---|
| 487 | /** |
---|
| 488 | * Verifies that date can be processed by the strtotime function. |
---|
| 489 | * |
---|
| 490 | * @param string $form_name the name of the incoming form variable |
---|
| 491 | * @param string $msg the message to display on error |
---|
| 492 | * |
---|
| 493 | * @return bool true if no errors found, false otherwise |
---|
| 494 | */ |
---|
| 495 | function validateStrDate($form_name, $msg='') |
---|
| 496 | { |
---|
[144] | 497 | $app =& App::getInstance(); |
---|
| 498 | |
---|
| 499 | if (parent::validateStrDate(getFormData($form_name))) { |
---|
| 500 | return true; |
---|
| 501 | } else { |
---|
[1] | 502 | $this->addError($form_name, $msg); |
---|
[136] | 503 | $app->logMsg(sprintf('The string date %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 504 | return false; |
---|
| 505 | } |
---|
| 506 | } |
---|
[42] | 507 | |
---|
| 508 | |
---|
[1] | 509 | /** |
---|
[279] | 510 | * Verifies credit card number using the Luhn (mod 10) algorithm. |
---|
| 511 | * http://en.wikipedia.org/wiki/Luhn_algorithm |
---|
[1] | 512 | * |
---|
| 513 | * @param string $form_name The name of the incoming form variable. |
---|
[144] | 514 | * @param string $cc_type Optional, card type to do specific checks. One of the CC_TYPE_* constants. |
---|
[1] | 515 | * |
---|
| 516 | * @return bool true if no errors found, false otherwise |
---|
| 517 | */ |
---|
[144] | 518 | function validateCCNumber($form_name, $cc_type=null) |
---|
[1] | 519 | { |
---|
[144] | 520 | $cc_num = getFormData($form_name); |
---|
| 521 | |
---|
| 522 | if (parent::validateCCNumber($cc_num, $cc_type)) { |
---|
[1] | 523 | return true; |
---|
| 524 | } else { |
---|
[282] | 525 | $this->addError($form_name, sprintf(_("The credit card number you entered is not valid. Please check the number and try again."), $cc_num)); |
---|
[1] | 526 | return false; |
---|
| 527 | } |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | /** |
---|
[136] | 531 | * Check whether a file was selected for uploading. If file is missing, it's an error. |
---|
[1] | 532 | * |
---|
| 533 | * @param string $form_name the name of the incoming form variable |
---|
| 534 | * @param string $msg the message to display on error |
---|
| 535 | * |
---|
| 536 | * @return bool true if no errors found, false otherwise |
---|
| 537 | */ |
---|
[144] | 538 | function fileUploaded($form_name, $msg='') |
---|
[1] | 539 | { |
---|
[144] | 540 | if (parent::fileUploaded($form_name)) { |
---|
| 541 | return true; |
---|
| 542 | } else { |
---|
[1] | 543 | $this->addError($form_name, $msg); |
---|
| 544 | return false; |
---|
| 545 | } |
---|
| 546 | } |
---|
[42] | 547 | |
---|
[1] | 548 | } // THE END |
---|
| 549 | |
---|
[314] | 550 | ?> |
---|