0

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?

user938363
  • 9,990
  • 38
  • 137
  • 303

1 Answers1

2

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.

Hoang Phan
  • 2,146
  • 1
  • 15
  • 16
  • Yes, it works. We always run the generate when installing simple_form. What's the magic here? Many thanks. – user938363 Dec 12 '15 at 04:45
  • Actually, simple_form works right when you just add the gem and use it. The generator just used to modify some of its config. Unfortunately, the default of the config file is not what users expect from it. Maybe it changes in later versions? – Hoang Phan Dec 12 '15 at 04:48
  • `@Hoang Phan` by setting `config.broswer_validations = true`, is this going to change the behaviors of simple_form? We have been using `false` which is the default after running generator. – user938363 Dec 12 '15 at 05:00
  • It's only affect the behaviors of validations. By default I think simple_form would expect server is the one to manipulate validations, not something like html `required`. – Hoang Phan Dec 12 '15 at 05:10
  • `Hoang Phan`, does setting `config.validate_browser` to be true bring back the normal behavior of browser? It seems that `required` is the attribute which is not working properly if validate_browser is false. – user938363 Dec 12 '15 at 05:25