Reputation: 691
I am using Laravel 5.2, I have form and wanting to insert fields with validation. I made createQuranRequest under Requests folder and trying to echo validation errors but I get below error
ErrorException in a19890dff92858726bf2b1048815af329d53d3b6.php line 6: Undefined variable: errors (View: /Applications/MAMP/htdocs/quran/resources/views/pages/quranForm.blade.php) (View:/Applications/MAMP/htdocs/quran/resources/views/pages /quranForm.blade.php)
My quranForm.blade.php code where I am trying to spit errors
<div class="form-group">
{!!Form::label('title','Surah Title:')!!}
{!!Form::text('title',null,['class' => 'form-control'])!!}
<span class="help-block">{{$errors->first('title') }}</span>
</div>
<div class="form-group">
{!!Form::label('lyrics','Surah Lyrics:')!!}
{!!Form::textarea('lyrics',null,['class' => 'form-control'])!!}
</div>
<div class="form-group">
{!!Form::submit('Add Surah',['class' => 'btn btn-primary'])!!}
</div>
My createQuranRequest file
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class createQuranRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required',
'lyrics' => 'required'
];
}
}
My controller file
public function store(createQuranRequest $request, quran $quran){
$quran->create($request->all());
return redirect('quran');
}
I have tried below code
@if (Session::get('errors'))
<ul>
@foreach ($errors->all() as $error)
<li>{ { $error } }</li>
@endforeach
</ul>
@endif
It removed the Exception Error but does not display the errors.
Upvotes: 0
Views: 1916
Reputation: 31
The laravel documentations says that the $errors variable is bound to the view by the Illuminate\View\Middleware\ShareErrorsFromSession middleware, which is provided by the web middleware group.
so u have to update your routes file located in app/http/routes.php
Route::group(['middleware' => ['web']], function ()
{
//this route will use the middleware of the 'web' group, so session and auth will work here
Route::post('/your-endpoint','MyController@store');
});
Upvotes: 1