form remote tag :before=> function problem

Hi I have a strange problem, I am using the below code

form_remote_tag :url => '/abc/test',:before => "return hello();", :update=>'test_categories' ,:complete => "Element.hide('spinner')", :loading => "Element.show('spinner')" do

I am using :before=> function to validate the fields in the form, but the problem is that it does not update the div test_categories, instead opens as a separate page like regular form. If I remove this :before=> all works fine.

the function hello is below and just a normal js function hello()   {

    if(document.getElementById("card_no").value=="")     {       alert("Card not captured, please try again")       document.getElementById("card_no").focus()       return false;     }     return true;   }

Any Ideas?

Hi,

Did you remember to include the relevant JavaScript files e.g <%= javascript_include_tag :defaults %>

Floyd Joseph wrote:

Hi,

Did you remember to include the relevant JavaScript files e.g <%= javascript_include_tag :defaults %>

Yes thats included, else form_remote_tag would not have worked upon removing :before=>

It works fine if i dont include the :before=> function , if I include it doesn work

Is it because you've got return then the function name? What does that function itself return?

Blog: http://random8.zenunit.com/ Twitter: http://twitter.com/random8r Learn: http://sensei.zenunit.com/ New video up now at http://sensei.zenunit.com/ real fastcgi rails deploy process! Check it out now!

The other thing i'd suggest is to look up the API for form remote tag and ensure that you explicitly put all your brackets, parenthesis and braces... I think ruby might be interpreting your URL string as a hash... Can you look at the HTML source generated and email it to us? Thanks

Blog: http://random8.zenunit.com/ Twitter: http://twitter.com/random8r Learn: http://sensei.zenunit.com/ New video up now at http://sensei.zenunit.com/ real fastcgi rails deploy process! Check it out now!

Hi again,

The problem is that before => does not prevent form submission and Julian was right about the inclusion of the return. However removing the return runs the JavaScript hello() function but does not cancel form submission.

Instead you should use the :condition => filter;

:condition => "$('card_no').value != ''",

This will check the value of card_no and if empty will not submit the form, otherwise the form is submitted and the test_categories div updated. If any of the JavaScript code is invalid then a standard http request will be made. This is why it opens a separate page like regular form.