doudaiyao0934 2012-12-22 09:02
浏览 55
已采纳

PHP表单验证返回奇怪的值

I recently made a very simple MVC'ish framework for my own. Now im trying to expand that framework with some classes, so i can save some time in the future. The class i am trying to make now is a form validation class. I thought it was working quite fine, untill i made a function to check for the minimum length of a string.

The class :

<?php
/**
 * Form Validation Class
 */
class formValidation
{
    private $errorMessages = array();
    private $method;
    public $errorString;
    public $validationRules = array();

    public function setMethod($method)
    {
        $this->method = $method;
    }
    public function setRules($rules)
    {
        $this->validationRules = $rules;
    }
    public function run()
    {
        if (!empty($this->method)) {
            foreach ($this->method as $fieldname => $fieldvalue) {
                foreach ($this->validationRules as $rule) {
                    if ($rule['field'] == $fieldname) {
                        foreach ($rule as $option => $rulevalue) {
                            switch ($option) {
                                case 'required':
                                    if (!$this->checkRequired($fieldvalue)) {
                                        $this->errorMessages[] = $rule['name'] . " is a required field";
                                    }
                                    break;
                                case 'letters_numbers':
                                    if (!$this->checkLettersNumbers($fieldvalue)) {
                                        $this->errorMessages[] = $rule['name'] . " may only contain letters and numbers";
                                    }
                                case 'min_length':
                                    if (!$this->checkMinLength($fieldvalue, $rulevalue)) {
                                        $this->errorMessages[] = $rule['name'] . " must contain at least $rulevalue characters";
                                    }
                            }
                        }
                    }
                }
            }
            if (!empty($this->errorMessages[0])) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
    // Checks
    private function checkRequired($value) // Makes a field mandatory
    {
        if ($value == "") {
            return false;
        } else {
            return true;
        }
    }
    private function checkLettersNumbers($value) // Allow Letters and Numbers only
    {
        return preg_match('/^[A-Za-z\s ]+$/', $value);
    }
    private function checkMinLength($value, $minlength) // Check input for minimum length
    {
        if (strlen($value) < $minlength) {
            return false;
        } else {
            return true;
        }
    }
    // Error Handling Functions
    private function makeErrorString($prelimiter = '<li>', $delimiter = '</li>') // Build all the errors into a string
    {
        if (!empty($this->errorMessages[0])) {
            $errors = "";
            foreach ($this->errorMessages as $message) {
                $errors .= $prelimiter;
                $errors .= $message;
                $errors .= $delimiter;
            }
            $this->errorString = $errors;
            return true;
        }
    }
    public function ValidationErrors() // Return the error string
    {
        if ($this->makeErrorString()) {
            return $this->errorString;
        }
    }
    /**
     * End of Class
     **/
}
/**
 * End of formValidation.php
 **/
?>

And my controller file :

<?php
/**
 * Homepage Controller
 * @copyright 2012
 */
class home extends SimpleController
{
    public function index()
    {
        $view = new view('home');
        $val  = new formValidation();
        $val->setMethod($_POST);
        $rules = array(
                        array(
                            'field' => 'test',
                            'name' => 'Test',
                            'required' => true,
                            'letters_numbers' => true,
                            'min_length' => '5'
                        ),
                        array(
                            'field' => 'bla',
                            'name' => 'Trololol',
                            'required' => true
                        )
                     );

        $val->setRules($rules);
        if (!$val->run()) {
            echo $val->ValidationErrors();
        }
    }
}
?>

In my view i have a form with 2 fields, test and bla. When i submit the form when it's empty, the checkMinLength() functions returns 2 value's :

Test must contain at least 1 characters Test must contain at least 5 characters

First of all, where is it getting that 1 from, and 2nd why is it showing 2 messages for the same function ? I just can't find out why.

Hope you guys can help me out! (The echo in the controller is just for demo)

Thanks!

  • 写回答

1条回答 默认 最新

  • doufang8282 2012-12-22 09:07
    关注

    You forgot a break in your switch, so you're falling through from the letters_numbers case into the min_length case.

    The "rule value" for your letters_numbers rule is true, which converts to a string as 1.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?