How can I redirect with old input in Laravel?
Asked Answered
H

4

20

I have a login modal. When the user log-in fail the authentication, I want to redirect back to this modal with :

  • error message(s)
  • and old input(s).

enter image description here


Controller

// if Auth Fail 
return Redirect::to('/')
                ->with('error','Username/Password Wrong')
                ->withInput(Request::except('password'))
                ->withErrors($validator);

Form

{!! Form::open(array('url' => '/', 'class' => 'login-form')) !!}

  <div class="form-group">
    <label for="username">Username</label>
    <input type="text" class="form-control"  id="username" name="username" placeholder="Enter Username" required>
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" id="password" name="password" placeholder="Enter Password" required>
  </div>
  <button type="submit" class="btn btn-primary">Login</button>

{!! Form::close() !!}

As you can see as part of my image, the error message seem to display, but the old input of username doesn't seem to populate.

Can someone please correct me ? Did I forget to do anything ? What is the most efficient way in Laravel to accomplish something like this ?

Hierarchy answered 2/5, 2015 at 13:2 Comment(0)
V
38

You can access the last inputs like so:

$username = Request::old('username');

As pointed at by @Arian Acosta you may simply do

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

As described in the docs it is more convenient in a blade view.


There are several possibilties in the controller:

Instead of ->withInput(Request::except('password'))

do:

a) Input::flash();

or:

b) Input::flashExcept('password');

and redirect to the view with:

a) ->withInput(Input::except('password'));

resp. with:

b) ->withInput();

The docs about Old Input for further reading...

Villainage answered 2/5, 2015 at 14:41 Comment(5)
Your solution is working for me. I got my old input to display now. Thank you so much.Hierarchy
Please take off Request::flash(); I don't need to do that.Hierarchy
Glad I could help. Yepp, because Request::except('password') already does it.Villainage
old() is also a global, you don't need to use Request::old('username'). Simply do value="{{ old('username') }}"Carleycarli
Input::flash() is cool when you use the "what was the name of the redirect to GET route after request to POST route - HTTP pattern again?" Ah PRG patternDetritus
S
5

Try this

 return redirect()->back()
       ->with('error','username is invalid')
       ->withInput();

make sure you have value="{{ old('username') }}" in your input element

Sealskin answered 15/3, 2021 at 14:23 Comment(0)
C
2

You are missing the value on your input element...

value="{{ Input::get ('username', '') }}"
Chloroplast answered 2/5, 2015 at 14:22 Comment(1)
This doesn't work ! I've tried <input type="text" class="form-control" id="username" name="username" placeholder="Enter Username" required value="{{ Input::get ('username', '') }}" > and nothing seem to populate.Hierarchy
M
0

For my project, I needed another way:

Shove a GET request in the validation return to fetch some previous data:

   // if the validator fails, redirect back to the form
    if ($validator->fails()) {
        //let's send some data back (Orlando,Florida):
        return Redirect::to('auth/register?city=Orlando&State=Florida')
            ->withErrors($validator)


    }else{
     //validation success
    }
Magistracy answered 7/12, 2018 at 2:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.