Yeamin Chowdhury
Yeamin Chowdhury

Reputation: 512

Laravel custom validator

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?

Upvotes: 0

Views: 74

Answers (1)

Rwd
Rwd

Reputation: 35180

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.

Upvotes: 1

Related Questions