I have an array with two relevant keys where at least one of both shall contain a valid email address.
$data = [
'mail' => 'firstname.lastname#tld.com',
'mail2' => 'firstname.lastname@tld.com',
...
]
I've tried a validation using the exclude_with method, which works if the mail field is invalid, but mail2 is valid. However, it doesn't vice versa.
$validated = Validator::make($data, [
'mail' => 'exclude_with:mail|email',
'mail2' => 'exclude_with:mail2|email',
])->validate();
I could do this easily with other PHP methods or regular expressions, but I wonder if this is archivable with Laravel's validator.
The goal is to get at least one field with a valid email or fail.
Update
Based on Abdulla's promising answer, I found that even if the first email is valid but the second is not, the validation fails:
$data = [
'mail' => 'firstname.lastname@tld.com', // correct
'mail2' => 'firstname.lastname#tld.com' // wrong
];
$validator = Validator::make($data, [
'mail' => 'exclude_unless:mail2,null|email',
'mail2' => 'exclude_unless:mail,null|email',
]);
Output:
The mail2 must be a valid email address.

