2017-10-09 189 views
2

我正在使用以下標準代碼來驗證使用各種數組的表單。規則被正確地實現,但是當使用標準的'messages()'方法覆蓋時,Laravel不會接收消息,而只是返回標準的內置消息。Laravel 5.5 FormRequest自定義錯誤消息驗證不起作用

輸出

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' 
     ]; 
    } 
} 

有什麼愚蠢和明顯我失蹤?

回答

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' 
    ]; 
} 
+0

完美!謝謝。我知道它一定是這樣的。現在我可以看到我誤解了文檔。 – fred2

+1

沒問題,錯誤讓人強烈學習 –