PHP表单验证返回奇怪的值

时间:2012-12-22 09:02:16

标签: php

我最近为自己制作了一个非常简单的MVC'框架。现在我试图用一些类扩展该框架,所以我可以节省一些时间。我现在要尝试的类是表单验证类。我认为它工作得很好,直到我做了一个功能来检查一个字符串的最小长度。

班级:

<?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
 **/
?>

我的控制器文件:

<?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();
        }
    }
}
?>

在我看来,我有一个包含2个字段的表单,test和bla。当我提交表格为空时,checkMinLength()函数返回2个值:

测试必须包含至少1个字符 测试必须包含至少5个字符

首先,它从哪里得到1,为什么它显示2个相同功能的消息?我只是找不到原因。

希望你们能帮助我! (控制器中的回声仅用于演示)

谢谢!

1 个答案:

答案 0 :(得分:2)

您忘记了break中的switch,因此您从letters_numbers案件陷入min_length案件。

letters_numbers规则的“规则值”为true,后者将字符串转换为1