0
我有一個AJAX郵件形式等如何驗證Ajax表單提交(remote_form_tag)?
- form_remote_tag :url=>mails_path,:condition=>"validate_mail()", :html => {:id=>"mailform",:method => :post, :class => 'ajax',:style=>"padding:15px;" } do |form|
.gimmespace
Naam
%br
= text_field_tag :name,params[:name],:class=>"title required"
.gimmespace
Telefoonnummber
%br
= text_field_tag :phone,params[:phone],:size=>25,:class=>"title"
.gimmespace
Mailadres
%br
= text_field_tag :email,params[:email],:size=>30,:class=>"title required"
.gimmespace
Onderwerp
%br
= text_field_tag :subject,params[:subject],:class=>"title required"
.gimmespace
Boodschap
%br
= text_area_tag :message,params[:message],:rows=>10,:cols=>45,:class=>"title required"
.gimmespace
= submit_tag "Verstuur",:id=>"mailsubmit",:class=>"sendBtn"
%button{:onclick=>"$.fn.colorbox.close();"} Annuleer
上面的代碼是在HAML。它使ajax表單提交給控制器。我必須在提交之前驗證這些字段。所以,我嘗試了幾個東西。我閱讀了這篇文章http://hillemania.wordpress.com/2006/09/18/rails-ajax-pre-submit-form-validation/並做了一個回調前的測試javascript函數來驗證。這是javascript驗證功能。
function validate_mail() {
alert("Your Name, Email, Subject and Body Content are Required !");
return false;
}
按照上面的函數,返回false任何方式和形式不應該提交,但其提交以及ajaxically。有沒有其他辦法,請幫助。
rails 2.3.8 docs說這個「:condition通過這個表達式有條件地執行遠程請求。當請求不應該被啓動時,用它來描述瀏覽器端的條件。」 ..我做到了,但仍然提交表單... :( – Anand 2010-08-12 04:24:22
你可以發佈'form_remote_tag'生成的代碼嗎? – nfm 2010-08-12 06:36:12