简体   繁体   English

验证码出错

[英]Error with the Validation Code

I have the following validate method in my Validation Class: 我的验证类中有以下验证方法:

public function validate($data, $rules)
{
    if(!is_array($rules))
    {
        $rules = array($rules);
    }
    foreach($rules as $rule)
    {
        if(is_array($rule))
        {
            if(!$this->{$rule[0]}($data, $rule[1]))
            {
                array_push($this->errors, $rule[0] . ' is ' . $rule[1]);
                $ruleValue = $rule[0];
                print_r($this->errorMessages[$ruleValue] . BR);
                return $this->errors;
            }
        }
        else
        {
            if(!$this->{$rule}($data))
            {
                array_push($this->errors, $rule);
                print_r($this->errorMessages[$rule] . BR);
                return $this->errors;
            }
        }       
    }
    if(empty($this->errors))
    {
        return TRUE;
    }
}

with a method that checks for valid date (ie less than the present date) 使用检查有效日期的方法(即小于当前日期)

private function validDate($data)
{
    $now = date('Ymd');
    return ($data < $now) ? TRUE : FALSE;
}

I have another php file which creates this Validation class and then checks for input and validates it: 我有另一个php文件创建此Validation类,然后检查输入并验证它:

if
            (
                $Validation->validate($email, array('required', 'isEmail')) &&
                $Validation->validate($password, array('required', array('min_length', '6'), array('max_length', '20'))) &&
                $Validation->validate($repeatPassword, array('required', array('min_length', '6'), array('max_length', '20'), array('matches', Input::fetch('password')))) &&
                $Validation->validate($date, array('required', 'validDate'))
            )
{
   echo 'Yes';
}

Now, ideally, the echo code should be executed when all validations are true and it worked fine before I made changes to the valid Date method in the Validation class. 现在,理想情况下,当所有验证都为真时,应该执行echo代码,并且在我更改Validation类中的有效Date方法之前它已正常工作。

It prints the error which means it returns false but the message 'Yes' is being echoed as well. 它打印错误,这意味着它返回false,但也回显了消息“是”。

I have used && in the if statement which means if any one condition is false, the code wouldn't be executed but I can't seem to understand why it is running when all conditions return false. 我在if语句中使用了&& ,这意味着如果任何一个条件为false,代码将不会被执行但我似乎无法理解为什么它在所有条件都返回false时运行。

You are returning 你回来了

return $this->errors;

in your validate function. 在您的验证功能中。 But it's definitelly not false or 0, so it is true and it pass through your condition. 但它的定义不是或0,所以它是真的 ,它通过你的条件。 You shoud return false 你应该回归虚假

The method validate always returns something different to false, so the if expression is always true. 方法validate总是返回不同的false,因此if表达式始终为true。 This way, you get the error, and also the "Yes" output. 这样,您将获得错误,并且还会显示“是”输出。 I suggest you to make the method to return true or false, according to the validation, and set the errors as a public attribute, so you can read them in case of a false validation. 我建议你根据验证使方法返回true或false,并将错误设置为公共属性,这样你就可以在错误验证的情况下读取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM