Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gputignano's avatar

Form Request Validation Redirect

How can I control the redirection after a Form Request Validation? Which is the class/method that make redirection?

0 likes
9 replies
tisuchi's avatar

What do you mean by control? What exactly you want to do after request validation?

Snapey's avatar

if validation fails then an exception is thrown. You would need to intercept that if you want to change default behaviour

gputignano's avatar

I'm usin AMP so my forms send a XHR request and wait for a JSON response and then eventually redirect. If I use Form Request Validation when the validation fails I get an error in AMP.

Snapey's avatar

trap the request in the form request and make sure that it expects json response.

eg

public function rules()
{

    dd(request()->wantsJson());

    return [....

You are expecting json I think - not 100% sure with AMP

gputignano's avatar

I read in documentation that for AJAX requests Form Request Validation already returns JSON. However I need return something like belove using Form Request Validation:

return response()->json([
    'aaa' => 'bbb'
])->withHeaders([
    'AMP-Access-Control-Allow-Source-Origin' => 'https://127.0.0.1',
    'Access-Control-Expose-Headers' => 'AMP-Access-Control-Allow-Source-Origin',
]);
Snapey's avatar

It only returns json if it recognises the request as ajax, which it does by checking the Accept header in the request.

gputignano's avatar

@SNAPEY - Where the default json response is created in code? And where can I intercept it to add headers?

Please or to participate in this conversation.