我正在学习Laravel。我目前正在进行表单验证。在documentation中,可以说变量$ errors闪存到会话中并且始终可用。我得到一个例外,因为一个未定义的变量。我只粘贴了文档中的示例代码:
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
错误讯息:
ErrorException in 318c473e4384f7c25db0019a770ee937b30041d1.php line 41: Undefined variable: errors (View: C:\xampp\htdocs\NightClubs\resources\views\add.blade.php)
这是控制器中的验证规则:
$this->validate($request, [
'youtube' => 'required|url',
'coordinatex' => 'required|between:-180,180',
'coordinatey' => 'required|between:-90,90',
'nameofclub' => 'required'
]);
答案 0 :(得分:1)
尝试将该组用于要使用$errors
变量的路线:
Route::group(['middleware' => ['web']], function () {
// Your routes
// Your routes
}
答案 1 :(得分:0)
我假设您的控制器中有验证规则。
在您的视图中尝试此操作
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif