Forcing A Unique Rule To Ignore A Given ID in Form Request

Published 4 months ago by phpMick

Can someone please remind me how to do this in L5.5?

I am editing a user, the email field is unique.

Like this:

Validator::make($data, [
    'email' => [
        'required',
        Rule::unique('users')->ignore($user->id),
    ],
]);

but in a form request.

This is what I have:

 return [
                'name'=>'required',
                'email'=>'required|email|unique:users,email'
}

It looks like I need the $user->id but I don't have the $user in my form request.

Pretty sure I have done this before.

Mick

Best Answer (As Selected By phpMick)
tykus

Are you route-model binding the User you are updating; you can get the bound User instance in the FormRequest using $this->user (assuming that is the route wildcard, and action parameter)

spodlogar

If you have an authenticated user you can get the user from the request.

phpMick

It's not the authenticated user I need, it is the one I am currently editing.

spodlogar

Can you post the rest of the update method so I can get a little more context?

tykus
tykus
4 months ago (575,140 XP)

Are you route-model binding the User you are updating; you can get the bound User instance in the FormRequest using $this->user (assuming that is the route wildcard, and action parameter)

phpMick

Yes I am binding, yes that works!

phpMick

How do I use that in my array?:

return [
                'name'=>'required',
                'email'=>'required|email|unique:users,email'
}

I need to get this in somehow?:

Rule::unique('users')->ignore($user->id),

Mick

spodlogar

Convert it to the array syntax.

return [
    'name' => ['required'],
    'email' => ['required', 'email', Rule::unique('users')->ignore($user->id)]
]
tykus
tykus
4 months ago (575,140 XP)

In your rules array? Change to an array style ruleset:

return [
    'name'=>'required',
    'email'=>['required', 'email', Rule::unique('users')->ignore($this->user->id)]
]

EDIT @spodlogar there first :)

phpMick

The only problem is that it fails for creating new users.

Trying to get property of non-object

Because $user is not set.

I guess the only way round this is to check if there is a $user and choose to include the rule or not?

I don't think I had to this last time I did this?

tykus
tykus
4 months ago (575,140 XP)

Are you using the same FormRequest object for the create and update scenarios? If you are then you would need to modify the rules on the fly depending on the User being present on the request or not:

$rules = [
    'name'=>'required',
    'email'=>['required', 'email', Rule::unique('users')->ignore($this->user->id)]
]

if (! $this->user) {
    $rules['email'] => ['required', 'email']
}

return $rules;
spodlogar

If you have a user to edit you should have a User model to get the id from

phpMick

Yes, I use the same form request for both store new and update existing.

spodlogar

@tykus has a pretty good solution. Does that work for you?

phpMick

Yes, this is pretty much what I did, thanks guys.

Please sign in or create an account to participate in this conversation.