Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleForm 2 : input tags inside a wrapper

by using the default simple_form 2 wrapper, it generates a markup that looks like this :

<div class="...">
  <label>...</label>
  <input ... />
</div>

I would like to get a markup in which the input tag is itself inside a wrapper like this :

<div class="...">
  <label>...</label>
  <div class="...">
    <input ... />
  </div>
</div>

Do I have to create a custom component for this behaviour ?

Thanks.

Nicolas.

like image 568
Nicolas Blanco Avatar asked Feb 28 '12 18:02

Nicolas Blanco


1 Answers

Check this out:

The wrapper API isn't well documented yet, but I've spent some time trying to figure it out. Here is a simple example, which can be setup inside the simple_form initializer.

# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
  # Wrappers are used by the form builder to generate a complete input.
  # You can remove any component from the wrapper, change the order or even
  # add your own to the stack. The options given to the wrappers method
  # are used to wrap the whole input (if any exists).

  config.wrappers :GIVE_YOUR_WRAPPER_A_NAME, :class => 'clearfix', :error_class => nil do |b|
    b.use :placeholder
    b.use :label
    b.use :tag => 'div', :class => 'WHATEVER_YOU_WANT_TO_CALL_IT' do |ba|
      ba.use :input
      ba.use :error, :tag => :span, :class => :'help-inline'
      ba.use :hint,  :tag => :span, :class => :'help-block'
    end
  end
end

Then, when you create a form just specify the wrapper you want to use:

<%= simple_form_for @user, wrapper: 'WHATEVER_YOU_CALLED_YOUR_WRAPPER' do |form| %>
<%end%>

Hope this helps.

like image 194
TheDelChop Avatar answered Sep 21 '22 13:09

TheDelChop