Rails - 简单形式 Bootstrap - 显示内联错误的全文 -(或任何地方)
Rails - Simple Form Bootstrap - show full text of error inline - (or anywhere)
我正在努力弄清楚如何使用带有 rails 5 的简单表单,以便我可以在我的表单中显示完整的错误消息。我得到的只是顶部的一个框,上面有一条消息:
Please review the problems below:
而不是问题列表或任何突出显示的表单字段 - 我没有得到任何东西。
我已遵循 this post 中的建议,并将初始化程序中所有对错误的引用替换为 'full_error',但这仅解决了父表单的问题 - 嵌套字段仍然只是不完整错误信息。有谁知道如何以简单的形式识别错误?
# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
config.error_notification_class = 'alert alert-danger'
config.button_class = 'btn btn-green'
config.boolean_label_class = nil
config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
b.use :label, class: 'control-label'
b.use :input, class: 'form-control'
b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
config.wrappers :vertical_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :readonly
b.use :label, class: 'control-label'
b.use :input
b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
config.wrappers :vertical_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.wrapper tag: 'div', class: 'checkbox' do |ba|
ba.use :label_input
end
b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
config.wrappers :vertical_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.use :label, class: 'control-label'
b.use :input
b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
config.wrappers :horizontal_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
b.use :label, class: 'col-sm-3 control-label'
b.wrapper tag: 'div', class: 'col-sm-9' do |ba|
ba.use :input, class: 'form-control'
ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :readonly
b.use :label, class: 'col-sm-3 control-label'
b.wrapper tag: 'div', class: 'col-sm-9' do |ba|
ba.use :input
ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
config.wrappers :horizontal_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.wrapper tag: 'div', class: 'col-sm-offset-3 col-sm-9' do |wr|
wr.wrapper tag: 'div', class: 'checkbox' do |ba|
ba.use :label_input
end
wr.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
wr.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.use :label, class: 'col-sm-3 control-label'
b.wrapper tag: 'div', class: 'col-sm-9' do |ba|
ba.use :input
ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
config.wrappers :inline_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
b.use :label, class: 'sr-only'
b.use :input, class: 'form-control'
b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
config.wrappers :multi_select, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.use :label, class: 'control-label'
b.wrapper tag: 'div', class: 'form-inline' do |ba|
ba.use :input, class: 'form-control'
ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
# Wrappers for forms and inputs using the Bootstrap toolkit.
# Check the Bootstrap docs (http://getbootstrap.com)
# to learn about the different styles for forms and inputs,
# buttons and other elements.
config.default_wrapper = :vertical_form
config.wrapper_mappings = {
check_boxes: :vertical_radio_and_checkboxes,
radio_buttons: :vertical_radio_and_checkboxes,
file: :vertical_file_input,
boolean: :vertical_boolean,
datetime: :multi_select,
date: :multi_select,
time: :multi_select
}
end
表格
<%= simple_form_for(@proposal) do |f| %>
<%= f.error_notification %>
<% @proposal.errors.full_messages.each do |msg| %>
<%= li= msg %>
<% end %>
<div class="form-inputs" style="margin-bottom: 50px">
<!-- General Organisation details -->
<div class="row">
<div class="col-md-12">
<%= f.input :title, :label => "Title" %>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.input :byline, :label => "Tagline" %>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.input :description, as: :text, :label => "Outline your proposal", input_html: { rows: 15 } %>
</div>
</div>
<!-- Package: :ethics considerations -->
<div class="row">
<div class="col-md-12">
<div class="form_title">Research Ethics</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.simple_fields_for :ethics do |f| %>
<%= f.error_notification %>
<%= render 'package/ethics/ethics_fields', f: f %>
<% end %>
<%= link_to_add_association 'Add another ethics issue', f, :ethics, partial: 'package/ethics/ethics_fields' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-10 col-md-offset-1" style="margin-top: 50px">
<div class="form-actions">
<%= f.button :submit %>
</div>
</div>
</div>
<% end %>
嵌套字段
<div class="nested-fields">
<%# @proposal.ethics.errors.full_messages.each do |e| %>
<li><%#= e %></li>
<%# end %>
<div class="form-inputs">
<div class="row">
<div class="col-md-12 ">
<%= f.select :category, [ "Risk of harm", "Informed consent", "Anonymity and Confidentiality", "Deceptive practices", "Right to withdraw"], { label: "Principle" }, id: "main_category" %>
<%= f.select :subcategory, [], {}, id: "sub_category", disabled: true %>
<%= f.input :consideration, as: :text, :label => "Identify the ethics considerations?", :input_html => {:rows => 8} %>
<%= f.input :plan, as: :text, :label => "How will these considerations be managed?", :input_html => {:rows => 8} %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6" style="margin-top: 20px; margin-bottom: 50px">
<%= link_to_remove_association 'Remove this ethical issue', f %>
</div>
</div>
</div>
我在您的代码中发现的第一个错误是,在您的嵌套字段中,您在参数中采用了相同的变量 f
,这可能会与父参数enter code here
ter f
所以,
嵌套将是,
<%= f.simple_fields_for :ethics do |ff| %>
<%= ff.error_notification %>
<%= render 'package/ethics/ethics_fields', f: f %>
<% end %>
现在表格,
<%= simple_form_for(@proposal) do |f| %>
<%= f.error_notification %>
<% @proposal.errors.full_messages.each do |msg| %>
<%= li= msg %>
<% end %>
<div class="form-inputs" style="margin-bottom: 50px">
<!-- General Organisation details -->
<div class="row">
<div class="col-md-12">
<%= f.input :title, :label => "Title" %>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.input :byline, :label => "Tagline" %>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.input :description, as: :text, :label => "Outline your proposal", input_html: { rows: 15 } %>
</div>
</div>
<!-- Package: :ethics considerations -->
<div class="row">
<div class="col-md-12">
<div class="form_title">Research Ethics</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.simple_fields_for :ethics do |ff| %>
<%= ff.error_notification %>
<%= render 'package/ethics/ethics_fields', f: ff %>
<% end %>
<%= link_to_add_association 'Add another ethics issue', f, :ethics, partial: 'package/ethics/ethics_fields' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-10 col-md-offset-1" style="margin-top: 50px">
<div class="form-actions">
<%= f.button :submit %>
</div>
</div>
</div>
<% end %>
<%= f.simple_fields_for :ethics do |ff| %>
<%= ff.error_notification %>
<%= render 'package/ethics/ethics_fields', f: ff %>
<% @proposal.ethics.errors.full_messages.each do |msg| %>
<%= li= msg %>
<% end %>
<% end %>
编辑:即使以上没有解决问题,
尝试从 @proposal
变量
中获取 ethics object
的错误
所以,
<%= f.simple_fields_for :ethics do |ff| %>
<%= ff.error_notification %>
<% @proposal.ethics.errors.full_messages.each do |msg| %>
<%= li= msg %>
<% end %>
<%= render 'package/ethics/ethics_fields', f: f %>
<% end %>
您的错误显示代码中似乎混淆了 haml 和 erb。
这个:
<% @proposal.errors.full_messages.each do |msg| %>
<%= li= msg %>
<% end %>
应该看起来更像这样:
<% if @proposal.errors.any? %>
<ul>
<% @proposal.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
我猜你正在使用 Cocoon,所以你需要正确设置它,但是在使用 Rails 5.
时有一个陷阱
您的模型中需要类似以下内容:
class Proposal < ApplicationRecord
has_many: :ethics, inverse_of: :proposal
accepts_nested_attributes_for :ethics ...
...
和
class Ethic < ApplicationRecord
belongs_to :proposal, inverse_of: :ethics
...
注意 has_many .. inverse_of 子句,它解决了 Rails 5.
中的一个错误
如果您正确设置了 accepts_nested_attributes_for 子句,我认为 @proposal.errors 应该包含表单的所有错误。
另一种选择是直接在字段中显示错误消息。
<%= f.input :title, :label => "Title", id:"title" %>
<%= f.error :title, :id => "title_error" %>
这将在标题字段旁边显示标题的错误消息。您可以将 f.error 方法添加到表单中的每个字段,而不是在表单上方显示 <ul>
,或者在表单上方显示 <ul>
。
对于 nested_form_fields 尝试将 validates_associated :assoc_model_name
添加到主模型中。这也将 运行 对关联模型进行验证。
要使其在默认情况下以简单形式内联工作,您必须使用 bootstrap_simple-form 并确保控制器中的所有内容都已正确设置。
查看我和 Hollownest 在这里给出的答案作为参考
Showing fields with errors for nested forms in Rails 3.2 + SimpleForm
我正在努力弄清楚如何使用带有 rails 5 的简单表单,以便我可以在我的表单中显示完整的错误消息。我得到的只是顶部的一个框,上面有一条消息:
Please review the problems below:
而不是问题列表或任何突出显示的表单字段 - 我没有得到任何东西。
我已遵循 this post 中的建议,并将初始化程序中所有对错误的引用替换为 'full_error',但这仅解决了父表单的问题 - 嵌套字段仍然只是不完整错误信息。有谁知道如何以简单的形式识别错误?
# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
config.error_notification_class = 'alert alert-danger'
config.button_class = 'btn btn-green'
config.boolean_label_class = nil
config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
b.use :label, class: 'control-label'
b.use :input, class: 'form-control'
b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
config.wrappers :vertical_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :readonly
b.use :label, class: 'control-label'
b.use :input
b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
config.wrappers :vertical_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.wrapper tag: 'div', class: 'checkbox' do |ba|
ba.use :label_input
end
b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
config.wrappers :vertical_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.use :label, class: 'control-label'
b.use :input
b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
config.wrappers :horizontal_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
b.use :label, class: 'col-sm-3 control-label'
b.wrapper tag: 'div', class: 'col-sm-9' do |ba|
ba.use :input, class: 'form-control'
ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :readonly
b.use :label, class: 'col-sm-3 control-label'
b.wrapper tag: 'div', class: 'col-sm-9' do |ba|
ba.use :input
ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
config.wrappers :horizontal_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.wrapper tag: 'div', class: 'col-sm-offset-3 col-sm-9' do |wr|
wr.wrapper tag: 'div', class: 'checkbox' do |ba|
ba.use :label_input
end
wr.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
wr.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.use :label, class: 'col-sm-3 control-label'
b.wrapper tag: 'div', class: 'col-sm-9' do |ba|
ba.use :input
ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
config.wrappers :inline_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
b.use :label, class: 'sr-only'
b.use :input, class: 'form-control'
b.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
config.wrappers :multi_select, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly
b.use :label, class: 'control-label'
b.wrapper tag: 'div', class: 'form-inline' do |ba|
ba.use :input, class: 'form-control'
ba.use :full_error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
# Wrappers for forms and inputs using the Bootstrap toolkit.
# Check the Bootstrap docs (http://getbootstrap.com)
# to learn about the different styles for forms and inputs,
# buttons and other elements.
config.default_wrapper = :vertical_form
config.wrapper_mappings = {
check_boxes: :vertical_radio_and_checkboxes,
radio_buttons: :vertical_radio_and_checkboxes,
file: :vertical_file_input,
boolean: :vertical_boolean,
datetime: :multi_select,
date: :multi_select,
time: :multi_select
}
end
表格
<%= simple_form_for(@proposal) do |f| %>
<%= f.error_notification %>
<% @proposal.errors.full_messages.each do |msg| %>
<%= li= msg %>
<% end %>
<div class="form-inputs" style="margin-bottom: 50px">
<!-- General Organisation details -->
<div class="row">
<div class="col-md-12">
<%= f.input :title, :label => "Title" %>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.input :byline, :label => "Tagline" %>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.input :description, as: :text, :label => "Outline your proposal", input_html: { rows: 15 } %>
</div>
</div>
<!-- Package: :ethics considerations -->
<div class="row">
<div class="col-md-12">
<div class="form_title">Research Ethics</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.simple_fields_for :ethics do |f| %>
<%= f.error_notification %>
<%= render 'package/ethics/ethics_fields', f: f %>
<% end %>
<%= link_to_add_association 'Add another ethics issue', f, :ethics, partial: 'package/ethics/ethics_fields' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-10 col-md-offset-1" style="margin-top: 50px">
<div class="form-actions">
<%= f.button :submit %>
</div>
</div>
</div>
<% end %>
嵌套字段
<div class="nested-fields">
<%# @proposal.ethics.errors.full_messages.each do |e| %>
<li><%#= e %></li>
<%# end %>
<div class="form-inputs">
<div class="row">
<div class="col-md-12 ">
<%= f.select :category, [ "Risk of harm", "Informed consent", "Anonymity and Confidentiality", "Deceptive practices", "Right to withdraw"], { label: "Principle" }, id: "main_category" %>
<%= f.select :subcategory, [], {}, id: "sub_category", disabled: true %>
<%= f.input :consideration, as: :text, :label => "Identify the ethics considerations?", :input_html => {:rows => 8} %>
<%= f.input :plan, as: :text, :label => "How will these considerations be managed?", :input_html => {:rows => 8} %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6" style="margin-top: 20px; margin-bottom: 50px">
<%= link_to_remove_association 'Remove this ethical issue', f %>
</div>
</div>
</div>
我在您的代码中发现的第一个错误是,在您的嵌套字段中,您在参数中采用了相同的变量 f
,这可能会与父参数enter code here
ter f
所以,
嵌套将是,
<%= f.simple_fields_for :ethics do |ff| %>
<%= ff.error_notification %>
<%= render 'package/ethics/ethics_fields', f: f %>
<% end %>
现在表格,
<%= simple_form_for(@proposal) do |f| %>
<%= f.error_notification %>
<% @proposal.errors.full_messages.each do |msg| %>
<%= li= msg %>
<% end %>
<div class="form-inputs" style="margin-bottom: 50px">
<!-- General Organisation details -->
<div class="row">
<div class="col-md-12">
<%= f.input :title, :label => "Title" %>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.input :byline, :label => "Tagline" %>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.input :description, as: :text, :label => "Outline your proposal", input_html: { rows: 15 } %>
</div>
</div>
<!-- Package: :ethics considerations -->
<div class="row">
<div class="col-md-12">
<div class="form_title">Research Ethics</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<%= f.simple_fields_for :ethics do |ff| %>
<%= ff.error_notification %>
<%= render 'package/ethics/ethics_fields', f: ff %>
<% end %>
<%= link_to_add_association 'Add another ethics issue', f, :ethics, partial: 'package/ethics/ethics_fields' %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-10 col-md-offset-1" style="margin-top: 50px">
<div class="form-actions">
<%= f.button :submit %>
</div>
</div>
</div>
<% end %>
<%= f.simple_fields_for :ethics do |ff| %>
<%= ff.error_notification %>
<%= render 'package/ethics/ethics_fields', f: ff %>
<% @proposal.ethics.errors.full_messages.each do |msg| %>
<%= li= msg %>
<% end %>
<% end %>
编辑:即使以上没有解决问题,
尝试从 @proposal
变量
ethics object
的错误
所以,
<%= f.simple_fields_for :ethics do |ff| %>
<%= ff.error_notification %>
<% @proposal.ethics.errors.full_messages.each do |msg| %>
<%= li= msg %>
<% end %>
<%= render 'package/ethics/ethics_fields', f: f %>
<% end %>
您的错误显示代码中似乎混淆了 haml 和 erb。
这个:
<% @proposal.errors.full_messages.each do |msg| %>
<%= li= msg %>
<% end %>
应该看起来更像这样:
<% if @proposal.errors.any? %>
<ul>
<% @proposal.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
我猜你正在使用 Cocoon,所以你需要正确设置它,但是在使用 Rails 5.
时有一个陷阱您的模型中需要类似以下内容:
class Proposal < ApplicationRecord
has_many: :ethics, inverse_of: :proposal
accepts_nested_attributes_for :ethics ...
...
和
class Ethic < ApplicationRecord
belongs_to :proposal, inverse_of: :ethics
...
注意 has_many .. inverse_of 子句,它解决了 Rails 5.
中的一个错误如果您正确设置了 accepts_nested_attributes_for 子句,我认为 @proposal.errors 应该包含表单的所有错误。
另一种选择是直接在字段中显示错误消息。
<%= f.input :title, :label => "Title", id:"title" %>
<%= f.error :title, :id => "title_error" %>
这将在标题字段旁边显示标题的错误消息。您可以将 f.error 方法添加到表单中的每个字段,而不是在表单上方显示 <ul>
,或者在表单上方显示 <ul>
。
对于 nested_form_fields 尝试将 validates_associated :assoc_model_name
添加到主模型中。这也将 运行 对关联模型进行验证。
要使其在默认情况下以简单形式内联工作,您必须使用 bootstrap_simple-form 并确保控制器中的所有内容都已正确设置。
查看我和 Hollownest 在这里给出的答案作为参考
Showing fields with errors for nested forms in Rails 3.2 + SimpleForm