Pass old input after Form Request validation in laravel 5
Asked Answered
S

3

16

if the validation fails it's not clear to me how i could pass the old input to fill again the form.

I mean, I know how to pass the data when using the Validator class and redirecting after fail with the method withInput(), but I'm trying to learn how to use Form Requests provided in laravel 5. Thanks

Studhorse answered 23/3, 2015 at 15:42 Comment(1)
The form requests do that for you automatically.Romano
Z
36
$username = Request::old('username');

or in view:

{{ old('username') }}

Read more: http://laravel.com/docs/5.0/requests#old-input

Zora answered 23/3, 2015 at 15:46 Comment(5)
{{{ old('username') }}} // for laravel earlier versions xss preventingPolycrates
I had to use the e() function in this case <input id=targa type=text name=targa size=10{!! (old('targa'))?' value="'.e(old('targa')).'"':' placeholder="Ej. AAA000"' !!}> do you have any other idea to do the same thing? I will have to do it almost for every field as i love to use placeholdersStudhorse
@Gabz if you're using L5 you don't need to follow Alupotha comment.Zora
@Polycrates author mentioned L5 in question and in tag.Zora
This does not seem to work for file uploads form - I cannot seem to populate the file upload section within a form - it just returns an empty arrayHb
V
8

You can redirect with old data like below with validation error

return redirect()->back()->withErrors($validator)->withInput();
Vary answered 20/6, 2018 at 4:55 Comment(1)
i tried this, but not working for me, we just need to pas this in controller right if validation condition failsWoodwaxen
B
0

<input type="text" name="username" value="{{ old('username') }}">

you must also define withInput() for the http route when redirecting to a page for example

return back()->withInput();

Bowfin answered 17/6, 2021 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.