[英]How can I validate json data in laravel?
我的驗證數據的代碼是這樣的:
public function register(Request $request)
{
//echo '<pre>';print_r($request->all());echo '</pre>';die();
$this->validate($request, [
'email'=>'required',
'password'=>'required',
'data_cache.dataCache.id'=>'required|numeric'
'data_cache.dataCache.quantity'=>'required|numeric'
]);
}
echo '<pre>';print_r($request->all());echo '</pre>';die();
像這樣 :
Array
(
[data_cache] => {"dataCache":{"id":112,"quantity":1,"information":"stamford bridge","request_date":"15-08-2017 02:30:00"},"expired":"2017-08-14T16:30:26.272Z"}
[email] => chelsea@gmail.com
[password] => 87654321
)
data_cache是json數據
在視圖刀片上,我添加了它以顯示消息驗證:
@if ($errors->has('data_cache'))
<span class="help-block">
<strong>{{ $errors->first('data_cache') }}</strong>
</span>
@endif
如果執行了代碼,則沒有顯示消息驗證
似乎驗證json數據仍然錯誤
如何正確執行?
您可以嘗試使用...
$dataCache = json_decode($request->data_cache);
$request->merge(['id' => $dataCache.dataCache.id, 'quantity' => $dataCache.dataCache.quantity]);
然后繼續使用$ validate方法。
有一個字段檢查正在驗證的數據字段是否為有效的json字符串,但這不是您所需要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.