Search code examples
ruby-on-railssimple-form

How to configure a SimpleForm input to default to no wrapper


Given a custom SimpleForm input

class MyCustomInput < SimpleForm::Inputs::Base
  # some stuff
end

How can I configure this so that these inputs, by default, do not have a wrapper.

Normally I would set input-specific wrappers like this:

# initializers/simpleform
config.wrapper_mappings = {
  my_custom: :my_wrapper
}

But the following does not work, and the default SimpleForm wrapper is still applied.

config.wrapper_mappings = {
  my_custom: false
}

I'm aware that there are ways to achieve this in the view, e.g.,

<%= f.input :attribute, as: :my_custom, wrapper: false %>

or

<%= f.input_field :attribute %>

But this is not what I'm looking for.

Is there a way to configure an input so that no wrapper is the default?


Solution

  • Apart from the option you already mentioned in your question, of just using input_field instead of input (more here: https://github.com/plataformatec/simple_form#stripping-away-all-wrapper-divs), you may define your wrapper like this (untested):

    SimpleForm.setup do |config|
      config.wrapper_mappings = {
        my_custom: :wrapper_false,
      }
    
      config.wrappers :wrapper_false, tag: false do |b|
        b.use :placeholder
        b.use :label_input
        b.use :hint,  wrap_with: { tag: :span, class: :hint }
        b.use :error, wrap_with: { tag: :span, class: :error }
      end
    end
    

    And then in your forms:

    <%= f.input :attribute, as: :my_custom %>
    

    More on the wrappers API: https://github.com/plataformatec/simple_form#the-wrappers-api

    EDIT:

    After considering the case you mentioned, I sent a PR to simple_form project to allow for wrapper: false to be configured in wrapper_mappings. If it gets accepted, you should be able to disable the wrapper directly in the initializer like this:

    config.wrapper_mappings = {
      my_custom: false
    }