Using onSubmit tag in form_tag?


#1

Hi guys,

I have been working on a form that would use javascript to validate the
data before submitting it, and from my view.rhtml I have a statement
like this:

However such a statement generates a HTML tag that looks like this:

Seems like Rails thinks it is a parameter instead of an option, so what
I tried next was something like:

<%= start_form_tag {:action => ‘Add’}, {:onSubmit => ‘validate()’} %>

Sadly that doesn’t compile. From rubydocs the syntax for form_tag is
form_tag(url_for_options = {}, options = {}, *parameters_for_url), so
how do I specify the options that I’d want as in this case?

Thanks!


#2

Marshall Roch wrote:

Woei S. wrote:

<%= start_form_tag {:action => ‘Add’}, {:onSubmit => ‘validate()’} %>

Sadly that doesn’t compile. From rubydocs the syntax for form_tag is
form_tag(url_for_options = {}, options = {}, *parameters_for_url), so
how do I specify the options that I’d want as in this case?

Just had the same problem. You need to use parentheses around all of
the arguments:

<%= start_form_tag( {:action => ‘Add’}, {:onSubmit => ‘validate()’} ) %>


Marshall Roch

P.S. I realize this is old, but I found it so I figured I’d pass it on
for others.

Correct. The problem is ruby has a hard time distinguishing between
passed hashes, and associated blocks tacked onto method calls. Using
parens fixes the issue since hashes would be isnside the parens, and
blocks would be outside.


#3

Woei S. wrote:

<%= start_form_tag {:action => ‘Add’}, {:onSubmit => ‘validate()’} %>

Sadly that doesn’t compile. From rubydocs the syntax for form_tag is
form_tag(url_for_options = {}, options = {}, *parameters_for_url), so
how do I specify the options that I’d want as in this case?

Just had the same problem. You need to use parentheses around all of
the arguments:

<%= start_form_tag( {:action => ‘Add’}, {:onSubmit => ‘validate()’} ) %>


Marshall Roch

P.S. I realize this is old, but I found it so I figured I’d pass it on
for others.


#4

hi when i do this…

<%= start_form_tag( {:action => ‘Add’}, {:onSubmit => :confirm=>‘are you
sure’} ) %>

the popup appears but both the ok and cancel buttons, no matter which
you you select, submits the form…

how can i make it so that if the user selects cancel or ok, that the
form does not get submit and enables the user to recheck the
information?

Alex W. wrote:

Marshall Roch wrote:

Woei S. wrote:

<%= start_form_tag {:action => ‘Add’}, {:onSubmit => ‘validate()’} %>

Sadly that doesn’t compile. From rubydocs the syntax for form_tag is
form_tag(url_for_options = {}, options = {}, *parameters_for_url), so
how do I specify the options that I’d want as in this case?

Just had the same problem. You need to use parentheses around all of
the arguments:

<%= start_form_tag( {:action => ‘Add’}, {:onSubmit => ‘validate()’} ) %>


Marshall Roch

P.S. I realize this is old, but I found it so I figured I’d pass it on
for others.

Correct. The problem is ruby has a hard time distinguishing between
passed hashes, and associated blocks tacked onto method calls. Using
parens fixes the issue since hashes would be isnside the parens, and
blocks would be outside.


#5

had a hunt around, seems you might need a ‘return’ for your validation.

something like…

<%= form_tag( {:action => ‘Add’}, {:onSubmit => ‘return validate();’} )
%>

then in your validate(); javascript function you can put the validation
code, returning true if ok, and false if not.