mardi 28 août 2018

Laravel Custom Error Validation JSON Response Object to Array

I try to create API for registration form if user not fill the required field. The validator show error in object format but i need json response in array format.

$validator = Validator::make($request->all(), [ 
    'name' => 'required', 
    'mobile' => 'required', 
    'address' => 'required', 
]);
if ($validator->fails()) { 
    return response()->json(['error'=>$validator->errors()], 401);            
}

Current output is

 {
    "error": {
        "name": [
            "The name field is required."
        ],
        "mobile": [
            "The mobile field is required."
        ],
        "address": [
            "The addressfield is required."
        ]
    }
}

Expected output

{
  "error": {
             [
               "The name field is required.",
               "The mobile field is required.",
               "The addressfield is required."
             ]
           }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire