#Trying to update email with unique user email.

30 messages · Page 1 of 1 (latest)

hollow flax
#

I am having a hard time trying to figure out why I keep getting 422 error code when trying to update an existing email address.
Can anyone explain to me what i am doing wrong in my update code and get me back on the right path. Thank you so much!

brazen narwhal
hollow flax
#

public function UpdateEmail(Request $request, User $user)
{
$validator=Validator::make($request->all(),[
'oldEmail' =>'required',
'email' => ['required', 'email', Rule::unique('users', 'email')->ignore($user)],
]);
if ($validator->fails()) {
return response()->json([
'message'=>'validations fails',
'errors' =>$validator->errors()
], 422);
}
$user=$request->user();

     $user->update([
        'email' => $request->email
     ]);
    
     return response()->json([
        'message'=>' email successfully updated',
        'errors' =>$validator->errors()
     ], 200);
  }
brazen narwhal
#
$validator = Validator::make($request->all(),[
    'oldEmail' =>'required',
    'email' => ['required', 'email', Rule::unique('users')->ignore($user)],
]);
#

You need to pass the user's ID to the ignore

#

Also, do not need to specify email column when the field being validated is the same name as the column.

humble garnet
#

Ignoring a model should work, right? I kinda feel like the user is actually an empty model (if there's no route model binding for that route?)

brazen narwhal
#

Actually you are correct Robert, just checked the core method.

hollow flax
#

still receiving
Request failed with status code 422

#

does it matter that my id is called _id on the users table?

brazen narwhal
#

Do you specify that as your PK on the user model? Assume you do if you are getting a properly route bound user model per your controller method.

hollow flax
#

yes i do

humble garnet
#

So, what are you receiving for validation errors? Hard to tell what's going on without knowing what the actual error is

brazen narwhal
#

Can you show what you are posting as well?

hollow flax
#

i am calling this from an react native app so its just showing me a 422 error code

brazen narwhal
#

Is this an admin endpoint, where you update the specified user from the bound model ID

hollow flax
#

const handleReset = async () => {
try {
const result = await axios.put(/api/updateEmail, {
oldPassword: oldEmail,
email: email,
});
resetTextInput();
setUpdateMessage(true);
return result.data;
} catch (e) {
setCodeErrorMessage(true);
console.log(e);
}
};

brazen narwhal
#

So your user model is empty

humble garnet
brazen narwhal
#

Your controller specifies a user, but you are not sending a user ID to your API call, thus your user model is an empty instance.

hollow flax
#

oh crap

#

i had a typo

brazen narwhal
#

and password ye

hollow flax
#

Im sorry guys for wasting your time

brazen narwhal
#

No worries, glad you got it sorted.

hollow flax
#

it just worked after i edited the api call

#

fresh eyes sometimes helps lol

brazen narwhal
#

Is this just for a user to change their email? Usually I would require their current password + new email to change it.

humble garnet