views:

146

answers:

3

How do i introduce validation before remote_form_for submits?

I have javascript function called validateForm(). I am able to call it before the AJAX request process. I tried to use return false and event.preventDefault. But there seems to be no effect. Here is what my code looks like

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", :url => {:action => "login", :controller => "site"},  :onsubmit => "validateForm(event)" do |f| %>
    User name : <%= f.text_field "uxUserName", :class => "TextBox", :style => "width:100px;" %>&nbsp;*
    &nbsp;Password : <%= f.password_field "uxPassword", :class => "TextBox", :style => "width:100px;" %>&nbsp;*
    <%= f.submit "Go!", :class => "Button-Simple", :id => "uxSubmitButton" %>   
<% end %>

the javascript function is simple as follows

function validateForm(event){
   return false;
   //event.preventDefault();
}
+1  A: 

I think what you're looking for is something like onSubmit="return validateForm(this)" or thereabouts. Then if the validation returns false (because it failed validation) the form should not submit.

edl
i tried it earlier.. it has a problem... if the return value is true the form submits directly and is no longer an AJAX request
ZX12R
If you are just trying to do a pure AJAX request, a submit button is unnecessary. You can just use a plain "button" type input like <input type="button" id="blah" /> and use an onClick handler.
edl
+1  A: 

Try this

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", :url => {:action => "login", :controller => "site"}, :html => {:id => "uxLoginForm"}, :onsubmit => "return validateForm(event)" do |f| %>

i.e. change

 :onsubmit => "validateForm(event)

to

 :onsubmit => "return validateForm(event)

EDITED it should be

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", 
         :url => {:action => "login", :controller => "site"}, 
         :html => {:id => "uxLoginForm", :onsubmit => "return validateForm(event)"} 
         do |f|
 %>

EDITED AGAIN

WHY don't you use :condition for it? something like following

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", 
         :url => {:action => "login", :controller => "site"}, 
         :html => {:id => "uxLoginForm"},
         :condition=>"validateForm(event)!=false"
         do |f|
 %>
Salil
using return validateForm(event) only makes it a usual form and makes the form behaving with ajax request
ZX12R
please check my "EDITED AGAIN" and let me know what happens.
Salil
+1  A: 

Hi ZX12R,

You might want to check LIvevalidation plugin (http://github.com/augustl/live-validations) which you can you to do Ajax with active record validations

And its always good to have non-ajax validations also (even though you use ajax validations) as ajax will not work in javascript disable browser

cheers, sameera

sameera207
thanks. will check it out
ZX12R
Good tip: i am already using the jquery.validate plugin for this; but had to manually copy my validations.
nathanvda