I''ll try and simplify this as best I can. I have a page layed out as follows: ----- | A | ----- | B | ----- Div A is a list of items, Div B is an Ajax-enabled form. I understand how to submit the form ''B'' and update list ''A'' with the results. What I can''t seem to do is handle validation errors. On an error (any error that invalidates the model), I would like ''B'' to display the form with errors (just like standard scaffolding). Upon success, I would like ''A'' to be updated. Anyone know how to accomplish this without patching Rails? Am I missing something? Thanks as always, Michael
Michael Gorsuch
2005-Jun-28 02:10 UTC
Re: Handling Validation Errors With Ajax Enabled Forms
OK.. so I think I''ve stumbled upon a conceptual solution, but I need someone with a better understanding of javascript to tell me if I''m on the right track. After processing, Div B will use ''complete'' and call a custom javascript function to investigate the contents of Div B. If there is a validation error, we hang tight. If there is no error, we clear the form (or whatever we want to do) and insert the appropriate html in A by using the prototype functions. So... does anyone know how to easily use javascript to detect a validation error within the returned content? On 6/27/05, Michael Gorsuch <michael.gorsuch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ll try and simplify this as best I can. I have a page layed out as follows: > > ----- > | A | > ----- > | B | > ----- > > Div A is a list of items, Div B is an Ajax-enabled form. > > I understand how to submit the form ''B'' and update list ''A'' with the results. > > What I can''t seem to do is handle validation errors. > > On an error (any error that invalidates the model), I would like ''B'' > to display the form with errors (just like standard scaffolding). > Upon success, I would like ''A'' to be updated. > > Anyone know how to accomplish this without patching Rails? Am I > missing something? > > Thanks as always, > > Michael >
Michael Champanis
2005-Jun-28 14:15 UTC
Re: Re: Handling Validation Errors With Ajax Enabled Forms
> OK.. so I think I''ve stumbled upon a conceptual solution, but I need > someone with a better understanding of javascript to tell me if I''m on > the right track. > > After processing, Div B will use ''complete'' and call a custom > javascript function to investigate the contents of Div B. If there is > a validation error, we hang tight. If there is no error, we clear the > form (or whatever we want to do) and insert the appropriate html in A > by using the prototype functions. > > So... does anyone know how to easily use javascript to detect a > validation error within the returned content?The solution that I''ve come up with is to put: <% if defined? @object %> <%= error_messages_for ''object'' %> <% end %> at the bottom of "A" (so it looks like it''s the top of "B"). Then when you submit and it fails, "A" will update and show the errors, and the item won''t be added to the list. The problem I had was clearing the form once a record had been successfully saved - I have since written http://www.sinner.co.za/articles/2005/06/28/returning-js-in-a-partial describing how I fixed that - basically: controller: if @object.save flash[:saved] = ''true'' end partial: <% if @flash[:saved] == ''true'' %> <script>document.forms[0].reset();</script> <% end %> There seem to be quite a few patches adding more elegant solutions to rails, I hope that they will be integrated into 0.13... Michael