Validation for non-negative integers and decimal values

前端 未结 3 1522
暗喜
暗喜 2021-02-01 13:44

My fields are: tax rate and tax amount in which I want to validate positive values.

I wrote this validation:

:format => { :w         


        
相关标签:
3条回答
  • 2021-02-01 13:57

    You could use:

    validates :tax_rate, inclusion: { in: 0..5 }
    

    It allows values like: 0, 2, 1.2, 3.2

    Hope it helps!

    0 讨论(0)
  • 2021-02-01 14:10

    you could use validates_numericality_of :amount, :greater_than => 0.0

    0 讨论(0)
  • 2021-02-01 14:17

    Doesn't this work?

    validates :your_field, :numericality => { :greater_than_or_equal_to => 0 }
    

    (guess for taxes following rule will be more correct:)

    validates :your_field, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100 }
    
    0 讨论(0)
提交回复
热议问题