【问题标题】:Numericality Validations, Catching nil Values on Comparisons数值验证,在比较中捕获 nil 值
【发布时间】:2011-03-06 05:25:23
【问题描述】:

我有一个包含数字范围和描述 [min_val, max_val, name] 的对象。 我需要验证 min_val

另外,如何更改数字的错误消息?

validates :min_val, :presence => true, :numericality => {:greater_than => 0, :less_than => :max_val}

validates :max_val, :presence => true, :numericality => {:greater_than => 0, :greater_than => :min_val}

validates :name, :presence => true, :if => Proc.new { |r| !r.min_val.nil? || !r.max_val.nil? }

【问题讨论】:

    标签: ruby-on-rails validation


    【解决方案1】:

    您可以使用:message 指定自定义错误消息。

    validates :max_val, :presence => true, :numericality => {:greater_than => 0, :message => " is an invalid number."}
    validates :min_val, :presence => true, :numericality => {:greater_than => 0, :message => " is an invalid number."}
    validate do |record|
      record.errors.add_to_base("The min_val should be less than max_val") if min_val.to_i >= max_val.to_i
    end
    validates :name, :presence => true, :if => Proc.new { |r| !r.min_val.nil? || !r.max_val.nil? }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 2013-04-05
      相关资源
      最近更新 更多