Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect with old input in Laravel?

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 ?

like image 385
code-8 Avatar asked May 02 '15 13:05

code-8


People also ask

How would you redirect back to a controller action?

Redirect to a Controller Action The last piece of the puzzle is that you can redirect to a specific Method of a specific Controller, no matter what its Route or URL is – use method action(): return redirect()->action('App\Http\Controllers\BooksController@index');


2 Answers

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...

like image 175
toesslab Avatar answered Sep 26 '22 02:09

toesslab


Try this

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

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

like image 21
Tongi Avatar answered Sep 26 '22 02:09

Tongi