验证码错误
Error with the Validation Code
我的验证中有以下验证方法 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;
}
}
使用检查有效日期(即小于当前日期)的方法
private function validDate($data)
{
$now = date('Ymd');
return ($data < $now) ? TRUE : FALSE;
}
我有另一个 php 文件创建此验证 class 然后检查输入并验证它:
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';
}
现在,理想情况下,回显代码应该在所有验证都为真时执行,并且在我更改验证 class 中的有效日期方法之前它工作正常。
它打印出错误,这意味着它 return 是错误的,但是消息 'Yes' 也被回显了。
我在 if
语句中使用了 &&
这意味着如果任何一个条件为假,代码将不会执行,但我似乎无法理解为什么它是 运行 当所有条件 return false.
您正在 return
return $this->errors;
在您的 validate 函数中。但它绝对不是 false 或 0,所以它是 true 并且它通过了你的条件。你应该 return false
方法 validate
始终 return 与 false 不同,因此 if
表达式始终为 true。这样,您会得到错误以及 "Yes" 输出。
我建议您根据验证将方法设置为 return true 或 false,并将错误设置为 public 属性,以便在验证错误的情况下阅读它们。
我的验证中有以下验证方法 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;
}
}
使用检查有效日期(即小于当前日期)的方法
private function validDate($data)
{
$now = date('Ymd');
return ($data < $now) ? TRUE : FALSE;
}
我有另一个 php 文件创建此验证 class 然后检查输入并验证它:
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';
}
现在,理想情况下,回显代码应该在所有验证都为真时执行,并且在我更改验证 class 中的有效日期方法之前它工作正常。
它打印出错误,这意味着它 return 是错误的,但是消息 'Yes' 也被回显了。
我在 if
语句中使用了 &&
这意味着如果任何一个条件为假,代码将不会执行,但我似乎无法理解为什么它是 运行 当所有条件 return false.
您正在 return
return $this->errors;
在您的 validate 函数中。但它绝对不是 false 或 0,所以它是 true 并且它通过了你的条件。你应该 return false
方法 validate
始终 return 与 false 不同,因此 if
表达式始终为 true。这样,您会得到错误以及 "Yes" 输出。
我建议您根据验证将方法设置为 return true 或 false,并将错误设置为 public 属性,以便在验证错误的情况下阅读它们。