4

I'm building a REST API with Laravel and wondering if there is a way to customize the API responses when validating.

For example, I have a validation rule in a Laravel request, saying a specific field is required.

public function rules() {
   return [
       'title'=>'required|min:4|max:100',
   ];
}

So, for this validation I get the error message in Postman like this

{
    "title": [
        "Please enter Ad Title"
    ]
}

What I want is to customize that response like this..

{
    "success": false,
    "message": "Validation Error"
    "title": [
        "Please enter Ad Title"
    ]
}

So, that the error is more specific and clear.

So, how to achieve this ?

Thanks!

Tharindu Thisarasinghe
  • 3,846
  • 8
  • 39
  • 70
  • You don't show *where* you're validating, but you can try parsing the validation response before sending it back. – aynber Jan 08 '18 at 18:37

3 Answers3

5

I got a solution for your REST-API Validation Laravel FormRequest Validation Response are change by just write a few lines of code. enter code here

Please add this two-line into your App\Http\Requests\PostRequest.php

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

after that add this function in your file.

you can change $response variable into your specific manner.

protected function failedValidation(Validator $validator) { 
        $response = [
            'status' => false,
            'message' => $validator->errors()->first(),
            'data' => $validator->errors()
        ];
        throw new HttpResponseException(response()->json($response, 200)); 
    }
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
Keyur Patel
  • 61
  • 1
  • 5
2

Provide a custom function to the FormRequest class called messages and return an array of validation messages mapped using dot notation for specific messages on specific rules:

public function messages()
{
    return [
        'title.required' => 'Please enter an Ad title',
        'title.min' => 'Your title must be at least 4 character'
    ]
}

Returning a success message is futile as if it fails a 422 error code will be thrown when performing an ajax request anyway.

As for the message property, you will receive that as part of the payload, wherein the actual validation errors will be contained in the object.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • Thanks! However, I'm already overriding messages method with custom error messages. The thing I was confused is the `success ` message. I thought that's a must in a API response in order to identify the status. – Tharindu Thisarasinghe Jan 08 '18 at 19:14
2

You can customize errors, check the documentation. also you can validate in this way

$validator = Validator::make($request->all(), [
        'title'=>'required|min:4|max:100'
    ]);

    if ($validator->fails()) {
        // get first error message
        $error = $validator->errors()->first();
        // get all errors 
        $errors = $validator->errors()->all();
    }

then add them to your response, for example

 return response()->json([
     "success" => false,
     "message" => "Validation Error"
     "title" => $error // or $errors
 ]);
devnull Ψ
  • 3,779
  • 7
  • 26
  • 43