Reputation: 923
I'm building a simple login form using Laravel 5 and I would like to fill some inputs by default when some error occurred. My router is calling these functions as given
Route::get('/login','AuthController@login');
Route::post('/login','AuthController@do_login');
My Controller is like showed below
namespace App\Http\Controllers;
use App;
use App\Models\User;
use Request;
use Illuminate\Support\Facades\Redirect;
class AuthController extends Controller {
function login()
{
if(User::check())
{
/*User is logged, then cannot login*/
return Redirect::back();
}
else
{
return view('auth.login');
}
}
function do_login()
{
return view('auth.login')->withInput(Request::except("password"));
}
}
Noticing that withInput is being underlined by PhpStorm saying that can't find it's implementation in Illuminate\View\View. Inside my view, i have the following:
{!! Form::open() !!}
<div class="form-group">
{!! Form::text('email', Input::old('email'), array('placeholder' => Lang::get('auth.email'), 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::password('password', array('placeholder' => Lang::get('auth.password'),'class'=>'form-control')) !!}
</div>
<div class="form-group">
<button class="btn btn-primary btn-block">@lang('auth.signin')</button>
<span class="pull-right"><a href="/{{App::getLocale()}}/register">@lang('auth.register')</a></span>
</div>
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
{!! Form::close() !!}
What am I missing here? This can be done in Laravel 5 ? Many thanks for your time!
Upvotes: 6
Views: 29081
Reputation: 31
When you need with old data if you are not use laravel validation then you can try blow
return Redirect::back()->withInput()
This works Perfectly
Upvotes: 0
Reputation: 144
When you return one redirect requirement(http response header "Location" item is not null), the session data corresponding to withInput will not be erased. So the next request can access this data.
When you return the response page with no redirect requirement("Location" item is null), the session data corresponding to withInput will be erased.
So, we can see that "withInput" function only work in redirect.
Upvotes: 0
Reputation: 2329
In Laravel 5.2, you can write
return Redirect::back()->withErrors([$errors])->withInput(Input::all());
Upvotes: 0
Reputation: 247
Building on @AnthonyPillos 's answer,
Laravel 5.2.27 I think it was removed the need for the web middleware group, hence why removing this works.
So:
Route::group(['middleware' => ['web']], function () { });
becomes redundant.
Hope it helps someone else searching through validateRequests, validatesLogin etc. etc. just to discover it was the route.
Upvotes: 0
Reputation: 205
My solution for this problem... Instead of putting your routes inside of this
Route::group(['middleware' => ['web']], function () { });
Just remove it and it will work properly. Its like 'web' middleware is loading twice.
Upvotes: 0
Reputation: 31
Passing data in the error object is a helpful tip? What if you're going to use the error object the way it's intended?
Instead, there is a proper way to do that. The second parameter you're passing to ->view is data! horray!
return view('your.view', dataYouWannaPass)
does the job perfectly!
laravel 5.1 doc also recomends using just
->with($data)
http://laravel.com/docs/5.1/views
Upvotes: 0
Reputation: 1553
I would suggest you to use the MessageBag class. First import it's namespace at the top of your controller's file:
use Illuminate\Support\MessageBag;
Then you can use it in your controller:
$message = new MessageBag(["username" => "A default and valid username"]); // Create your message
Redirect::to('auth/login')->withErrors($message); // Return it to your view, whatever the url
And then in your auth/login view:
{!! Form::text('username', Input::old('username'), array('placeholder' => ($errors->has('username')?$errors->first('username'):Lang::get('auth.username')), 'class' => 'form-control')) !!}
You could even replace the input's value too if you want:
{!! Form::text('username', ($errors->has('username')?$errors->first('username'):null), array('placeholder' => ($errors->has('username')?$errors->first('username'):Lang::get('auth.username')), 'class' => 'form-control')) !!}
Your "username" input should now contain the "A default and valid username" message if an error is encountered.
Upvotes: 2
Reputation: 180136
withInput()
is for preserving the input during redirects.
It is not necessary (nor possible) to call it if you're doing a return view()
.
You might consider having your do_login
do return redirect()->back()->withInput(Request::except("password"));
though, to properly implement Post/Redirect/Get.
Upvotes: 12