# Form\_for

**URL:** <https://www.ruby-forum.com/t/form-for/173176>\
**Category:** Rails\
**Created:** [August 4, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/form-for/173176 "2009-08-04T13:38:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/49beb7/32.png) [@fisa](https://www.ruby-forum.com/u/fisa)\
**Post date:** [August 4, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/form-for/173176/1 "2009-08-04T13:38:38Z")

</div>

hi everybody…  
I have a form\_for in that, onclicking submit button i am calling  
a javascript for validation.If validation fails that form\_for doesnt do  
anything.But even the validation fails it goes to the controller and  
action.  
here my code:  
\<%form\_for :promotion\_code,  
:url=\>{:controller=\>“home”,:action=\>“create”,:discount\_id=\>product.discount.id,:id=\>@ad.id}  
do |f|%\>  
\<%=f.radio\_button :isemail,“true”,:checked=\>true%\> **Email**  
   
\<%=f.radio\_button :isemail, “false”%\> **Sms**  
\<%=f.text\_field :email\_or\_mobile,:size=\>20%\>  
\<%=f.submit “Generate”, :onclick=\>“ValidateForm()”%\>  
\<%end%\>

on validation failure it doesn’t go to the :action

please help me…

---

<div class="post-metadata">

**Author:** ![fisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/49beb7/32.png) [@fisa](https://www.ruby-forum.com/u/fisa)\
**Post date:** [August 4, 2009, 4:16pm UTC](https://www.ruby-forum.com/t/form-for/173176/2 "2009-08-04T16:16:10Z")

</div>

You need to move the :onclick =\> “ValidateForm()” to :onsubmit =\>  
“ValidateForm()” in the form\_for line. Then when the ValidateForm  
returns false it will halt submission of the form.

You might want to consider using something like this:  
[http://www.livevalidation.com/](http://www.livevalidation.com/)

There’s a Rails plugin as well:

> **[GitHub - porras/livevalidation: Client-side validations for Ruby on Rails (using...](https://github.com/porras/livevalidation/tree/master)**
>
> master

Cheers,  
Nicholas

---

<div class="post-metadata">

**Author:** ![fisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/49beb7/32.png) [@fisa](https://www.ruby-forum.com/u/fisa)\
**Post date:** [August 4, 2009, 4:24pm UTC](https://www.ruby-forum.com/t/form-for/173176/3 "2009-08-04T16:24:57Z")

</div>

On Tue, Aug 4, 2009 at 4:38 AM, Md Fisa  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> do |f|%\>  
> \<%=f.radio\_button :isemail,“true”,:checked=\>true%\> **Email**  
>    
> \<%=f.radio\_button :isemail, “false”%\> **Sms**  
> \<%=f.text\_field :email\_or\_mobile,:size=\>20%\>  
> \<%=f.submit “Generate”, :onclick=\>“ValidateForm()”%\>  
> \<%end%\>
> 
> on validation failure it doesn’t go to the :action
> 
> please help me…

Hi, I think that you really want the onclick handler on the form element  
instead of the  
input tag. Then the following should work for you:

\<%form\_for :promotion\_code,  
:url =\>  
{:controller=\>“home”,:action=\>“create”,:discount\_id=\>  
product.discount.id,:id=\>@ad.id },  
:html =\> { :onclick =\> “return ValidateForm();” } do |f|%\>

\<%=f.radio\_button :isemail,“true”,:checked=\>true%\> **Email**  
\<%=f.radio\_button :isemail, “false”%\> **Sms**  
\<%=f.text\_field :email\_or\_mobile,:size=\>20%\>  
\<%=f.submit “Generate”, :onclick=\>“ValidateForm()” %\>

\<%end%\>

Lastly, you can read more on the use of the :html symbol by referring to  
section 23.5 of AWDwR 3ed.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![fisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/49beb7/32.png) [@fisa](https://www.ruby-forum.com/u/fisa)\
**Post date:** [August 5, 2009, 6:34am UTC](https://www.ruby-forum.com/t/form-for/173176/4 "2009-08-05T06:34:30Z")

</div>

Nicholas H. wrote:

> You need to move the :onclick =\> “ValidateForm()” to :onsubmit =\>  
> “ValidateForm()” in the form\_for line. Then when the ValidateForm  
> returns false it will halt submission of the form.
> 
> You might want to consider using something like this:  
> [http://www.livevalidation.com/](http://www.livevalidation.com/)
> 
> There’s a Rails plugin as well:  
> [GitHub - porras/livevalidation: Client-side validations for Ruby on Rails (using javascript library livevalidation.com)](http://github.com/porras/livevalidation/tree/master)
> 
> Cheers,  
> Nicholas

Thanks for yor reply …  
I applied what you said.But its not getting.When i am giving :onsubmit  
it doesn’t check for validation.It directly go to the action without  
doing any validation.When validation return false it shouldn’t execute  
the action.Your reply appreciated…Thanks verymuch

---

<div class="post-metadata">

**Author:** ![fisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/49beb7/32.png) [@fisa](https://www.ruby-forum.com/u/fisa)\
**Post date:** [August 5, 2009, 9:34am UTC](https://www.ruby-forum.com/t/form-for/173176/5 "2009-08-05T09:34:05Z")

</div>

On Tue, Aug 4, 2009 at 9:34 PM, Md Fisa  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > [GitHub - porras/livevalidation: Client-side validations for Ruby on Rails (using javascript library livevalidation.com)](http://github.com/porras/livevalidation/tree/master)
> > 
> > Cheers,  
> > Nicholas
> 
> Thanks for yor reply …  
> I applied what you said.But its not getting.When i am giving :onsubmit  
> it doesn’t check for validation.It directly go to the action without  
> doing any validation.When validation return false it shouldn’t execute  
> the action.Your reply appreciated…Thanks verymuch

Hi, please post the Javascript that’s performing the validation?

-Conrad

---

<div class="post-metadata">

**Author:** ![fisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/49beb7/32.png) [@fisa](https://www.ruby-forum.com/u/fisa)\
**Post date:** [August 4, 2009, 4:25pm UTC](https://www.ruby-forum.com/t/form-for/173176/6 "2009-08-04T16:25:32Z")

</div>

On Tue, Aug 4, 2009 at 7:16 AM, Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > \<%form\_for :promotion\_code,  
> > on validation failure it doesn’t go to the :action  
> > product.discount.id,:id=\>@ad.id },  
> > section 23.5 of AWDwR 3ed.
> 
> Good luck,
> 
> -Conrad
> 
> >

The correct javascript handler to use would be onsubmit as Nicholas  
Henry  
recommends. Thus, the above  
code has been corrected with this in mind as well as removing the typo.

-Conrad

---

<div class="post-metadata">

**Author:** ![fisa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/49beb7/32.png) [@fisa](https://www.ruby-forum.com/u/fisa)\
**Post date:** [August 5, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/form-for/173176/7 "2009-08-05T15:26:26Z")

</div>

And please post the form\_for code.

Thank you.
