If you're using Blade templates, echo it using the {{ $err }} syntax. Otherwise <?php echo $err; ?>
Hi,
Validation & error messages is very well documented: http://laravel.com/docs/validation
You can show errors in your views created by the validation class the following way:
{{ $errors->first('field_name') }}
Where field_name is the name of the input.
When login, you can do the following:
public function store()
{
$errors = new MessageBag; // initiate MessageBag
$credentials = [
'email' => Input::get('email'),
'password' => Input::get('password')
];
if (Auth::attempt($credentials)) // use the inbuilt Auth::attempt method to log in the user ( if the credentials are wrong, this will fail )
return Redirect::to('account')->with('alert-success', 'You are now logged in.'); // if the credentials were correct, Auth::attempt will log in the user automatically and you can redirect the user to the intended page. Moreover, using the ->with() method, you can store a message in a session, which can be accessed on the next page. (se explanation under)
$errors = new MessageBag(['password' => ['Email and/or password invalid.']]); // if Auth::attempt fails (wrong credentials) create a new message bag instance.
return Redirect::back()->withErrors($errors)->withInput(Input::except('password')); // redirect back to the login page, using ->withErrors($errors) you send the error created above
}
Now in your login view, you can show the error message by doing:
@if ($error = $errors->first('password'))
<div class="alert alert-danger">
{{ $error }}
</div>
@endif
http://laravel.com/docs/security#authenticating-users
And remember to include the MessageBag class in your controller, basically put this in top of your controller, e.g:
<?php
use Illuminate\Support\MessageBag;
class SessionsController extends \BaseController
{
}
To access the message created by the ->with() method, do:
In your controller:
return Redirect::to('account')->with('alert-success', 'You are now logged in.');
In your view:
@if ($alert = Session::get('alert-success'))
<div class="alert alert-warning">
{{ $alert }}
</div>
@endif
Hope this helps.
sajeeldev liked this reply
AndrewBNZ said:
If you're using Blade templates, echo it using the {{ $err }} syntax. Otherwise <?php echo $err; ?> Yes, but if there is no error, i will get undefined variable "err"
dvabr said:
Hi,
Validation & error messages is very well documented: http://laravel.com/docs/validation
You can show errors in your views created by the validation class the following way:
{{ $errors->first('field_name') }}
Where field_name is the name of the input.
When login, you can do the following:
public function store() { $errors = new MessageBag; // initiate MessageBag $credentials = [ 'email' => Input::get('email'), 'password' => Input::get('password') ]; if (Auth::attempt($credentials)) // use the inbuilt Auth::attempt method to log in the user ( if the credentials are wrong, this will fail ) return Redirect::to('account')->with('alert-success', 'You are now logged in.'); // if the credentials were correct, Auth::attempt will log in the user automatically and you can redirect the user to the intended page. Moreover, using the ->with() method, you can store a message in a session, which can be accessed on the next page. (se explanation under) $errors = new MessageBag(['password' => ['Email and/or password invalid.']]); // if Auth::attempt fails (wrong credentials) create a new message bag instance. return Redirect::back()->withErrors($errors)->withInput(Input::except('password')); // redirect back to the login page, using ->withErrors($errors) you send the error created above }
Now in your login view, you can show the error message by doing:
@if ($error = $errors->first('password')) <div class="alert alert-danger"> {{ $error }} </div> @endif
http://laravel.com/docs/security#authenticating-users
And remember to include the MessageBag class in your controller, basically put this in top of your controller, e.g:
<?php use Illuminate\Support\MessageBag; class SessionsController extends \BaseController { }
To access the message created by the ->with() method, do:
In your controller:
return Redirect::to('account')->with('alert-success', 'You are now logged in.');
In your view:
@if ($alert = Session::get('alert-success')) <div class="alert alert-warning"> {{ $alert }} </div> @endif
Hope this helps.
Actually, i had some problems with auth attempt, so i just used auth login
$auth = User::where('Name', '=', Input::get('username'))->where('Password', '=', Hash::make(Input::get('password')))->first(); if($auth){ if(Input::get('rememberme')) { Auth::login($auth, true); } else { Auth::login($auth); } return Redirect::intended('home'); } else { View::make('login')->with('err', 'There was a problem signing in'); } }
Joe96 said: Actually, i had some problems with auth attempt, so i just used auth login
$auth = User::where('Name', '=', Input::get('username'))->where('Password', '=', Hash::make(Input::get('password')))->first(); if($auth){ if(Input::get('rememberme')) { Auth::login($auth, true); } else { Auth::login($auth); } return Redirect::intended('home'); } else { View::make('login')->with('err', 'There was a problem signing in'); } }
That's the wrong way to do it.
What does your User model looks like?
dvabr said:
Joe96 said: Actually, i had some problems with auth attempt, so i just used auth login
$auth = User::where('Name', '=', Input::get('username'))->where('Password', '=', Hash::make(Input::get('password')))->first(); if($auth){ if(Input::get('rememberme')) { Auth::login($auth, true); } else { Auth::login($auth); } return Redirect::intended('home'); } else { View::make('login')->with('err', 'There was a problem signing in'); } }
That's the wrong way to do it.
What does your User model looks like? I tried your method and it worked, thanks
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community