我正在验证Laravel
中的表单,但我没有收到空错误消息。
$validator = Validator::make($all, [
'email' => 'required|email',
'email_confirmation' => 'required',
'first_name' => 'required',
'last_name' => 'required',
'gender' => 'required',
'year' => 'required',
'month' => 'required',
'day' => 'required',
'birth_country' => 'required',
'birth_city' => 'required',
'native_country' => 'required',
'password' => 'required',
'password_confirmation' => 'required',
'address' => 'required',
'country' => 'required',
'region' => 'required',
'city' => 'required',
'tel_area_code' => 'required',
'tel_number' => 'required',
'postal_code' => 'required',
'mobile_area_code' => 'required',
'mobile_number' => 'required',
'terms_cond_check' => 'required',
],[
'terms_cond_check.required' => 'You must agree to our Terms and Conditions',
]);
回复是
MessageBag {#568 ▼
#messages: array:10 [▼
"password" => array:1 [▼
0 => ""
]
"password_confirmation" => array:1 [▼
0 => ""
]
"address" => array:1 [▼
0 => ""
]
"region" => array:1 [▼
0 => ""
]
"city" => array:1 [▼
0 => ""
]
"tel_area_code" => array:1 [▼
0 => ""
]
"tel_number" => array:1 [▼
0 => ""
]
"postal_code" => array:1 [▼
0 => ""
]
"mobile_area_code" => array:1 [▼
0 => ""
]
"mobile_number" => array:1 [▼
0 => ""
]
]
#format: ":message"
}
答案 0 :(得分:1)
首先,您需要在表单视图中重定向。我的意思是加上这个 验证后
if ($validator->fails()) {
return redirect(route('tape here your form route (you can find that in route/web.php)'))
->withErrors($validator)
->withInput();
}
之后,您需要在表单视图中显示所有错误消息。
只需将其添加到表单视图页面的顶部即可显示所有错误消息:
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>