ruby-on-railsruby-on-rails-5activeadminformtastic

required input in activeadmin form not working


I am using active_admin. I am trying to make a form field required in activeadmin:

  input :team, as: :select, required: true, collection: Team.all.pluck(:name, :id), include_blank: "Please enter a team", allow_blank: false

It is only on this specific activeadmin page that i want this validation. It should not exist anywhere else in the site, so I don't want to do it in the model.

For some reason, the code above is not working. While the form field does show a *, it still submits. How can I make this input required only on this page?


Solution

  • What you need is input_html: {required: true}

    # adds .required class to the input's enclosing <li> element - form can still be submitted
    input :team, required: true   
    
    # adds required attribute to the <input> element - form cannot be submitted
    input :team, input_html: {required: true}