Is there any way to show errors not on top of form page, but next to field, that fired an error?
问问题
2701 次
5 回答
8
initializers/my_custom_error_messages.rb
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
errors = Array(instance.error_message).join(',')
%(#{html_tag}<span class="validation-error"> #{errors}</span>).html_safe
end
update:
without label
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
errors = Array(instance.error_message).join(',')
if html_tag =~ /^<label/
html_tag
else
%(#{html_tag}<span class="validation-error"> #{errors}</span>).html_safe
end
end
ref: rails guide
于 2012-12-19T10:38:31.373 回答
1
这曾经是 Rails 的一部分,现在可以在 gem 中使用:
https://github.com/joelmoss/dynamic_form
它允许您轻松显示表单构建器对象的任何特定属性的错误,例如:
<%= f.text_field :foo %>
<%= f.error_message_on :foo %>
于 2012-12-19T11:58:22.293 回答
1
您可以使用 simple_form gem 显示字段的验证错误
<%= simple_form_for @user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
如果您想在表单顶部显示验证错误,请使用
object.error_messages
表单标签后
于 2012-12-19T10:29:39.190 回答
0
@model.errors
由于布局顶部 div 中的迭代,错误显示在表单顶部。如果您移动此代码以检查每个字段上的哈希错误,您可以完成您想要的。
于 2012-12-19T10:32:13.420 回答
0
<td class="error"><%=@user.errors[:firstname].join(",") %></td>
于 2015-07-07T03:39:24.260 回答