required input in activeadmin form not working
Asked Answered
R

4

8

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?

Reversal answered 13/7, 2017 at 17:3 Comment(0)
A
5

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} 
Apery answered 12/1, 2020 at 6:44 Comment(1)
Works, verified. ThanksVary
T
3

This is really a Formtastic issue, not Active Admin. I don't think you can combine allow_blank: false, include_blank: 'text' and required: true. Try include_blank: false and hint: 'Please enter a team'.

Tellurite answered 15/7, 2017 at 15:9 Comment(0)
J
2
ActiveAdmin.register Model, as: "Model" do
    Formtastic::FormBuilder.perform_browser_validations = true
    # all code
end
Jeanettajeanette answered 5/11, 2021 at 8:1 Comment(0)
B
2
Add this line to your related active_admin file

Formtastic::FormBuilder.perform_browser_validations = true

ActiveAdmin.register Model, as: "Model" do   
  Formtastic::FormBuilder.perform_browser_validations = true  
    # all code  
end

Add input_html: {required: true}  to field which you want to make it required
input :first_name, input_html: {required: true} 
Buttock answered 11/12, 2023 at 13:58 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Salesin

© 2022 - 2025 — McMap. All rights reserved.