我正在使用以下标准代码来验证使用各种数组的表单。规则正确实现,但是当使用标准的'messages()'方法覆盖时,Laravel不会接收消息,而只是返回标准的内置消息。
输出
array (size=7)
0 => string 'The newitems.0.pickup_date field is required.' (length=45)
1 => string 'The newitems.0.m_f field is required when newitems.0.item type id is present.' (length=77)
2 => string 'The newitems.1.pickup_date is not a valid date.' (length=47)
3 => string 'The newitems.0.size field is required when newitems.0.item type id is present.' (length=78)
4 => string 'The newitems.1.size field is required when items.1.size unknown is not present.' (length=79)
5 => string 'The newitems.0.width field is required when newitems.0.item type id is present.' (length=79)
6 => string 'The newitems.1.width field is required when items.1.width unknown is not present.' (length=81)
FormRequest
UpdateOrderItemsRequest extends FormRequest{
[...]
public function rules()
{
return [
'items.*.item_type_id' => 'required_with:items.*.m_f,items.*.pickup_date|numeric',
'items.*.m_f' => 'required_with:items.*.item_type_id',
'items.*.pickup_date' => 'required_with:items.*.item_type_id|date|nullable',
'items.*.size' => 'required_without:items.*.size_unknown|numeric',
'items.*.width' => 'required_without:items.*.width_unknown|numeric',
'newitems.*.m_f' => 'required_with:newitems.*.item_type_id',
'newitems.*.pickup_date' => 'required_with:newitems.*.item_type_id|date',
'newitems.*.size' => 'required_with:newitems.*.item_type_id|required_without:items.*.size_unknown|numeric',
'newitems.*.width' => 'required_with:newitems.*.item_type_id|required_without:items.*.width_unknown|numeric'
];
}
/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'items.*.item_type_id' => 'An item type is required',
'items.*.m_f' => 'Gender is required',
'items.*.pickup_date' => 'A pickup date is required',
'items.*.size' => 'A size is required',
'items.*.width' => 'A width is required',
'newitems.*.m_f' => 'Gender is required',
'newitems.*.pickup_date' => 'A pickup date is required',
'newitems.*.size' => 'A size is required',
'newitems.*.width' => 'A width is required'
];
}
}
有什么东西是愚蠢而明显的,我错过了吗?
答案 0 :(得分:4)
自定义验证消息属性必须以验证规则名称结尾。 在您的情况下,您应该使用消息功能,如下面的
public function messages()
{
return [
'items.*.item_type_id.required_with' => 'An item type is required',
'items.*.m_f.required_with' => 'Gender is required',
'items.*.pickup_date.required_with' => 'A pickup date is required',
'items.*.size.required_without' => 'A size is required',
'items.*.width.required_without' => 'A width is required',
'newitems.*.m_f.required_with' => 'Gender is required',
'newitems.*.pickup_date.required_with' => 'A pickup date is required',
'newitems.*.size.required_with' => 'A size is required',
'newitems.*.width.required_with' => 'A width is required'
];
}