Topic: another question about validation...

I know this topic's been beaten a lot, but I can't find any good answers for my question.  I've created an ajax form, and when you click the 'save' button, my rjs file replaces the form with a blank space.  However, when validation fails, I want the form to stay and the validation errors to display. 

This may be a dumb question to some, but I'm still a little new.  I'm assuming when you create a new item using ajax, that the validation errors are not sent back to display as they normally do in non-ajax forms?  I had it working for non-ajax forms, and built upon it for my ajax forms.  I'm just looking for a little direction.  I can probably figure it out once I get started, but I'm scratching my head on where to start with this.  If anyone can give me a quick tip or point me to a decent source, I'd appreciate their help.  Thanks for any advice.

Re: another question about validation...

You just need to control the RJS that is returned - if the valdiation fails, don't respond with the "form swapping" message.

  if {MODEL}.save
    # display form swap RJS
  else d
    # display form validation message RJS

Toby Hede
FiniteStateMachine - Software Development for Social Networks

Re: another question about validation...

Toby, thanks for the reply.  That works in that the form stays there when validation fails, but I'm trying to display the validation error messages such as "username can't be blank", etc.  I know it's possible cause I've seen other ajax forms use this, but I'm not sure if they found a way to use rail's error messages, or if they had to create their own.  Any insight would be helpful.  And thanks again for your reply.

Re: another question about validation...

I don't think there is anything built into Rails to do this but it should be a simple case of looping through {MODEL}.errors and adding/showing elements on your form using page["element"].insert_html and page["element"].show.

Re: another question about validation...

you can add a hidden div (style="display: none;") in the form view with an id "error_div" and then render the error_messages_for into it and show the div

page[:error_div].replace_html render :text => "#{error_message_for :object}"
page[:error_div].visual_effect :appear

I'm really unsure weither or not you can use ActionView Helpers like error_messages_for in render :text. I *think i did this once but can't really remember weither or not it worked big_smile
If not, put that into a partial and use render :partial instead:

page[:error_div].replace_html render :partial => "shared/error_msgs", :locals => { :object => "object" }
# "object" should be the name of your instance variable you want the errors of. e.g. you have @post, the "object" should be "post"
page[:error_div].visual_effect :appear

<%= error_messages_for object %>

if that's not suited for your case, you can just loop thorugh object.errors (like said in the above post), and put that code into the partial.

Last edited by Duplex (2007-08-27 09:51:05)

Re: another question about validation...

Thanks guys for your help.  That should solve the problem.  smile