Min value for input in Simple Form

前端 未结 4 820
耶瑟儿~
耶瑟儿~ 2021-02-18 21:42

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

相关标签:
4条回答
  • 2021-02-18 22:06

    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.

    0 讨论(0)
  • 2021-02-18 22:18

    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.

    0 讨论(0)
  • 2021-02-18 22:23

    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 %>
    
    0 讨论(0)
  • 2021-02-18 22:30

    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.

    0 讨论(0)
提交回复
热议问题