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;
}
謝謝!我會檢查出來的! –