我一直在尝试在我的表单中进行一些 ajax 验证,但遇到了一些麻烦
我的表单中有这样的代码:
<% 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,这会很好地显示错误,但我不确定如何通过 ajax 显示和消失“error_message_on”部分,如果可能的话,通过 onblur 操作。
我的控制器工作正常,所以我不会将代码放在这里。
我想知道是否有人可以在这里填写空白:
在 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;
}