Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC form:errors using Ajax

I have a question related to Spring 3 MVC, Ajax, and form:errors.

I have a form I am submitting to the server and I am using Spring MVC. I have all the validation, controllers, etc set up and working fine. When I submit data that fails validation, I get the error message coming back and being displayed on the form as per my form:errors tags in my jsp.

My question is...

If I want to convert over to an Ajax approach, can I still take advantage of all the infrastructure of a "normal" Spring MVC app (ie, specifically the validator and form:errors features) to get the errors back to (and displayed on) the browser? I see how to submit Ajax requests, but I don't see how (or if) I can still utilize the form:errors capability of the jsp.

Does anyone know if this can be done, and if so, point me in the appropriate general direction?

Thanks in advance.

like image 596
Dan Avatar asked May 17 '11 18:05

Dan


1 Answers

You can't, since tags like <form:errors> are evaluated server-side and embed error messages/elements in the HTML content of the HTTP response. Since an AJAX request to the server typically returns XML or JSON to a response (and not HTML), which is then parsed by client-side JavaScript, there is no point at which Spring can bind to the request/form and add error messages to the content.

like image 182
matt b Avatar answered Sep 21 '22 15:09

matt b