2010-01-21 18 views
1

我一直試圖讓得到一些AJAX驗證要在我的表格,並已運行到有點麻煩的Rails:Ajax表單驗證與error_message_on

我有這樣的代碼在我的形式:

<% form_for @user, :html => { :id => 'user_form', :multipart => true } do |f| %> 
    <%= f.label :username %><br /> 
    <%= f.text_field :username %> <%= error_message_on :user, :username %> 
    </p> 
    <p> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> <%= error_message_on :user, :email %> 
    </p> 
    #other form fields (password and confirmation) 
    Type the words below to prove you aren't a robot: <%= recaptcha_tags %> 
    <%= error_message_on :user, :base %> 
    <p><%= f.submit "Submit" %></p> 
<% end %> 
<%= observe_form "user_form", :url => users_path %> 

如果我不使用ajax,這會顯示錯誤,但我不確定如何通過可能的onblur操作使「error_message_on」部分通過ajax顯示並消失。

我的控制器工作正常,所以我不會在這裏放置代碼。

我想知道如果任何人都可以在這裏填空:

在validate.js.rjs:

#enables and disables the submit button on the bottom 
if @user.valid? 
    page[:user_submit].enable 
else 
    page[:user_submit].disable 
end 
page.select('.formError').each(&:remove) 
#Some code to add the form error 
#Some code to highlight the error field 

只給你什麼,我試圖做一個直觀的想法,這裏是我的CSS:

.formError { 
    color: #D00; 
    font-size: 12px; 
    font-weight: bold; 
} 

.fieldWithErrors input { 
    border: 5px solid #f66; 
} 

回答

1

我的第一條建議供你會拋棄軌形成的幫手,並開始使用formtastic(http://github.com/justinfrench/formtastic)。我保證你滿意,或者你退錢。

喝完酒後,您的下一個停靠港應該是有效的(http://github.com/grimen/validatious-on-rails/)。它與formtastic(或任何形式的建設者)在這方面效果非常好,並且可以幫助您避免重蹈覆轍。

我希望這有助於!

+0

謝謝!我會檢查出來的! –