Ad

Laravel Custom Validator

- 1 answer

I have an form where user will post only an email address. I want to validate this input like:

if that email exists in the table, then a specific column's (e.g. is_valid) data must be null.

if the email doesn't exists then should be validated

I've got solution for the first condition, but can't find anything to combine both.

For first condition my solution:

Rule::exists('table_name')->where(function ($query) {
    $query->where('is_valid', null);
});

How to write a rule for both conditions?

Ad

Answer

For this you could use something like the unique rule:

Rule::unique('table_name', 'email')->whereNotNull('is_valid');

The above should satisfy both conditions you have i.e.

The email doesn't exist at all or it doesn't exist when is_valid isn't null.

Ad
source: stackoverflow.com
Ad