0

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?

2
  • I've edited my question, please check now. Commented Mar 8, 2020 at 8:22
  • @Rwd 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); }) Commented Mar 8, 2020 at 8:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

not working, your condition in where clause is not correct
I've used whereNotNull instead of where, now working.
@MdYeamin Sorry, you're right, a little oversight on my part. I've updated my answer :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.