0

In Laravel 8 I have this validation in RegisterRequest:

public function rules()
{
    return [
        'name' =>'required|max:255',
        'email' =>'required|email|max:255|unique:users,email',
        'mobile' =>'required|digits:11|unique:users,mobile',
        'password' =>'required|confirmed|min:8|max:255'
    ];
}

The problem is that maybe the user entered the wrong email or maybe they entered someone else's email. I want to check if email exists only when the email is verified. How to do that?

  • Remove `|unique:users,email` ? However, if you don't check it beforehand, then you will need to add that user (with the duplicate email) to your database, right? Then you'll have duplicates, at least until their verification. – M. Eriksson Jan 26 '22 at 16:21
  • https://stackoverflow.com/questions/1976712/using-mx-records-to-validate-email-addresses –  Jan 26 '22 at 16:22
  • Your requirement doesn't make sense. If someone is entering an email address, presumably you are going to save it to the table. So why would you not want to check for uniqueness? – miken32 Jan 26 '22 at 18:54

2 Answers2

1

You could use

Rule::unique((new User)->getTable(), 'email')->whereNotNull('email_verified_at')

instead of unique:users,email.

(new User)->getTable() will return the User model's table. Using Rule:: you are able to query the database and therefor you are able to check if email_verified_at is empty or not. Here you can read more about using Rule::unique.

SuperDJ
  • 7,488
  • 11
  • 40
  • 74
0
Rule::unique('users','email')->where('email_verified_at')