I'm trying to write a custom validation rule in Laravel to ensure that every email in a comma separated list is not on a suppression list. I already have a different rule that verifies if every email in the comma separated list is a valid email, so I don't check that here.
The code above works fine, but I have two questions:
- Can I do this better?
- How can I modify the error message to include the email address that's not correct? I want it to say something like
"The email address [email protected] is on the suppression list and will not be used for sending".
public function passes($attribute, $value)
{
$emails = explode(',', $value);
foreach ($emails as $email) {
$onSuppressionList = $this->suppressionListManager->find($email);
if ($onSuppressionList) {
return false;
}
}
return true;
}
public function message()
{
return 'The validation error message.';
}
SuppressionListManageris an utility class. It's not an Eloquent model. That part is fine, no need to change it.