您可以通过修改您的 simple_form 配置文件中的默认包装来做到这一点。使用默认(非引导)配置执行此操作的方法是修改 config/initializers/simple_form.rb 中的 Inputs 部分默认包装器,来自:
b.use :label_input
b.use :hint, wrap_with: { tag: :span, class: :hint }
b.use :error, wrap_with: { tag: :span, class: :error }
到:
b.use :label
b.use :hint, wrap_with: { tag: :span, class: :hint }
b.use :error, wrap_with: { tag: :span, class: :error }
b.use :input
您可以将相同的技术应用于配置的引导版本。
这会使单个复选框在出现错误时看起来有点奇怪,因此您还需要制作一个与原始配置重复的包装器版本,并将其设置为仅与 boolean 输入一起使用类型。例如(同样来自默认配置,而不是引导配置):
config.wrappers :checks, class: :input,
hint_class: :field_with_hint, error_class: :field_with_errors do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
## Inputs
b.use :label_input
b.use :hint, wrap_with: { tag: :span, class: :hint }
b.use :error, wrap_with: { tag: :span, class: :error }
end
config.wrapper_mappings = { boolean: :checks }