Min value for input in Simple Form
Definitely! The (simplified) HTML for the form you want looks something like this:
<form>
<input type="number" min="0" step="any" name="amount">
</form>
Notice the two really important properties on the input field: min
is set to 0, because you don't allow negative numbers, and step
is set to any
, because you want to allow decimals in. If you only wanted even dollar amounts, you could set step="1"
, for example. Also, if you wanted to set a max value you could with the max
attribute.
Important Note: this only performs validation on the client side. It's possible to override this if you know what you're doing, so make sure you still validate that the input sent with this form is valid on the server.
I'm not a Ruby guy, but reading through the Simple Form docs, I think this should work:
<%= simple_form_for @transaction do |f| %>
<%= f.input :amount, input_html: { min: '0', step: 'any' } %>
<%= f.button :submit %>
<% end %>
Assuming amount is an number type in your model, it will automatically make an input[type=number] for you.
For @georgebrock's answer to work, in simple_form.rb (or whatever name you do use, could be simple_form_bootstrap.rb) you need to change:
b.optional :min_max
To:
b.use :min_max
For the wrapper that's being used to draw the input.
The "optional" means simple form will use the component but will not lookup automatically, letting you to specify :min and :max params on the input manually.
simple_form
will set the input attributes based on the model's validations, which means that your client-side and server-side validations will match and cover users with fancy modern browsers that support HTML5's new input types and attributes, and users with older browsers that do not.
For example, given this model:
class Purchase < ActiveRecord::Base
validates :amount, numericality: { greater_than_or_equal_to: 1 }
end
And this view:
<%= simple_form_for Purchase.new do |f| %>
<%= f.input :amount %>
<% end %>
The resulting markup will look something like this (I've removed some attributes for clarity):
<form …>
<div>
<label for="purchase_amount">Amount</label>
<input id="purchase_amount" name="purchase[amount]"
min="1" step="1" type="number" />
</div>
</form>
As noted in another answer, if you want to override the default attributes you can use the input_html
option when calling #input
:
<%= simple_form_for Purchase.new do |f| %>
<%= f.input :amount, input_html: { min: 0 } %>
<% end %>