How do you work with forms in your web applications?
I am not talking about RESTful applications, I don't want to build heavy front-end using frameworks like Backbone.
For example, I need to add "contact us" form. I need to check data which was filled by user and tell him that his data was sent. Requirements:
- I want to use AJAX.
- I want to validate form on back-end side and don't want to duplicate the same code on front-end side.
I have my own solution, but it doesn't satisfy me. I make an AJAX request with serialized data on form submit and get response. The next is checking "Content-type" header.
- html -> It means that errors with filling form are exists and response html is form with error labels. -> I will replace my form with response html.
- json and response.error_code == 0 -> It means that form was successfully submited. -> I will show user notification about success.
- json and response.error_code != 0 -> Something was broken on back-end (like connection with database).
- other - I display the following message :
We have been notified and have started to work with that problem. Please, try it later.
The problem of that way is that I can't use it with forms that upload file.
What is your practise? What libraries and principles do you use?