Min value for input in Simple Form

非 Y 不嫁゛ 提交于 2019-12-21 06:59:58

问题


I want to create a simple money input with SImpleForm.

So the money field should have a minimum value which is 0, so that user cannot enter value less than 0.

However I don't know how to define min value with simple form.

I wonder if it's possible.


回答1:


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.




回答2:


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 %>



回答3:


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.




回答4:


The answer of @alexander-s is normally right.

However, I had an issue with simple_form.rb being run over by simple_form_bootstrap.rb that they instruct you NOT to change. It lists b.optional :min_max many places.

Instead of the global setting of b.use :min_max mentioned by @alexander-s, I used

<%= f.input :amount, min_max: true %>

This is better as it reuses the min number set in your model.



来源:https://stackoverflow.com/questions/23190542/min-value-for-input-in-simple-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!