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

6
  • 1
    The StorePostRequest will auto validate and send error response, you don't need to do $request->fails(). Check this. Commented May 1, 2018 at 18:38
  • @TheAlpha yes I know, I tried using without those also but it is not working as expected. Commented May 1, 2018 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. Commented May 1, 2018 at 18:41
  • 2
    You need to send an accept header, i.e: Accept: application/json. Commented May 1, 2018 at 18:48
  • 1
    @TheAlpha yaah that worked, thanks. Commented May 1, 2018 at 18:49

2 Answers 2

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

Comments

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));
}

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.