At forms, "Required: true" is not working

yotta1 picture yotta1 · Aug 5, 2015 · Viewed 16.9k times · Source

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"} %>

Answer

Curtis picture Curtis · Aug 5, 2015

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.