AJAX and Model Validations

Elliot Bowes
Mon, 15 Aug 2005 18:20:26 -0000

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