How to overcome disadvantages of submitting form via ajax
I have a form which usually has a hash. I can submit the form the normal way and run the usual validation. If there are errors, the form re-renders and zend adds the usual errors. It seems that submitting forms with ajax (jQuery) means that I lose all this validation, which I'm sure was already addressed before. How do you get the same validation and handle the errors when doing ajax submissions?
I used to worry about the hash element, but what I discovered is that it seems to no开发者_JAVA百科t matter for ajax forms submissions. So that's one out of the way.
Handling validation with an AJAX for is a two-step process. First you should incorporate comprehensive frontend validation. This should alert the user to issues prior to actually making the AJAX call.
Second include standard validation on the server. If this fails you can simply generate a 404 not found or 400 bad request because someone is attempting to send data without going through the frontend validation. Note that this could cause issues if you need to do server side validation for some components (duplicate email addresses, usernames, etc). In that case you can send the errors back in your desired format (JSON and XML are very common) and render them frontend.
精彩评论