我无法自定义哑剧的错误消息
我尝试了laravel 5.4 custom error message for MIME not working?此处给出的解决方案,但没有成功
请求:
public function rules()
{
$rules = [
'album'=> 'required',
'add_image.*' => 'image|mimes:png|max:2000',
];
return $rules;
}
public function messages()
{
$messages = [
'album.required' => 'Album is required',
'add_image.mimes' => "Image isn't png",
];
return $messages;
}
表单视图:
<form class="addImage" method="post" action="{{route('addImage.form')}}" enctype="multipart/form-data">
{{csrf_field()}}
<select name="album">
<option value="">-----</option>
@foreach($albums as $album)
<option value="{{$album}}">{{$album}}</option>
@endforeach
</select>
<input multiple="multiple" name="add_image[]" type="file" />
<button class="buttonADM" type="submit">Send</button>
</form>
谢谢您的帮助,对不起我的英语不好
答案 0 :(得分:0)
您正在验证数组,因此请尝试以下操作:
'add_image.*.mimes' => "Image isn't png",
我在项目中使用了您的表单,并尝试了此操作:
$valid = Validator::make(request()->all(), [
'add_image.*' => 'image|mimes:png'
], [
'add_image.*.mimes' => 'Image isnt png'
]);
dd($valid->errors());
// output
{
"add_image.0": [
"Image isnt png"
],
"add_image.1": [
"Image isnt png"
],
"add_image.2": [
"Image isnt png"
]
}
,我收到了自定义消息。因此,请检查错误包中是否包含正确的消息,并确保可以正常打印。
答案 1 :(得分:0)
我有不同的结果
控制器
public function addImage(/*addImageRequest $request*/){
if(session('user')['isA'] == 1){
$valid = Validator::make(request()->all(), [
'add_image.*' => 'image|mimes:png'
], [
'add_image.*.mimes' => 'Image isnt png'
]);
dd($valid->errors());
}
}
返回:
MessageBag {#224 ▼
#messages: array:1 [▼
"add_image.0" => array:1 [▼
0 => "The add_image.0 failed to upload."
]
]
#format: ":message"
}