2

So, I'm currently trying to make change password functionality to my user profile, but I have some issues regarding PUT/UPDATE request reaching correctly to my UserController.

According to Laravel documentation I simply need to add @csrf and @method('PUT') below the form element, but when make dump request in controller I can see request reaches to the function in my controller but it doesn't take neccesary fields when submitting.

profile.blade.php

<form method="POST" action="{{route('update-password')}}">
    @csrf @method('PUT')

    <div class="form-group row">
        <label for="old_password" class="col-md-2 col-form-label">{{ __('Current password') }}</label>

        <div class="col-md-6">
            <input id="old_password" type="password" class="form-control" required autofocus >

        </div>
    </div>
    <div class="form-group row">
        <label for="new_password" class="col-md-2 col-form-label">{{ __('New password') }}</label>

        <div class="col-md-6">

            <input id="new_password" type="password" class="form-control" required autofocus >

        </div>
    </div>
    <div class="form-group row">
        <label for="password_confirm" class="col-md-2 col-form-label">{{ __('Confirm password') }}</label>

        <div class="col-md-6">
            <input id="password_confirm" type="password" class="form-control" required autofocus>

        </div>
    </div>
    <div class="form-group login-row row mb-0">
        <div class="col-md-8 offset-md-2">
            <button type="submit" class="btn btn-primary">
                {{ __('Submit') }}
            </button>
        </div>
    </div>
</form>

Controller.php

public function updatePassword(Request $request){

    $this->validate($request, [
        'old_password' => 'required',
        'new_password' => 'required|confirmed',
        'password_confirm' => 'required'
    ]);

    $user = User::find(Auth::id());

    if (!Hash::check($request->current, $user->password)) {
        return response()->json(['errors' => ['current'=> ['Current password does not match']]], 422);
    }

    $user->password = Hash::make($request->password);
    $user->save();

    return $user;
}

Currently when field are submitted it only submits

_token: UcJwliogSngHauWCNuDvRtRnZy8NP4lrQeNoZQGG
_method: PUT

and just makes redirect (302) back to profile page.

It should submit all the 3 fields.

1 Answer 1

3

You can send data in your inputs by adding a name attribute to them. In order for all of your inputs to work properly you need to add name to all of them. An example of it is as such:

<input id="old_password" name="old_password" type="password" class="form-control" required autofocus >

Note that id is not enough for sending data. For input identification you require name.

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

1 Comment

Thanks! All fields are now submitted to controller.

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.