简体   繁体   中英

Different method called when using Laravel form request validation

I have a controller ( API\\Fields ) with a method named store , the route to that method is set up like this:

POST /api/templates/{template}/fields -> API\\Fields@store

Everything worked properly until I created a very simple form request validation class with the following rules (This is the only thing I changed besides the return value for the authorize method) :

    return [
        'name'          =>  ['required', 'alpha_num'],
        'coordinates'   =>  ['required', 'json'],
        'type'          =>  ['required', BaseField::RULE],
        'page'          =>  ['required', 'numeric'],
        'readonly'      =>  ['sometimes', 'boolean'],
        'required'      =>  ['sometimes', 'boolean']
    ];

After I created the class, I simply changed the request class from Request , to CreateFieldsRequest and it messed pretty much the whole routing for that route up. Instead of calling store , Laravel seems to be calling index . When I restore CreateFieldsRequest back to just the Request class, it behaves as it should again.

I haven't been able to find any information on this topic, and I've verified over and over that I don't have some sort of incorrect routing or redirections on any of the related classes.

Any help or guidance with this would be greatly appreciated, thank you!

After running a very simple test I realized that this seems to be an issue with Postman . If you are experiencing this issue stick to adding a _method=POST parameter on your POST body, or simply use XHR or a different API testing tool.

Edit: After further testing I realized the issue had not been fixed. When I run the request through the Chrome developer console as a POST request, Laravel kicks it back as a "GET" request, not sure why.

When I run the request through the Chrome developer console as a POST request, Laravel kicks it back as a "GET" request, not sure why.

A FormRequest that fails validation issues a redirect . It's the default behavior.

If you issue an AJAX request, or request a JSON response with an Accept header, it'll respond with a JSON list of validation errors and a 422 HTTP code instead .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM