Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap input-group wrapper with simple_form

I'm using simple_form with bootstrap wrappers, and can't figure out, how to do this.

I want to generate input like this:

<div class="form-group string optional event_start_time_date ">
  <label class="string optional control-label" for="event_start_time_date">Start date</label>
  <div class="input-group date" id="date_picker">
    <input class="string required form-control" type="text" name="event[start_time_date]" id="event_start_time_date">
    <span class="input-group-addon">
      <span class="glyphicon glyphicon-calendar"></span>
    </span>
  </div>
</div>

My wrapper looks like this:

config.wrappers :input_group, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
  b.use :label, class: 'control-label'
  b.wrapper :input_group_div, tag: 'div', class: 'input-group' do |ba|
    ba.use :input, class: 'form-control'
  end

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

And for

<%= f.input :start_time_date, as: :string, required: false, label: "Start date", wrapper: :input_group, :input_group_div_html {class: "date", id: "date-picker"} %>

It generates below html output:

<div class="form-group string optional event_start_time_date">
  <label class="string optional control-label" for="event_start_time_date">Start time</label>
  <div class="input-group date" id="date-picker">
    <input class="string optional form-control" type="text" name="event[start_time_date]" id="event_start_time_date">
  </div>
</div>

Now the differences:

  1. <span class="input-group-addon"> this is required, and should be generated by wrapper, but I don't know how generate it in wrapper.
  2. <span class="glyphicon glyphicon-calendar"></span> - different input groups will have different class here and different span text, best if I could pass it as some option in <%= f.input ...
like image 475
Marcin Doliwa Avatar asked Mar 18 '15 12:03

Marcin Doliwa


2 Answers

You can simply add a div between <%= f.input %> and <% f.input_field %>. Works fine :

  <%= f.input :start_time_date, as: :string, required: false, label: "Start date" do %>
   <div class="input-group">
      <%= f.input_field :start_time_date, class: "form-control" %>
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
      </span>
   </div>
  <% end %>
like image 82
Gregdebrick Avatar answered Oct 23 '22 11:10

Gregdebrick


  = simple_form_for(current_account) do |f|
    = f.input :slug, required: true, label: 'URL da página' do
      .input-group
        .input-group-prepend
          span.input-group-text https://www.immobilier.com.br/
        = f.input_field :slug, class: 'form-control'

enter image description here

like image 1
Flavio Wuensche Avatar answered Oct 23 '22 11:10

Flavio Wuensche