繁体   English   中英

如何在laravel中验证json数据?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM