7

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 ...
Veger
  • 37,240
  • 11
  • 105
  • 116
Marcin Doliwa
  • 3,639
  • 3
  • 37
  • 62
  • There is a cleaner way of solving this. [Check this other question for reference](https://stackoverflow.com/a/67236379/9908922) – mferreri Apr 23 '21 at 20:32

4 Answers4

2

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 %>
Gregdebrick
  • 533
  • 6
  • 14
1
  = 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

Flavio Wuensche
  • 9,460
  • 1
  • 57
  • 54
0

You wrapper looks quite ok to me, but the code in your view to render the field needs some changes:

<%= f.input :start_time_date, as: :string, required: false, label: "Start date" do %>
  <%= f.input_field :start_time_date, class: "form-control" %>
  <span class="input-group-addon">
    <span class="glyphicon glyphicon-calendar"></span>
  </span>
<% end %>

By passing a block to the input field you are able to add the calendar icon to the input field. The f.input_field helper method renders the actual input field, the outer f.input is used to match the correct wrapper.

For more information and a compete example on using simple_form with Bootstraps input-group, you can check my blog post.

Veger
  • 37,240
  • 11
  • 105
  • 116
0

Same problem solve this way.

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'
    ba.wrapper tag: 'span', class: 'input-group-addon' do |append|
      ba.wrapper tag: 'span', class: 'glyphicon glyphicon-calendar' do |i|
      end
    end
  end

  b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
  b.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
end
Vivek Singh
  • 625
  • 8
  • 15