2016-03-02 138 views
0

如何使用自定義的請求驗證,我有一個數組密鑰請求如何使用自定義請求驗證laravel數組?

$request = [ 
    'link_inc_characteristic_id' => $inc_char_id[$i], 
    'value' => $value[$i], 
    'created_by' => $created_by, 
    'last_updated_by' => $last_updated_by, 
]; 

$this->validate($request, [ 
    'value['.$i.']' => 'max:30' 
]); 

$linkIncCharacteristicValue = LinkIncCharacteristicValue::create($request); 
return Response::json($linkIncCharacteristicValue); 

[編輯] [CODE增訂] 顯示錯誤:

Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given,

+0

您收到了哪些錯誤? –

+0

驗證不起作用,沒有錯誤.....輸入值自動裁減數據庫字段長度,從驗證沒有錯誤.. – Khoerodin

+0

我只是更新代碼..並得到錯誤 – Khoerodin

回答

0

它,因爲我把驗證內部循環,而不是使用*字符來驗證一個數組

... 
for ($i=0; $i < $count ; $i++) { 
    ... 
    $this->validate($request, [ 
     'value['.$i.']' => 'max:30' 
    ]); 
    ... 
    // save etc 
} 

正確的代碼,把確認循環前,並使用*字符進行數組驗證:

$this->validate($request, [ 
     'value.*' => 'max:30' 
]); 
... 
for ($i=0; $i < $count ; $i++) { 
    // save etc 
} 
1

Controller::validate()不是一個通用的驗證方法,它驗證請求。對於這種使用情況下,只需直接使用驗證:

Validator::make($data, ['value' => 'max:30']);