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!

1
  • You don't show where you're validating, but you can try parsing the validation response before sending it back. Commented Jan 8, 2018 at 18:37

3 Answers 3

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)); 
    }
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
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
 ]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.