我尝试使用验证器验证字段。但它不起作用。错误消息未显示在中。 这是我的控制器
$validator = Validator::make(
$request->all(),
[
'b_name' => 'required|max:20',
],
[
'b_name.required' => 'please fill this field',
]
);
if ($validator->fails()) {
return Response::make([
'message' => trans('validation failed'),
'errors' => $validator->errors(),
]);
}
这是我的刀刃
<div class="form-group">
<label for="Building" class="control-label">Building<sup class="mandatory">*</sup></label>
<input type="text" class="form-control" name="b_name" id="b_name" value=""required="true" aria-required="true" />
<div class="help-block b_name-error"></div>
</div>
此字段为模态。有可能因为模态而无法正常工作。
答案 0 :(得分:1)
您正在返回错误消息,但在刀片文件中没有任何错误消息要显示。
if ($validator->fails()) {
return redirect()->back()->withErrors(['msg', $validator->errors()->first()]);
}
现在出现以下错误。
@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif
答案 1 :(得分:0)
您必须输入此代码。
if ($validator->fails()) {
return redirect()->back()->withErrors(['msg', $validator->errors()->first()]);
}