我正在构建一个包含多个字段的表单,其中还有一个文件上传输入。
我正在尝试在每次输入后显示一条错误消息,但是似乎这种逻辑不适用于文件上传。
<div class="form-group">
<label for="attachment">Attachment</label>
<input id="attachment" type="file" class="form-control" name="attachment">
</div>
@if ($errors->has('attachment'))
<span class="error">
<strong>{{ $errors->first('attachment') }}</strong>
</span>
@endif
我的验证规则如下:
$validatedData = $request->validate([
'firstname' => 'required|max:32|regex:/^[. a-zA-Z0-9_-]+$/',
'lastname' => 'required|max:32|regex:/^[. a-zA-Z0-9_-]+$/',
'email' => 'required|email',
'message' => 'required|max:1000',
'captcha' => 'required|captcha',
'attachment' => 'file|max:1024|mimes:jpeg,bmp,png'
]);
错误消息似乎在消息袋中,但Blade模板中没有输出:
ViewErrorBag {#561 ▼
#bags: array:1 [▼
"default" => MessageBag {#552 ▼
#messages: array:1 [▼
"attachment" => array:2 [▼
0 => "The attachment may not be greater than 1024 kilobytes."
1 => "The attachment must be a file of type: jpeg, gif, png."
]
]
#format: ":message"
}
]
}
所以我的问题是:为了获取特定字段的错误,我需要做什么?
谢谢。