如何在$validator->fails()
$validator = Validator::make($request->all(), [
'patient_uname' => 'required|max:255|Alpha',
'patient_pass' => 'required|max:255|Alpha'
]);
if ($validator->fails()) {
}
例如,如果patient_uname
失败,则应显示patient_name
答案 0 :(得分:1)
检查
public function postReset(Request $request)
{
$message = [
'patient_uname.required' => 'Patient name is required',
'patient_pass.required' => 'Patient pass is required'
];
$this->validate(
$request,
[
'patient_uname' => 'required|max:255|Alpha',
'patient_pass' => 'required|max:255|Alpha'
],
$message
);
验证者需要三个参数
$validator = Validator::make($input, $rules, $messages);
$messages = ['patient_uname.required' => 'patient name is required'];
$rules = ['patient_uname' => 'required|max:255|Alpha'];
在您的问题中,只需传递一个消息数组
$validator = Validator::make($request->all(), [
'patient_uname' => 'required|max:255|Alpha',
'patient_pass' => 'required|max:255|Alpha'
], $messages);
答案 1 :(得分:1)
您可以获取所有失败字段的键,如下所示:
if ($validator->fails()) {
dd($validator->errors()->keys());
}
返回:
array:2 [
0 => "patient_uname"
1 => "patient_pass"
]
您可以收到像这样的所有错误消息
if ($validator->fails()) {
dd($validator->errors()->all());
}
返回:
array:2 [
0 => "The patient uname field is required."
1 => "The patient pass field is required."
]
您可以获得一个这样的特定字段:
if ($validator->fails()) {
dd($validator->errors()->get('patient_uname'));
}
返回:
array:1 [
0 => "The patient uname field is required."
]