So I currently validate my forms by doing:
$this->validate($request, [
'title' => 'required',
'content' => 'required|min:3'
]);
How do I take these validation errors and push them back through JSON? The docs state that $this->validate(...)
if false will redirect back and allow you to display errors ...
How do you validate server side for API requests?
You need to use Validator::make and then return response in json with array or your errors.
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails())
{
return Response::json(['errors' => $validator->getMessageBag()->toArray()], 200);
}
Note that you also need to setup $rules variable.
For example:
$rules = ['email' => 'required|email'];
Hope it will help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.