<%= f.label :net %>
<%= f.text" /> <%= f.label :net %>
<%= f.text" /> <%= f.label :net %>
<%= f.text"/>

Rails asterisk/"*" not appearing in one label of a form

85 Views Asked by At

Situation

Have a simple form which abstract parts i post below:

  <div class="form-group">
    <%= f.label :net %>
    <div class="input-group  w-50">
      <%= f.text_field :net,
        required: true,
        class: 'form-control text-right' %>
      <div class="input-group-append">
        <span class="input-group-text" style="width: 40px;">€</span>
      </div>
    </div>
  </div>

&

<div class="form-group">
            <%= f.label :vat_rate, t('in_invoice.form.vat_rate') %>
            <%= render 'home/shared/standard_vat'  %>
            <div class="input-group w-50">
              <%= f.text_field :vat_rate,
                               required: true,
                               class: 'form-control text-right' %>
              <div class="input-group-append">
                <span class="input-group-text" style="width: 40px;">%</span>
              </div>
            </div>
          </div>

with model:

    ...

  validates :net, :number, :vat_rate, presence: true
  validates :net, numericality: { greater_than: 0}
  validates :vat_rate, numericality: { only_integer: true, greater_than_or_equal_to: 0}
  validates :net, numericality: true

    ...

My problem I want to put an asterisk on the vat_rate (Screenshot: "VAT-Rate in %") label in the form. But for some reason it appears only on the :net and the :number field despite making them equal (e.g. removing the partial in between, includind presence validation) as you can see on the screenshotform. (I think it doesn't matter but the form is a simple_form initialized with simple_form_for)

2

There are 2 best solutions below

0
Alex On

Asterisk is added by simple form. When you pass text as a second argument you override everything. If you want to keep the * pass text as an option:

<%= f.label :vat_rate, label: t('in_invoice.form.vat_rate') %>
#                      ^^^^^^

https://github.com/heartcombo/simple_form/blob/v5.3.0/lib/simple_form/form_builder.rb#L319


If your simple_form doesn't look like this, you're missing the point of simple form:

<%= simple_form_for @invoice do |f| %>
  <%= f.input :net %>
  <%= f.input :vat_rate %>
<% end %>

You should configure your inputs only once and just use input helper:

https://github.com/heartcombo/simple_form/tree/main#configuration

For example:

# config/initializers/simple_form.rb

module InputGroup
  def append(wrapper_options = nil)
    template.content_tag(:span, options[:append], class: "input-group-text")
  end
end
SimpleForm.include_component(InputGroup)

SimpleForm.setup do |config|
  # https://github.com/heartcombo/simple_form-bootstrap
  config.wrappers :group, tag: "div", class: "form-group", error_class: "form-group-invalid", valid_class: "form-group-valid" do |b|
    b.use :html5
    b.use :placeholder
    b.optional :maxlength
    b.optional :minlength
    b.optional :pattern
    b.optional :min_max
    b.optional :readonly
    b.use :label

    b.wrapper :input_group_tag, tag: "div", class: "input-group-append" do |ba|
      ba.use :input, class: "form-control", error_class: "is-invalid", valid_class: "is-valid"
      ba.optional :append
    end

    b.use :full_error, wrap_with: {tag: "div", class: "invalid-feedback d-block"}
    b.use :hint, wrap_with: {tag: "small", class: "form-text text-muted"}
  end
end
<%= simple_form_for @invoice do |f| %>
  <%= f.input :net,      wrapper: :group, append: "€" %>
  <%= f.input :vat_rate, wrapper: :group, append: "%" %>
<% end %>
0
Julian On

so to summarize: "required: true/false[DEFAULT]" is an option of the label tag which I overwrote by setting a label. Removed the label and now it works. Setting it worked too but wanted less code :-)