Search code examples
ruby-on-railssimple-form

Rails: Attribute required for collection in simple_form


We are trying to set required in simple_form. The following setup does not work.

<%= f.input :payee_id, :label => t('Payee'), required: true, :collection => BankAccountx.payee_class.where(active: true).map {|x| [x.name, x.id]}, :selected => params[:payee_id], include_blank: true %>

Also tried :input_html => {required: true} and it did not work as well.

What's the right way to set required in simple_form?


Solution

  • Found it. If you use this command:

    rails generate simple_form:install
    

    It will create a file at config/initializer/simple_form.rb. Find this line

    config.browser_validations = false
    

    And change it to true. NOTE: If you just add the gem and not run rails generate, it works by default.