我在显示验证错误消息时遇到了麻烦。我尝试了其他类似的问题,但对我不起作用。请帮助我。
这是我的 Controller.blade.php
public function store(Request $request)
{
$this->validate($request,[
'surname'=>['required', 'string', 'max:255'],
'firstname'=>['required', 'string', 'max:255'],
'birthday'=>'required',
'pofbirth'=>['required', 'string', 'max:255'],
'gender'=>['required', 'numeric'],
'address'=>['required', 'string', 'max:255'],
'city'=>['required', 'string', 'max:255'],
'district'=>['required', 'string', 'max:255'],
'cap'=>['required', 'string', 'max:255'],
'cstatus'=>['required', 'string', 'max:255'],
'conjugated'=>['required', 'string', 'max:255'],
'cellphone'=>['required', 'numeric'],
'email'=>['required', 'string', 'email', 'max:255'],
]);
这是我的 message.blade.php
@if (count($errors) > 0)
@foreach ($errors->all() as $error)
<p class="alert alert-danger">{{ $error }}</p>
@endforeach
@endif
@if (session()->has('message'))
<p class="alert alert-success">{{ session('message') }}</p>
@endif
这是我的 blade.php
@include('includes.navbar')
<div class="col" style="background-color: #ffffff;">
@include('includes.message')
<form method="POST" action="{{ route('app.store')}}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="container shadow" style="background-color: rgb(248,249,250);">
<div class="row">
<div class="col">
...
...
...
非常感谢您!
答案 0 :(得分:1)
在您的方法中,您没有任何名为message
的会话密钥,但在验证中没有任何名为message
的密钥:
@if (session()->has('message'))
<p class="alert alert-success">{{ session('message') }}</p>
@endif
应为firstname
,surname
,gender
,age
等(验证中使用的密钥)。
像这样:
@if($errors->has('firstname'))
<p class="alert alert-success">{{ $errors->first('firstname') }}</p>
@endif
获取所有错误消息:
@if ($errors->any())
@foreach ($errors->all() as $error)
<div>{{$error}}</div>
@endforeach
@endif
以这种方式更改您的验证:
$validatedData = $request->validate([
'surname' => 'required|string|max:255',
'firstname' => 'required|string|max:255',
'birthday' => 'required',
'pofbirth' => 'required|string|max:255',
'gender' => 'required|numeric',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'district' => 'required|string|max:255',
'cap' => 'required|string|max:255',
'cstatus' => 'required|string|max:255',
'conjugated' => 'required|string|max:255',
'cellphone' => 'required|numeric',
'email' => 'required|string|email|max:255',
]);