1

我一直在尝试在我的表单中进行一些 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;
}
4

1 回答 1

1

我给你的第一条建议是放弃 rails form helpers 并开始使用 formtastic ( http://github.com/justinfrench/formtastic )。我保证您满意,否则退款。

在你喝完formtasticcoolaid之后,你的下一个停靠港应该是有效的(http://github.com/grimen/validatious-on-rails/)。它与 formtastic(或任何形式的构建器)一起工作得非常好,并且应该可以帮助您避免重新发明轮子。

我希望这有帮助!

于 2010-01-22T21:59:29.400 回答