IsaacBen's avatar

Keep inputs after failed validation

If form validation fails then I have to fill all the fields all over again. Is there a way to keep inputs filled and just fix the error?

0 likes
18 replies
frezno's avatar
frezno
Best Answer
Level 36
<input type="text" name="firstname" id="firstname" class="form-control" value="{{ $user->firstname or old('firstname') }}">
6 likes
IsaacBen's avatar

@JoeDawson Hey I tried this in the controller, but it's not working.

    public function update($id, TicketRequest $request)
    {
    
        if ($validator->fails()) {
            return redirect()->back()->withInput();
        }
        ......
    }
IsaacBen's avatar

I need to do it in the Request file I think, but can't find out where.

richardbishopme's avatar

Hi @IsaacBen,

Is TicketRequest a form request including validation rules? If so, Laravel should redirect you back to the previous page and flash the errors to the session automatically.

You can do what @frezno suggested and use the old function to display the values. The old function takes a second parameter too where you can display a default value.

Not sure if it makes a difference, but try re-ordering the parameters in the controller method (TicketRequest before id) and see what happens:

 public function update(TicketRequest $request, $id)
 {
   ...
}
IsaacBen's avatar

Okay guys thanks. The old thing should work, I just need to add conditions in case I already Have a value. The "or" thing is not working.

frezno's avatar

@IsaacBen

I have like 30 fields so it will be tedious to do it that way.

you only have to do it once and it does work that way.
imagine you have a checkout form and want to go back from step 3 to step 1 to correct fields. In this case the redirect way wouldn't work, imho (haven't tried it though).
so, yes, it's a little bit anoying - copy & paste does help - but in the end, it's working.

richardbishopme's avatar

Hi @IsaacBen,

The old function takes a second parameter for a default, you can do this for edit pages:

{{  old('firstname', $user->firstname) }}
4 likes
IsaacBen's avatar

@frezno Yeah you're right, but there is one problem. It won't work if the user is changing a value. It will only work if the input was empty from the beginning. I have a condition that if there is no value in the DB then put the old one. I can't think about other way around it.

richardbishopme's avatar

Hi @IsaacBen ,

No probs - just discovered that a couple of weeks ago - super useful :)

If you're using an include to share the form between your insert and edit pages though, it will break.

If you are, then in your insert controller, just return a new User to the view.

IsaacBen's avatar

@desloc You mean like this?

<div style-"position:relative;">   
@include('ticketEditForm')
</div>

I have that, but it still works from what I saw.

IsaacBen's avatar

@desloc No, I have a separate form for the insert since it's much less fields.

tempaccount's avatar

Hi, I tried all the solutions above but still no luck. Please help me out.

blade:

                <div class="form-group">
                    <input type="text" class="form-control" name="description[]" value="{{old('description[]')}}"/>
                </div>
            </td>
            <td>
                <div class="form-group">
                    <input type="file"  class="form-control" name="image[]" multiple/>
                </div>
            </td>

Controller:

    if($validatedData = $request->validate([
                'bill_number.*'=>'required|date',
                'description.*'=>'required',
             'image.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]))
        {
        ......
            ......
        ......
                return redirect('/employee/home')->withInput()->with('alert-success','Application is sent for approval');
        }else{
                return redirect('/employee/home')->wihtInput()->with('error');
    }
armancs's avatar

what is the purpose for dropdown field?

Please or to participate in this conversation.