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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘