如何在Laravel中重定向的视图中显示验证消息?
这是我在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');
}
}
我知道下面给出的代码是一个糟糕的尝试,但我怎么能解决它和什么 我错过了
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
答案 0 :(得分:57)
验证失败时返回验证错误。
if($validator->fails()) {
return Redirect::back()->withErrors($validator);
}
您可以使用
捕捉视图中的错误@if ($errors->any())
{{ implode('', $errors->all('<div>:message</div>')) }}
@endif
<强>更新强>
要在每个字段下显示错误,您可以这样做。
<input type="text" name="firstname">
@if ($errors->has('firstname'))
<div class="error">{{ $errors->first('firstname') }}</div>
@endif
使用css获得更好的显示风格。
您可以参考文档here.
答案 1 :(得分:5)
@if ($errors->has('category'))
<span class="error">{{ $errors->first('category') }}</span>
@endif
答案 2 :(得分:5)
如果要从同一控制器上加载视图,则:
if ($validator->fails()) {
return self::index($request)->withErrors($validator->errors());
}
如果您想快速显示所有错误但又有更多控制权:
@if ($errors->any())
@foreach ($errors->all() as $error)
<div>{{$error}}</div>
@endforeach
@endif
答案 3 :(得分:1)
要使其看起来不错,可以使用一些引导程序帮助
@if(count($errors) > 0 )
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<ul class="p-0 m-0" style="list-style: none;">
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
答案 4 :(得分:1)
$validator = Validator::make($request->all(), [ 'email' => 'required|email', 'password' => 'required', ]);
if ($validator->fails()) { return $validator->errors(); }
答案 5 :(得分:0)
如果只是上面的方法不适合你,请将所有内容移到kernel.php中 记住除了上面的解决方案之外,你还必须在kernel.php中移动所有这些行
让我先显示它在文件中的存在方式..
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',
],
];
现在你需要做的是
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',
],
];
即;
答案 6 :(得分:0)
{!! Form::text('firstname', null !!}
@if($errors->has('firstname'))
{{ $errors->first('firstname') }}
@endif
答案 7 :(得分:0)
Laravel 5.8.13
中出现了新的Laravel刀片错误指令// Before
@if ($errors->has('email'))
<span>{{ $errors->first('email') }}</span>
@endif
// After:
@error('email')
<span>{{ $message }}</span>
@enderror
答案 8 :(得分:0)
@if($errors->any())
{!! implode('', $errors->all('<div class="alert alert-danger">:message</div>')) !!}
@endif
我们可以根据要求设置标签格式。
答案 9 :(得分:0)
在输入字段下方,我包含了额外的视图:
@include('input-errors', ['inputName' => 'inputName']) #For your case it would be 'email'
input-errors.blade.php
@foreach ($errors->get($inputName) as $message)
<span class="input-error">{{ $message }}</span>
@endforeach
CSS - 为消息添加红色。
.input-error {
color: #ff5555;
}
答案 10 :(得分:0)
如果使用 toastr,请使用以下内容显示带有浮动消息的错误
@if($errors->any())
<script type="text/javascript">
toastr.error({{ implode(' ', $errors->all()) }});
</script>
@endif