I want to create a simple money input with SimpleForm.
The money field should have a minimum value of 0, so that a user cannot enter a value less than 0.
However
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):
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 %>