simple_form: include_blank: false is ignored when required is true

This is an odd issue I am facing:

f.input :title, collection: @titles, include_blank: false

works as expected.

f.input :title, collection: @titles, include_blank: false, required: true

adds an empty option at the top.

Is this expected behaviour?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 4
  • Comments: 18 (6 by maintainers)

Most upvoted comments

I have this issue right now! On version 3.4.0.

= f.input :language, collection: languages, include_blank: false

is ok, but when I add required: true, it shows blank option.

Ok, but I think the problem in context of simple_form is that people put required: true on the input to get the asterisk next to the label, not necessarily to get a required-attribute on the select-tag. The asterisk next to label still makes sense when there is no blank field.

I think simple_form needs a way to enable the required message on the label without changing the select tag. I did the following in a project of mine:

# initializer
module LabelRequiredExt
  def required_field?
    options[:label_required] || super
  end
end

SimpleForm::Components::Labels.prepend(LabelRequiredExt)

# view
f.input :title, collection: @titles, include_blank: false, label_required: true

@adeelejaz: This is definetely not fixed in simple_form 3.5.0 on Rails 4.2.9.

This is a workaround which works for me, include prompt: '', for example: f.input :language, collection: languages, include_blank: false, required: true, prompt: ''

@fschwahn That sounds like a new feature request. Maybe it deserves a new issue to be discussed there? I’m not a maintainer of this gem so it will be up to them.

Just curious though, couldn’t you achieve the same result by using this code?

f.input :title, collection: @titles, include_blank: false, html: { class: 'required' }

Is there a new issue for this problem already?