php - Submit a Form using PUT instead of POST -


I have a controller that presents the form with AJAX requests. I do not want to repeat myself, so I put the processing code in some method:

  // post request public work should be made (request $ request) {return $ the-> and; ProcessEdit ($ request); } // PUT request should process public function updates ($ id, $ request request) {$ entity = $ this- & gt; Search Entity ($ id); // Custom method if (! $ Unit) returned $ $- & gt; MyCustomErrorResponse (); $ $ Return - process edit ($ request, $ unit); } Private Function Process Edit (Request $ request, unit $ unit = zero) {$ form = $ this- & gt; CreateForm ('my_entity', $ unit); $ Form & gt; handleRequest ($ request); If ($ form-> isValid ()) {// do something} other {// handle invalid form} return $ response; }   

I have the following two routes:

  ajax_create: pattern: / defaults: {_controller: 'MyBundle: Ajax: create'} Methods: [ POST] ajax_update: pattern: / id} default: {_controller: 'MyBundle: Ajax: update'} Methods: [PUT] Requirements: ID: \ d +   

However, when I Submitting the form via AJX, this will not accept the PUT request and without the form form message the return form is not valid. If I change the controller code abit,

  $ form = $ this- & gt; createform ('my_entity', $ unit, array ('method' = & gt; 'PUT',));   

... This will act on the PUT request but will not process the POST request.

I wonder what part of Symfony2 does an HTTP method check for a form, so I tried to see the answer in the source code, but I could not find any clue. Can any of you share your knowledge?

Another question, is there any way to bypass the check of HTTP method? I am currently passing in the way shown above of $ method .

Thank you very much.


Update:

To clarify my question, my Simophony 2 application routes requests (both post and put) in the right control method.

I mentioned the above code, here it is:

  // Post requests should create public work (request $ request) {return $ this- & gt; Edit process ($ request); } // PUT request should process public function updates ($ id, $ request request) {$ entity = $ this- & gt; Search Entity ($ id); // Custom method if (! $ Unit) returned $ $- & gt; MyCustomErrorResponse (); $ $ - Return-> Process Edit ($ request, 'Put', $ unit); } Private function process edit (request $ request, $ method = 'POST', unit $ unit = zero) {$ form = $ this-> createForm ('my_entity', $ entity, array ('method' = & gt; $ method,)); $ Form & gt; handleRequest ($ request); If ($ form-> isValid ()) {// do something} other {// handle invalid form} return $ response; }    

Just a few (expected) useful notes:

Secondly, I think you gave me Looking for the found piece of code found First of all, if you check the handleRequest call in the Symphony square, you can see that it is located in the handleRequest from RequestHandler class in the config Calls (check FormConfigInterface category). I'm feeling that the correct implementation of RequestHandlerInterface is that you can check on the line for the similarity of requested methods there.

Now, to handle it, you can set a form of form to a custom value, where you define RequestHandler as your own implementation if NativeRequestHandler as a service You are in luck (currently I do not have access to the list of services). Just switch the class for your own implementation.

After saying all this, I think the type of form testing is for a reason. Now, what you are doing, you submit your form in different ways. In addition, using POST to insert and editing is a great solution to present a new bug, better, less chance!

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -