2016-12-07 73 views
1

Laravel驗證自定義消息我想問一下關於Laravel驗證一些建議......使用嵌套屬性

比方說,我有一個名爲invoiceAddress[name]輸入和控制器我有一個規則

$rule = ['invoiceAddress.name' => 'required',]; 

,或只是一個

$validator = \Validator::make($request->all(), [ 
    'invoiceAddress.name' => 'required', 
]); 

現在,內部自定義的驗證語言文件validation.php,我能夠嵌套屬性不知何故?像:

'required' => ':attribute is mandatory', 

'attributes' => [ 
    'invoiceAddress' => [ 
     'name' => 'blahblah' 
    ], 
], 

如果我嘗試拼圖屬性上面的方式,我得到

ErrorException 
    mb_strtoupper() expects parameter 1 to be string, array given 

因爲我使用的字段(如上)

['name' => 'blahblah'] 

我想使用該文件和:attribute指令(如上面的代碼中所述)獲取自定義消息。

我基本上是試圖做到這一點How to set custom attribute labels for nested inputs in Laravel,但我得到了提到的錯誤...

預先感謝您...

+0

什麼是validation.php你試圖實現的是你在驗證的消息中有問題嗎? –

+0

validation.php是我的自定義驗證語言文件,我試圖使用已定義的屬性和':attribute'指令來獲取消息...而不是定義數百個自定義消息 –

+0

此問題依然存在,您是否找到任何有效的解決方案? – Desh901

回答

1

一個注嵌套屬性

如果您的HTTP請求中包含「嵌套」參數,您可以在您的驗證規則中使用它們指定「點」語法:

$this->validate($request, [ 
    'title' => 'required|unique:posts|max:255', 
    'author.name' => 'required', 
    'author.description' => 'required', 
]); 

參考:https://laravel.com/docs/5.3/validation#quick-writing-the-validation-logic(關於嵌套屬性部分的註釋)

+0

我很抱歉,我忘了具體的目標 - 我想使用自定義驗證語言文件...並使用文件中的':attribute'指令來獲取自定義消息... –

+0

參考:[https:// laravel。 com/docs/5.3/validation#custom-error-messages] –

+0

那麼你已經解決了自定義驗證信息還是需要幫助? –