Rails4/Bootstrap3:从简单表单中删除has错误包装器



我使用的是bootstrap 3和Rails 4,我正试图以一种简单的形式消除我的字段周围所有讨厌的"有错误"包装。

我已添加

  config.action_view.field_error_proc = Proc.new { |html_tag, instance|
  html_tag
   }

到我的配置/应用程序.rb

但什么也没发生。有很多关于这方面的帖子,但似乎没有一个解决方案奏效。我正在重新启动服务器。

将以下内容放入config/ininitializers/simple_form_bootstrap.rb(请注意,"error_class"选项现在到处都是空白字符串,而且b.use: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-primary pull-right"
  config.boolean_label_class = nil
  config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: '' 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 :hint,  wrap_with: { tag: 'p', class: 'help-block' }
  end
  config.wrappers :vertical_file_input, tag: 'div', class: 'form-group', error_class: '' 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 :hint,  wrap_with: { tag: 'p', class: 'help-block' }
  end
  config.wrappers :vertical_boolean, tag: 'div', class: 'form-group', error_class: '' do |b|
    b.use :html5
    b.optional :readonly
    b.wrapper tag: 'div', class: 'checkbox' do |ba|
      ba.use :label_input
    end

    b.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
  end
  config.wrappers :vertical_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: '' do |b|
    b.use :html5
    b.optional :readonly
    b.use :label, class: 'control-label'
    b.use :input
    b.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
  end
  config.wrappers :horizontal_form, tag: 'div', class: 'form-group', error_class: '' 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 :hint,  wrap_with: { tag: 'p', class: 'help-block' }
    end
  end
  config.wrappers :horizontal_form_medium, tag: 'div', class: 'form-group', error_class: '' 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-5' do |ba|
      ba.use :input, class: 'form-control'
      ba.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
    end
  end  
  config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group', error_class: '' 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 :hint,  wrap_with: { tag: 'p', class: 'help-block' }
    end
  end
  config.wrappers :horizontal_boolean, tag: 'div', class: 'form-group', error_class: '' 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, class: 'col-sm-9'
      end
      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: '' 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 :hint,  wrap_with: { tag: 'p', class: 'help-block' }
    end
  end
  config.wrappers :inline_form, tag: 'div', class: 'form-group', error_class: '' 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 :hint,  wrap_with: { tag: 'p', class: 'help-block' }
  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,
  }
end

要恢复错误包装器,请将以下内容放回每个包装器块:

b.use :error, wrap_with: { tag: 'span', class: 'help-block' }

要更改错误包装类,请更改此键值对中的类名:

error_class: 'has-error'

相关内容

最新更新