Philip7899
Philip7899

Reputation: 4687

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?

Upvotes: 8

Views: 8632

Answers (4)

Rutani Thakkar
Rutani Thakkar

Reputation: 31

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} 

Upvotes: 2

Manikchand V
Manikchand V

Reputation: 21

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

Upvotes: 2

AFOC
AFOC

Reputation: 854

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} 

Upvotes: 5

Piers C
Piers C

Reputation: 2978

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'.

Upvotes: 3

Related Questions