Rails:使用 error_message_on 进行 Ajax 表单验证
我一直在尝试在我的表单中进行一些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;
}
I've been trying to get get some ajax validations going in my form and have been running into a bit of trouble
I have code like this in my form:
<% 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 %>
This displays the errors fine if I don't use ajax, but I'm not sure how to make the "error_message_on" part to appear and disappear through ajax, through an onblur action if possible.
My controller works fine so I won't place the code here.
I was wondering if anyone could fill in the blanks here:
in 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
Just to give you a visual idea of what I'm trying to do, here is my css:
.formError {
color: #D00;
font-size: 12px;
font-weight: bold;
}
.fieldWithErrors input {
border: 5px solid #f66;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我给你的第一条建议是放弃 Rails 表单助手并开始使用 formtastic (http://github.com /justinfrench/formtastic)。我保证您满意,否则退款。
喝完形式化的酷莱德后,您的下一个停靠港应该是有效的(http:// github.com/grimen/validatious-on-rails/)。它与 formtastic(或任何与此相关的表单生成器)配合得非常好,并且应该可以帮助您避免重新发明轮子。
我希望这有帮助!
My first piece of advice for you would be to ditch rails form helpers and start using formtastic (http://github.com/justinfrench/formtastic). I guarantee you satisfaction, or your money back.
After you've drunk the formtastic coolaid your next port of call should be validatious (http://github.com/grimen/validatious-on-rails/). It works really well with formtastic (or any form builder for that matter), and should help you avoid reinventing the wheel.
I hope this helps!