在 simple_form 上显示完整消息
Show full messages on simple_form
我有这个表格
<%= simple_form_for :post do |f| %>
<%= f.error_notification %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.button :submit %>
<% end %>
我的示例中的标题字段是必需的。因此,如果我尝试提交 :title
中没有任何值的表单,验证将失败并返回一条错误消息,正如预期的那样。但是我默认得到的错误就是这个 can't be blank
,所以我想知道是否有办法显示验证错误的完整消息(比如不使用 simple_form
gem 时),比如The title field can't be blank
等
有没有办法显示完整的消息?
不是很干净,但是可以用:)
<%= simple_form_for :post do |f| %>
<%= f.error_notification %>
<%= f.input :title, error: f.object.errors.full_messages_for(:title).to_sentence %>
<%= f.input :body, error: f.object.errors.full_messages_for(:body).to_sentence %>
<%= f.button :submit %>
<% end %>
尝试使用 full_error 方法,如下所示:
<%= f.input :title, error: f.full_error(:title) %>
经检查,我发现您可以全局配置:
config/initializers/simple_form.rb
SimpleForm.setup do |config|
config.wrappers :default, class: :input,
hint_class: :field_with_hint, error_class: :field_with_errors do |b|
# ...
# ...
# COMMENT THIS LINE JUST LIKE THIS:
# b.use :error, wrap_with: { tag: :span, class: :error }
# THEN UNCOMMENT THIS LINE JUST LIKE THIS:
b.use :full_error, wrap_with: { tag: :span, class: :error }
end
end
已测试工作。
我有这个表格
<%= simple_form_for :post do |f| %>
<%= f.error_notification %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.button :submit %>
<% end %>
我的示例中的标题字段是必需的。因此,如果我尝试提交 :title
中没有任何值的表单,验证将失败并返回一条错误消息,正如预期的那样。但是我默认得到的错误就是这个 can't be blank
,所以我想知道是否有办法显示验证错误的完整消息(比如不使用 simple_form
gem 时),比如The title field can't be blank
等
有没有办法显示完整的消息?
不是很干净,但是可以用:)
<%= simple_form_for :post do |f| %>
<%= f.error_notification %>
<%= f.input :title, error: f.object.errors.full_messages_for(:title).to_sentence %>
<%= f.input :body, error: f.object.errors.full_messages_for(:body).to_sentence %>
<%= f.button :submit %>
<% end %>
尝试使用 full_error 方法,如下所示:
<%= f.input :title, error: f.full_error(:title) %>
经检查,我发现您可以全局配置:
config/initializers/simple_form.rb
SimpleForm.setup do |config|
config.wrappers :default, class: :input,
hint_class: :field_with_hint, error_class: :field_with_errors do |b|
# ...
# ...
# COMMENT THIS LINE JUST LIKE THIS:
# b.use :error, wrap_with: { tag: :span, class: :error }
# THEN UNCOMMENT THIS LINE JUST LIKE THIS:
b.use :full_error, wrap_with: { tag: :span, class: :error }
end
end
已测试工作。