[英]Laravel 5.4 - Error Message Without Form Validation
想知道我是否可以將Laravel 5.4上的驗證錯誤消息與不包含表單驗證的自定義錯誤消息一起使用。
我正在對查看用戶個人資料進行驗證,以確保只有id的所有者才能使用Gate
查看用戶個人資料。
我是否需要創建Validator
的實例,並在Laravel docs上做類似的事情 ?
以下是我的代碼示例,
UserController.php
public function show(User $user)
{
if (\Gate::denies('showUser', $user)) {
return redirect()->back()
->with('status', 'Invalid Access to other User Profile');
// how to use instead ->withErrors($message); ?
}
return view('profiles.update', compact('user'));
}
messages.blade.php
@if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<ol>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ol>
</div>
@endif
@if(session('success'))
<div class='alert alert-success'>
{!! session('success') !!}
</div>
@endif
@if(session('status'))
<div class='alert alert-danger'>
{!! session('status') !!}
</div>
@endif
希望這會有所幫助。
public function show(User $user, MessageBag $message_bag)
{
if (\Gate::denies('showUser', $user)) {
$message_bag->add('token', 'This is the error message');
return redirect()->back()->withErrors($message_bag);
}
return view('profiles.update', compact('user'));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.