0

I'm have created a custom form request in my laravel 5.6 something like this:

<?php

namespace Noetic\Plugins\blog\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StorePostRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [

        ];
    }
}

When I do not place anything in rules, I get the controllers working, and when I put any rule inside suppose I put

return [
    'title' =>  'required',
    'body'  =>  'required',
];

It works until it gets validated true, I mean if title and body is passed it gets validated, but when I don't send any data for title or body I'm not getting errors as response, I see the home page belonging to web middleware, I want to return the error data as response.

My controller is something like this:

public function store( StorePostRequest $request )
{
    if ($request->fails()) {
        return $this->errorResponse($request->errors()->all());
    }

    $data = $request->only('title', 'body');

    $post = Post::create($data);

    return response()->json(['post'=> $post ],200);
}

Help me out with these. Thanks

Nitish Kumar
  • 6,054
  • 21
  • 82
  • 148
  • 1
    The `StorePostRequest` will auto validate and send error response, you don't need to do `$request->fails()`. [Check this](https://laravel.com/docs/5.6/validation#form-request-validation). – The Alpha May 01 '18 at 18:38
  • @TheAlpha yes I know, I tried using without those also but it is not working as expected. – Nitish Kumar May 01 '18 at 18:39
  • 1
    How are you sending the `ajax` request? If you are sending an `ajax` request then `Laravel` will send the errors as json, ptherwise you'll get a redirect response. So, make sure you are sending the request properly so `Laravel` can determine the type of response it should send. – The Alpha May 01 '18 at 18:41
  • @TheAlpha calling the routes in post method with headers defined as `Content-Type` as `application/json` I'm using `postman` to test the api. – Nitish Kumar May 01 '18 at 18:46
  • 2
    You need to send an `accept` header, i.e: `Accept: application/json`. – The Alpha May 01 '18 at 18:48
  • 1
    @TheAlpha yaah that worked, thanks. – Nitish Kumar May 01 '18 at 18:49

2 Answers2

1

In your controller function you don't need to catch the validation just try with success path.

Handler will handle your validation

public function store( StorePostRequest $request )
{
    $data = $request->only('title', 'body');

    $post = Post::create($data);

    return response()->json(['post'=> $post ],200);
}

In your Handler

use Illuminate\Validation\ValidationException;

if ($exception instanceof ValidationException)
{
    return response($exception->errors())->header('Content-Type', 'application/json');
}
Hamelraj
  • 4,676
  • 4
  • 19
  • 42
0

use Illuminate\Contracts\Validation\Validator;

use Illuminate\Http\Exceptions\HttpResponseException;

after that

protected function failedValidation(Validator $validator) {
    throw new HttpResponseException(response()->json($validator->errors(), 422));
}