0

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?

1
  • 2
    Return will do just that, return, exiting the loop Commented Apr 15, 2014 at 15:52

1 Answer 1

3

Move return true to the end so that it will run only if everything was valid.

foreach (...) { if (...) { return false; } } return true; 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that worked. Will accept your answer in 10 minutes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.