我编写了一个自定义验证,一旦用户尝试提款超过允许的最小提款金额,就会出错。当用户的提款额低于最小允许金额但代码继续运行时,验证将失败,验证应该失败。它不像对模型字段进行常规验证那样。
validate :minimum_withdrawal_amount, on: :create
validates :amount, numericality: {greater_than: 0}
def minimum_withdrawal_amount
if sum.nil? || sum < currency.min_withdraw_amount
errors.add :base, -> { I18n.t('activerecord.errors.models.withdraw.amount.min_withdraw_amount', currency: currency.key, amount: currency.min_withdraw_amount ) }
end
end
它继续进行并验证其后的金额。如果验证失败,则会出错。我希望我的自定义验证像amount
上的验证一样工作。希望我足够清楚
答案 0 :(得分:0)
Following replacement might work for you: errors.add(:base, I18n.t('activerecord.errors.models.withdraw.amount.min_withdraw_amount', currency: currency.key, amount: currency.min_withdraw_amount ))