2017-08-08 45 views
1

我需要驗證具有圖像和標籤的請求。如何爲標記返回常見的驗證錯誤消息。如何獲得陣列字段的常見驗證錯誤消息

$this->validate($request, [ 
    'image' => 'required|image, 
    'tags.*' => 'string' 
]); 

Currecnt message is。

{ 
    "image": [ 
     "The image field is required." 
    ], 
    "tags.0": [ 
     "The tags.0 must be a string." 
    ], 
    "tags.1": [ 
     "The tags.1 must be a string." 
    ] 
} 

預期消息是。

{ 
    "image": [ 
     "The image field is required." 
    ], 
    "tags": [ 
     "The tags must be a string." 
    ] 
} 

回答

0

我想你應該與下面的例子類似的嘗試:

public function messages() 
     { 
      $messages = []; 
      foreach ($this->request->get('tags') as $key => $val) { 
       $messages['tags.' . $key . '.string'] = 'The tags must be a string.' 
      } 
      return $messages; 
     } 

希望這對你的工作!

1

你有沒有試過,

$this->validate($request, [ 
    'image' => 'required|image, 
    'tags.*' => 'string' 
],$messages = [ 
    'tags.*' => 'The tags must be a string.' 
]); 

我不會放棄知道,但,這可能爲你工作。

+0

消息已更改。但它來自多個數組索引。 「tags.0」:[ 「標籤必須是字符串。」 ], 「tags.1」:[ 「標籤必須是字符串。」 ] – noufalcep

+0

@noufalcep這是因爲你有多個不正確的字段的數組輸入字段 –

+0

是的。我需要多個輸入的單個錯誤消息。 – noufalcep

0

您可以使用*將消息添加到您的翻譯文件as documented

'custom' => [ 
    'tags.*' => [ 
     'string' => 'The tags must be a string.', 
    ] 
], 
+0

消息已更改。但它來自多個數組索引。 「tags.0」:[「標籤必須是一個字符串。」 ],「tags.1」:[「標籤必須是一個字符串。」 ] – noufalcep