I am creating a validation class in PHP. I want to validate two items in one array, and for some reason it only validates the first item. I call it like this:
Form::validate(array('user' => 'required', 'pass' => 'required'), 'login');
and the function is
public static function validate($rules, $form)
{
foreach ($rules as $rule => $val) {
if ($val === 'required') {
if (empty($_POST[$rule])) {
if (isset($_POST[$form])) {
self::$_error = Error::set('All fields are required. ' . $rule);
echo self::$_error;
return false;
}
} else {
return true;
}
}
}
}
My question is how I could validate both items in the one array?
Move return true
to the end so that it will run only if everything was valid.
foreach (...) {
if (...) {
return false;
}
}
return true;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.