Ignore:
Timestamp:
Jun 3, 2006 7:47:48 PM (18 years ago)
Author:
scdev
Message:

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/FormValidator.inc.php

    r121 r136  
    44 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information
    55 *
    6  * The FormValidator:: class provides a method for validating input from
     6 * The FormValidator class provides a method for validating input from
    77 * http requests and displaying errors.
    88 *
     
    1010 * @version   1.8
    1111 *
    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.
     15require_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")));
     26if (is_null($var)) {
     27    $fv->addError('field_name', sprintf(_("%s is invalid."), _("Field name")));
     28}
     29if ($fv->anyErrors()) {
     30    // Errors!
     31}
     32---------------------------------------------------------------------
    2433 */
    25 class FormValidator
    26 {
     34class FormValidator {
    2735
    2836    // Array filling with error messages.
     
    103111    function printErrorMessages()
    104112    {
     113        $app =& App::getInstance();
    105114        if ($this->anyErrors()) {
    106115            ?><div class="sc-msg"><?php
    107116            $errors = $this->getErrorList();
    108             foreach ($errors as $err) {
    109                 if (!empty($err['message']) && is_string($err['message'])) {
    110                     if (error_reporting() > 0 && isset($err['file']) && isset($err['line'])) {
    111                         echo "\n<!-- [" . $err['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'] . '] -->';
    112121                    }
    113                     switch ($err['type']) {
     122                    switch ($e['type']) {
    114123                    case MSG_ERR:
    115                         echo '<div class="sc-msg-error">' . $err['message'] . '</div>';
     124                        echo '<div class="sc-msg-error">' . $e['message'] . '</div>';
    116125                        break;
    117126
    118127                    case MSG_WARNING:
    119                         echo '<div class="sc-msg-warning">' . $err['message'] . '</div>';
     128                        echo '<div class="sc-msg-warning">' . $e['message'] . '</div>';
    120129                        break;
    121130
    122131                    case MSG_SUCCESS:
    123                         echo '<div class="sc-msg-success">' . $err['message'] . '</div>';
     132                        echo '<div class="sc-msg-success">' . $e['message'] . '</div>';
    124133                        break;
    125134
    126135                    case MSG_NOTICE:
    127136                    default:
    128                         echo '<div class="sc-msg-notice">' . $err['message'] . '</div>';
     137                        echo '<div class="sc-msg-notice">' . $e['message'] . '</div>';
    129138                        break;
    130139                    }
     
    389398    function validateEmail($form_name)
    390399    {
     400        $app =& App::getInstance();
     401   
    391402        $email = getFormData($form_name);
    392403        if ('' == trim($email)) {
     
    400411        if (!preg_match($e->getParam('regex'), $email, $e_parts)) {
    401412            $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__);
    403414            return false;
    404415        }
     
    412423        if (strlen($local) > 64 || strlen($domain) > 191) {
    413424            $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__);
    415426            return false;
    416427        }
     
    420431        if ((ip2long($domain) == '-1' || ip2long($domain) === false) && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) {
    421432            $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__);
    423434            return false;
    424435        }
     
    454465    function validateStrDate($form_name, $msg='')
    455466    {
     467        $app =& App::getInstance();
     468   
    456469        if (($timestamp = strtotime(getFormData($form_name, '0'))) === -1) {
    457470            $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__);
    459472            return false;
    460473        } else {
     
    530543        for ($i=0; $i<$num; $i++) {
    531544            // Get each digit.
    532             $digit = substr($cc_num,$i,1);
     545            $digit = substr($cc_num, $i, 1);
    533546
    534547            //  If it's an odd digit, double it.
     
    539552            //  If the result is two digits, add them.
    540553            if (strlen($digit) == 2) {
    541                 $digit = substr($digit,0,1) + substr($digit,1,1);
     554                $digit = substr($digit, 0, 1) + substr($digit, 1, 1);
    542555            }
    543556
     
    556569
    557570    /**
    558      * Check whether uploaded file is valid.
     571     * Check whether a file was selected for uploading. If file is missing, it's an error.
    559572     *
    560573     * @param  string $form_name the name of the incoming form variable
     
    565578    function validateFile($form_name, $msg='')
    566579    {
    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'])) {
    568581            $this->addError($form_name, $msg);
    569582            return false;
Note: See TracChangeset for help on using the changeset viewer.