6
votes

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 ...
4
There is a cleaner way of solving this. Check this other question for referencemferreri

4 Answers

1
votes

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 %>
1
votes
  = 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

0
votes

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.

0
votes

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