How to use Bootstrap's input group in simple_form gem
Asked Answered
L

2

5

Environment

  • Ruby 2.5.1
  • Rails 5.2.1
  • Simple Form 4.0.1

Current behavior

I follow example in http://simple-form-bootstrap.plataformatec.com.br/examples/input_group

I start with <%= simple_form_for app, wrapper: :input_group do |f| %> and get Couldn't find wrapper with name input_group, so I uncomment

config.wrappers :input_group, tag: 'div', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b|
    b.use :html5
    b.use :placeholder
    b.optional :maxlength
    b.optional :minlength
    b.optional :pattern
    b.optional :min_max
    b.optional :readonly
    b.use :label, class: 'form-control-label'
    b.wrapper :input_group_tag, tag: 'div', class: 'input-group' do |ba|
      ba.optional :prepend
      ba.use :input, class: 'form-control', error_class: 'is-invalid', valid_class: 'is-valid'
      ba.optional :append
    end
    b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' }
    b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' }
  end

in simple_form_bootstrap.rb

And get undefined methodappend' for class SimpleForm::Inputs::StringInput' instead.

Problem on this <%= f.input :subdomain, append: "example.com" %>

How can I use this, does this even support in simple_form? Don't know why they comment that chunk of code out.

Leitman answered 15/9, 2018 at 7:32 Comment(0)
G
7

create config/initializers/simple_form_component.rb

module InputGroup
  def prepend(wrapper_options = nil)
    span_tag = content_tag(:span, options[:prepend], class: "input-group-text")
    template.content_tag(:div, span_tag, class: "input-group-prepend")
  end

  def append(wrapper_options = nil)
    span_tag = content_tag(:span, options[:append], class: "input-group-text")
    template.content_tag(:div, span_tag, class: "input-group-append")
  end
end

# Register the component in Simple Form.
SimpleForm.include_component(InputGroup)

Source

Goudy answered 15/9, 2018 at 21:2 Comment(2)
Thanks, its weird that this doesn't included in the lib and have to add manually.Leitman
To use it in your form: <%= f.input :percentage, wrapper: :input_group, append: '%' %>Masonite
R
-1

In case somebody is wondering how to make this work. To complement PGill's Answer. After you create the file config/initializers/simple_form_component.rb with the content show above.

You should go to your file config/initializers/simple_form_bootstrap.rb and on the input you are going to use add something like this:

b.wrapper :grid_wrapper, tag: 'div', class: 'input-group' do |ba|
  ba.optional :prepend
  ba.use :input, class: 'form-control', error_class: 'is-invalid', valid_class: ''
  ba.optional :append
  # [...]
end

So your input_wrapper should look something like this:

  config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b|
    b.use :html5
    b.use :placeholder
    b.optional :maxlength
    b.optional :minlength
    b.optional :pattern
    b.optional :min_max
    b.optional :readonly
    b.use :label
    b.wrapper :grid_wrapper, tag: 'div', class: 'input-group' do |ba|
      ba.optional :prepend
      ba.use :input, class: 'form-control', error_class: 'is-invalid', valid_class: ''
      ba.optional :append
      ba.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback' }
      ba.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' }
    end
  end

And after that you could use it on your erb views like this:

 <%= f.input :attribute_name, prepend: "$" %>
Rudnick answered 23/4, 2021 at 20:20 Comment(1)
This is not required in this example. follow the question and the selected answer only.Rally

© 2022 - 2025 — McMap. All rights reserved.