# Form remote tag :before=\> function problem

**URL:** <https://www.ruby-forum.com/t/form-remote-tag-before-function-problem/188086>\
**Category:** Rails\
**Created:** [May 5, 2010, 3:04pm UTC](https://www.ruby-forum.com/t/form-remote-tag-before-function-problem/188086 "2010-05-05T15:04:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shanker](https://www.ruby-forum.com/u/shanker)\
**Post date:** [May 5, 2010, 3:04pm UTC](https://www.ruby-forum.com/t/form-remote-tag-before-function-problem/188086/1 "2010-05-05T15:04:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shanker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shanker](https://www.ruby-forum.com/u/shanker)\
**Post date:** [May 5, 2010, 5:41pm UTC](https://www.ruby-forum.com/t/form-remote-tag-before-function-problem/188086/2 "2010-05-05T17:41:36Z")

</div>

Hi,

Did you remember to include the relevant JavaScript files e.g  
\<%= javascript\_include\_tag :defaults %\>

---

<div class="post-metadata">

**Author:** ![shanker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shanker](https://www.ruby-forum.com/u/shanker)\
**Post date:** [May 5, 2010, 6:33pm UTC](https://www.ruby-forum.com/t/form-remote-tag-before-function-problem/188086/3 "2010-05-05T18:33:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shanker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shanker](https://www.ruby-forum.com/u/shanker)\
**Post date:** [May 5, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/form-remote-tag-before-function-problem/188086/4 "2010-05-05T18:51:18Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![shanker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shanker](https://www.ruby-forum.com/u/shanker)\
**Post date:** [May 5, 2010, 9:33pm UTC](https://www.ruby-forum.com/t/form-remote-tag-before-function-problem/188086/5 "2010-05-05T21:33:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shanker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@shanker](https://www.ruby-forum.com/u/shanker)\
**Post date:** [May 5, 2010, 6:55pm UTC](https://www.ruby-forum.com/t/form-remote-tag-before-function-problem/188086/6 "2010-05-05T18:55:24Z")

</div>

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/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)  
Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
New video up now at [http://sensei.zenunit.com/](http://sensei.zenunit.com/) real fastcgi rails deploy  
process! Check it out now!
