Simple_form: include_blank: false is ignored when required is true

Created on 9 Sep 2016  路  15Comments  路  Source: heartcombo/simple_form

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?

Most helpful comment

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

All 15 comments

With simple_form (3.3.1)

f.input :foap_number, collection: @foaps, required: true, include_blank: false
=> ArgumentError - include_blank cannot be false for a required field.:

This is not a simple form issue. It is related action view. https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/tags/base.rb#L125 raising this error.

Thanks @sivagollapalli

Still have this issue. Any info?

Simple form 3.4.0

This is no longer an issue and thank you for the fix.

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.

@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: ''

I have same issue with simple_form 3.5.0 and rails >= 5
f.association :gallery, required: true, label: false, collection: galleries, label_method: :name, value_method: :id, include_blank: false

This is still an issue. I also am experiencing this on rails 4.2.10 and rails 5 with version 3.5.0 of simple_form.

Can we get a fix for this?

I guess we can open a new issue...

This is not an issue with simple_form nor rails. Here is the pull request that implemented it in actionview: https://github.com/rails/rails/pull/20124

In that thread you will find the explanation for this behavior. It's defined over here: https://html.spec.whatwg.org/multipage/forms.html#attr-select-required

actionview is implementing HTML correctly. The error message could be a more expressive though.

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

@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' }

Setting the class won't include the asterisk, I'm talking about this code:

https://github.com/plataformatec/simple_form/blob/f98b158a605f9a76dd0a4f60ae8549a3c5f863a5/lib/simple_form/components/labels.rb#L63-L65

This would be a new feature, just left this here for other people as a possible workaround.

Is there a new issue for this problem already?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

frankyston picture frankyston  路  4Comments

mszyndel picture mszyndel  路  5Comments

Naokimi picture Naokimi  路  4Comments

gdgp picture gdgp  路  4Comments

JanStevens picture JanStevens  路  5Comments