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.

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

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?