return redirect()->back()->withInput();
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?
<input type="text" name="firstname" id="firstname" class="form-control" value="{{ $user->firstname or old('firstname') }}">
@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();
}
......
}
I need to do it in the Request file I think, but can't find out where.
@frezno I have like 30 fields so it will be tedious to do it that way.
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)
{
...
}
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.
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.
Hi @IsaacBen,
The old function takes a second parameter for a default, you can do this for edit pages:
{{ old('firstname', $user->firstname) }}
@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.
@desloc Perfect, that solved my problem. Thank you!
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.
Hi @IsaacBen ,
Yep. Do you also use that include in your insert view?
@desloc No, I have a separate form for the insert since it's much less fields.
@IsaacBen, Ah you should be good to go then :)
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');
}
what is the purpose for dropdown field?
Please or to participate in this conversation.