How can I display the validation message in the view that is being redirected in Laravel ?
Here is my function in a Controller
public function registeruser()
{
$firstname = Input::get('firstname');
$lastname = Input::get('lastname');
$data = Input::except(array('_token')) ;
$rule = array(
'firstname' => 'required',
'lastname' => 'required',
) ;
$validator = Validator::make($data,$rule);
if ($validator->fails())
{
$messages = $validator->messages();
return Redirect::to('/')->with('message', 'Register Failed');
}
else
{
DB::insert('insert into user (firstname, lastname) values (?, ?)',
array($firstname, $lastname));
return Redirect::to('/')->with('message', 'Register Success');
}
}
I know the below given code is a bad try, But how can I fix it and what am
I missing
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
Update : And how do I display the error messages near to the particular fields
When the validation fails return back with the validation errors.
if($validator->fails()) {
return Redirect::back()->withErrors($validator);
}
You can catch the error on your view using
@if ($errors->any())
{{ implode('', $errors->all('<div>:message</div>')) }}
@endif
UPDATE
To display error under each field you can do like this.
<input type="text" name="firstname">
@if ($errors->has('firstname'))
<div class="error">{{ $errors->first('firstname') }}</div>
@endif
For better display style with css.
You can refer to the docs here.
@if ($errors->has('category'))
<span class="error">{{ $errors->first('category') }}</span>
@endif
If you want to load the view from the same controller you are on:
if ($validator->fails()) {
return self::index($request)->withErrors($validator->errors());
}
And if you want to quickly display all errors but have a bit more control:
@if ($errors->any())
@foreach ($errors->all() as $error)
<div>{{$error}}</div>
@endforeach
@endif
Move all that in kernel.php if just the above method didn't work for you
remember you have to move all those lines in kernel.php in addition to the above solution
let me first display the way it is there in the file already..
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
now what you have to do is
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
],
'api' => [
'throttle:60,1',
],
];
i.e.;