php - How can I validate json data in laravel? -
my code validate data :
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' ]); }
the result of 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 )
the data_cache json data
on view blade, add display message validation :
@if ($errors->has('data_cache')) <span class="help-block"> <strong>{{ $errors->first('data_cache') }}</strong> </span> @endif
if code executed, there no display message validation
seems validation json data still wrong
how can correctly?
you try using...
$datacache = json_decode($request->data_cache); $request->merge(['id' => $datacache.datacache.id, 'quantity' => $datacache.datacache.quantity]);
then continue $validate method were.
there field checks whether data field under validation valid json string or not, that's not need.
Comments
Post a Comment