Reputation: 557
For testing and support issues I´d like to allow one or two emails to be used multiple times in my users database. All other emails should be unique. For example:
"email" => "required|email|unique:users,email,[email protected]"
This isn´t working and I can´t use the id field for the exception because there will be multiple entries with the same email.
Upvotes: 4
Views: 1245
Reputation: 152890
Sounds like a case for sometimes
$whitelist = ['[email protected]', '[email protected]', '[email protected]'];
$validator->sometimes('email', 'unique:users', function($input) use ($whitelist){
return ! in_array($input->email, $whitelist);
});
Meaning if the email is not in the whitelist, the unique
rule applies.
Inside a form request you can add stuff to the validator instance by overriding getValidatorInstance()
:
protected getValidatorInstance(){
$validator = parent::getValidatorInstance();
$validator->sometimes(...);
return $validator;
}
Upvotes: 3
Reputation: 8663
Try below where XX is your test user id.
"email" => "required|email|unique:users,email,XX,id"
PS You can also set it dynamically during runtime if you want to
Upvotes: 0