php - custom validation error format laravel 5.2 -
i want custom validation json response api. right default error message this
{"password":["the password must @ least 6 characters."],"type":["the type field required."]}
i want
{flag:0,msg:"the password must @ least 6 characters.",data:{"password":["the password must @ least 6 characters."],"type":["the type field required."]}}
and want format rest api only. how can achieve it.
i found solution.
i chnaged parent controller , added formatvalidationerrors method. here is
namespace app\http\controllers; use illuminate\foundation\bus\dispatchesjobs; use illuminate\routing\controller basecontroller; use illuminate\foundation\validation\validatesrequests; use illuminate\foundation\auth\access\authorizesrequests; use illuminate\foundation\auth\access\authorizesresources; use illuminate\contracts\validation\validator; class controller extends basecontroller { use authorizesrequests, authorizesresources, dispatchesjobs, validatesrequests; protected function formatvalidationerrors(validator $validator) { $res = $validator->errors()->all(); if(\request::wantsjson()) { $res = ["flag" => 0, "msg" => $validator->errors()->first()]; $res['data'] = $validator->errors()->all(); } return $res; } }
Comments
Post a Comment