heartcombo / simple_form

Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
http://blog.plataformatec.com.br/tag/simple_form
MIT License
8.21k stars 1.32k forks source link

include_blank: false is ignored when required is true #1427

Closed adeelejaz closed 7 years ago

adeelejaz commented 8 years ago

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?

olimart commented 7 years ago

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.:
sivagollapalli commented 7 years ago

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.

olimart commented 7 years ago

Thanks @sivagollapalli

ngoral commented 7 years ago

Still have this issue. Any info?

Simple form 3.4.0

adeelejaz commented 7 years ago

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

ngoral commented 7 years ago

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.

gutschi commented 7 years ago

@adeelejaz: This is definetely not fixed in _simpleform 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: ''

danijel commented 6 years ago

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

caseyprovost commented 6 years ago

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?

ngoral commented 6 years ago

I guess we can open a new issue...

etagwerker commented 6 years ago

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.

fschwahn commented 6 years ago

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
etagwerker commented 6 years ago

@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' }
fschwahn commented 6 years ago

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.

vongruenigen commented 5 years ago

Is there a new issue for this problem already?

mayuresh-srivastava commented 3 years ago

I got it working with these two: required: true, prompt: ''

colinlieberman commented 2 years ago

I got it working with these two: required: true, prompt: ''

@mayuresh-srivastava with what rails version?

mayuresh-srivastava commented 2 years ago

I got it working with these two: required: true, prompt: ''

@mayuresh-srivastava with what rails version?

Rails 5