Skip to content
Advertisement

Laravel 8 – Handle errors on PUT form request

I want to handle errors of put request like I do on POST request.

Is that possible ?

Because my actual solution seems don’t work.

My handling errors looks like this

@error('name')
    <span class="invalid-feedback is-invalid" role="alert">
        <strong>{{ $message }}</strong>
    </span>
@enderror

My form code

<form class="theme-form" method="POST" action="{{ route('user-profile-information.update') }}">
    @csrf
    {{ method_field('PUT') }}

    <div class="form-group row">
        <label class="col-sm-3 col-form-label" for="name">Name</label>

        <div class="col-sm-9">
            <input type="text" class="form-control" id="name" name="name" value="{{ Auth::user()->name }}" />
        </div>

        @error('name')
            <span class="invalid-feedback is-invalid" role="alert">
                <strong>{{ $message }}</strong>
            </span>
        @enderror
    </div>

    <div class="form-group row">
        <label class="col-sm-3 col-form-label" for="email">Email Address</label>

        <div class="col-sm-9">
            <input type="email" class="form-control" id="email" name="email" value="{{ Auth::user()->email }}" />
        </div>

        @error('email')
            <span class="invalid-feedback is-invalid" role="alert">
                <strong>{{ $message }}</strong>
            </span>
        @enderror
    </div>

    <input type="submit" class="btn btn-primary f-right" value="Save" />
</form>

Advertisement

Answer

you need use validate in your controller ex:

 public function update(Request $request, User $user,$id)
    {

        $request->validate([
            'name' => 'required|max:65|min:4',
            'email' => 'required|email',
        ]);

        $date = $request->except(['_token','_method']);

        $user->where('id', $id)->update($date);

        return redirect()->route('home');

    }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement