invalid($data, $rules, $messages = array())
Runs a number of validators on a set of data and checks if the data is invalid
-
$data (array)
-
$rules (array)
Validation rules per data field -
$messages (array)
Error messages per data field -
return (array)
Example
$data = array(
'username' => 'Homer',
'fname' => 'Homer',
'lname' => 'Simpson',
'email' => 'home@simpsonscom'
);
$rules = array(
'username' => array('required'),
'fname' => array('required', 'max' => 2),
'lname' => array('required', 'min' => 20),
'email' => array('required', 'email')
);
$messages = array(
'fname' => 'Please enter a valid first name',
'lname' => 'Please enter a valid last name',
'email' => 'Please enter a valid email address'
);
if($invalid = invalid($data, $rules, $messages)) {
dump($invalid);
} else {
dump('Nice data!');
}
2.5.0 +
You can also separately define a message for each validation rule:
$messages = array(
'fname' => 'Please enter a valid first name',
'lname' => 'Please enter a valid last name',
'email' => [
'Please enter an email address',
'The email address is invalid'
]
);