Hi What''s the best way to handle model validations with an AJAX form? E.g, I have a form that allows you to add an item to a list. Each item has a name and a description. My model validations for the item state that the name needs to be unique, and between 3 and 255 characters, and that the description must be at least 30 characters. Obviously I can handle the length checks with some client side javascript, but I''d also like to enforce the rules on the server-side too to ensure database consistency. What would be the best way to handle the errors returned from the model''s validation, preferably allowing me to display something like the "errors for @item" helper could for a non-AJAX form? I know about the :failure option for the form_remote/link_to_remote, but do not really know how this relates to specific errors such as validations, and how I would trigger the failure on the serverside. Regards Elliot
Hi,>What would be the best way to handle the errors returned from the >model''s validation, preferably allowing me to display something like >the "errors for @item" helper could for a non-AJAX form? > >I know about the :failure option for the form_remote/link_to_remote, >but do not really know how this relates to specific errors such as >validations, and how I would trigger the failure on the serverside. > >Some people like to return "custom" HTTP status codes and match them to error messages. I had the same problem and messing around with HTTP status codes just felt wrong to me. As the X in Ajax stands for XML, it seemed a good idea to generate some XML for this task. In my case I wanted to update a select list and wanted a "cleaned" version of the entered data in the list (trimmed, special chars removed etc. anything that''s done before saving to the DB). So the benefit wasn''t just the server-side validation. The encoding method of XML-RPC seemed convenient. It''s quite easy to do with Builder templates and the JavaScript DOM functions. Here''s a small howto: http://people.freenet.de/schoefmax/howto/ajax_responses_with_xmlrpc/ HTH, Max