At forms, "Required: true" is not working
Asked Answered
A

3

24

I'm making a Rails app but I'm having this issue

In my form, I'm trying to make required some fields but it is not working.

<%= f.input :name, required: true, label: false, placeholder: "Name", input_html: {class: "form-control"} %>
Augmented answered 4/8, 2015 at 22:19 Comment(0)
U
55

If you place required: true in the input you should see the field has the "required" class and required="required" attribute.

If you're not seeing a required attribute, then check your simple_form initializer (config/initializers/simple_form.rb) and ensure that browser validations are on:

config.browser_validations = true

I believe it's also possible to override the required attribute through the input_html hash, for a single field. For example:

<%= f.input :name, label: false, placeholder: "Name", input_html: { class: "form-control", required: true } %>

Personally, I don't like browser validations. They feel clunky to me. If you're interested in better client-side validations, then check out the judge gem and it's simple_form adapter.

Uneducated answered 5/8, 2015 at 1:37 Comment(1)
I had this issue and it was not solved by only doing the above. However, updating simple_form with the above did fix my issue (specifically with the country_select gem). Hope this helps someone else.Carothers
S
5

add this to the f.input input_html: { required: true }

after setting config.browser_validations = true

Reset rails server. <-- this took me awhile to realize since config are run once every time you start the server

Stooge answered 19/4, 2017 at 18:32 Comment(0)
G
5

Be careful, it is required: true NOT require: true

Gleda answered 25/8, 2020 at 22:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.