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?
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}
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'.
ActiveAdmin.register Model, as: "Model" do
Formtastic::FormBuilder.perform_browser_validations = true
# all code
end
Related
In ActiveAdmin I have a form with typed inputs and required fields, and even If I let required fields blank or put numbers in email typed input, I am still able to perform submit even if the form is not valid.
my code :
ActiveAdmin.register User do
actions :all, except: [:destroy, :edit]
permit_params :email
form do |f|
f.semantic_errors
f.inputs 'Invite a new user' do
f.input :email, required: true, input_html: {required: true, type: "email" }, as: :email
f.input :first_name
f.input :last_name
end
f.actions
end
This issue is due to the fact that formtastic puts "novalidate" by default on all forms :
https://github.com/formtastic/formtastic/issues/1001
The fix is to create an initializer and update default formtastic config where it make sense for you such as :
# config/initializers/formtastic.rb
# Formtastic is the form builder used by ActiveAdmin
# You can find the original config file here:
# https://github.com/formtastic/formtastic/blob/master/lib/generators/templates/formtastic.rb
# You can opt-in to Formtastic's use of the HTML5 `required` attribute on `<input>`, `<select>`
# and `<textarea>` tags by setting this to true (defaults to false).
Formtastic::FormBuilder.use_required_attribute = false
# You can opt-in to new HTML5 browser validations (for things like email and url inputs) by setting
# this to true. Doing so will omit the `novalidate` attribute from the `<form>` tag.
# See http://diveintohtml5.org/forms.html#validation for more info.
Formtastic::FormBuilder.perform_browser_validations = true
I'm creating a form with a .select field that loads a list of states via partial. The requirement isn't being enforced on state and I'm not sure why. It lets you submit the form with the default blank value 'State'
Would appreciate any help figuring out where my syntax is wrong on this form? If this looks foreign, using SLIM instead of HTML.
= f.select :state, nil, include_blank: 'State', required: true # not working
= render partial: 'addresses/states'
= f.text_field :zip, placeholder: 'Zip', required: true, pattern:'[0-9]*' # works
The states partial looks like this:
option value="AL" AL
option value="AK" AK
option value="AZ" AZ
option value="AR" AR
...
I am using Rails 4 and Simple Form to create a form where I ask users for a bunch of data. I am including a dropdown selector to a model association in the following way:
<%= f.association :location, collection: Location.order("LOWER(name)").all, required: true, include_blank: false, prompt: "Choose location..." %>
However, I get a undefined method 'name' for nil:NilClass error when the user doesn't actively choose anything and leaves the default prompt message selected in the dropdown.
How can I make the app send the user back to the form and highlight that he needs to choose a location in the dropdown? Just like it happens when you have a required input field and no data is provided...
Thanks!
Adding the required: true in your form doesn't actually make the :location a required attribute on your model.
You need to add the following to your model:
validates :location, presence: true
i have this form
= simple_form_for #category.fields.build, url: category_fields_path(#category) do |f|
= f.input :kind, collection: Field::FIELD_TYPES, prompt: "Choose field type"
= f.input :description
= f.submit "Add field"
and this field model
class Field < ActiveRecord::Base
FIELD_TYPES = %w(integer float date string text)
validates :description, presence: true
validates :kind, presence: true
belongs_to :category
end
when i leave 'description' field empty, no request is send and i get notice 'Please fill out this field'. which is what i want. on the other hand, when description is filled in but kind is not, a request is still send to the 'create' action! No field gets created, but 'description' needs to be filled in again. there should be no request in such situation. any idea how to fix this?
Though, I don't have exact answer to Your problem, but You should start with checking HTML output. Simple from relies on HTML5 to provide front-end validation. All inputs should have required attribute, to have validation enabled. Maybe there is a bug, and in this particular case simple_form does not output required attribute.
Another thing to take in account as it is HTML5, consult browser support: http://caniuse.com/#feat=form-validation . Theoretically it's possible that You are testing on browser that has limited support for form validations.
If You discover that simple_from did not output required for Your kind fuel, try forcing it:
= f.input :kind, collection: Field::FIELD_TYPES, prompt: "Choose field type", required: true
I got my answer at Simple Form github's issue topichere. to sum up, problem was prompt, validation is not (yet?) working with it correctly, solution is to replace it, eg like this:
= f.input :kind, collection: Field::FIELD_TYPES, include_blank: "Choose field type", label: false
I have a form that I want to validate if an option has been selected.
I do not want to accept the default starting option, "Please Select a Product"
I using this now and get a uninitialized contant error. I believe I am writing the syntax wrong.
Controller:
validates :product_name, :presence => { :unless => (product_name = "Please Select a Product")}
View:
<span class="span5 pagination-right">
<%= f.label "Product" %>
<%= f.select :product_name, options_for_select([ ["Please Select a Product"] ]) %>
</span>
How am I supposed to have the option written?
Thank you
The product_name is changed like this:
<script>
$(document).ready(function() {
$('#ticket_product_name').html("<option>Please Select a Product</option>");
$('#ticket_firmware_version_string').html("<option>Please Select a Firmware</option>");
$('#category').change(function(){
$('#ticket_product_name').html("<option>Please Select a Product</option>");
$('#ticket_firmware_version_string').html("<option>Please Select a Firmware</option>");
if ($('#category').val() == "blah")
{
$('#ticket_product_name').append("<option>blah</option>");
$('#ticket_product_name').append("<option>blah</option>");
}
else if ($('#category').val() == "another category")
{
$('#ticket_product_name').append("<option>blah product</option>");
}
until end of options, end script.
I think what you are looking for is a placeholder for this select. In this case, I used a disabled option:
<% options = options_for_select(["Select an option", ["Product #1",1], ["Product #2",2]], disabled: "Select an option") %>
<%= f.select :product_name, options %>
You should use include_blank from the select helper, like this:
<%= f.select :product_name, options_for_select([["opt1",1],["opt2",2]]), include_blank: true %>
and then, on model
validates :product_name, :presence => true
#MrYoshiji commented something truthful in my answer - if you are not allowing it to be blank, the easy way is simply not add a blank option in the select
I would recommend to use the :prompt option for your select helper (the documentation on this isn't quite straight forwward, but it's ok in this version of the same thing: http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag). This allows you to define a helper string of text to encourage the user to select an option, but will submit a nil value back when the form is submitted.
<%= f.select :product_name, PRODUCT_NAME_OPTIONS, prompt: 'Select an option' %>
The next thing I'd recommend is... instead of validating presence of on the model, do a validates_inclusion_of. This way you can define the set of options in the model and then use that same set of options in the select helper, above, as well is in your validation. This way you know the form wasn't manipulated to include a different option and it actually keeps things a bit DRYer.
Product
PRODUCT_NAME_OPTIONS = {'opt 1' => 'opt1', 'opt 2' => 'opt2'}
validates_inclusion_of :product_name, :in => PRODUCT_NAME_OPTIONS.values, :message => 'choose from the available options'
Instead of making this too complicated, I just added a length validation since the default is much longer then the product names. Simple is best, however there should be a string validator option.
validates :product_name, presence: true, length: {
maximum: 21,
too_long: ": Must select a product"
}