PHP表单验证返回奇怪的值


PHP Form validation returning strange value

我最近为自己做了一个非常简单的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个字段,测试和bla的形式。当我提交表单时,它是空的,checkMinLength()函数返回2个值:

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

首先,它从哪里得到那1,第二,为什么它显示2个消息为相同的功能?我就是不知道为什么。

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

谢谢!

您在switch中忘记了一个break,因此您从letters_numbers情况下降到min_length情况。

您的letters_numbers规则的"规则值"是true,它将转换为字符串作为1