gpt4 book ai didi

ruby-on-rails - 如何从 spree 中删除模型验证

转载 作者:行者123 更新时间:2023-12-05 06:40:38 24 4
gpt4 key购买 nike

我在这个问题上苦苦挣扎了 6 个多小时。

我想从 Spree::Order 中删除验证

module Spree
class Order < Spree::Base
MONEY_THRESHOLD = 100_000_000
MONEY_VALIDATION = {
presence: true,
numericality: {
greater_than: -MONEY_THRESHOLD,
less_than: MONEY_THRESHOLD,
allow_blank: true
},
format: { with: /\A-?\d+(?:\.\d{1,2})?\z/, allow_blank: true }
}.freeze

NEGATIVE_MONEY_VALIDATION = MONEY_VALIDATION.deep_dup.tap do |validation|
validation.fetch(:numericality)[:less_than_or_equal_to] = 0
end.freeze

validates :promo_total, NEGATIVE_MONEY_VALIDATION

# ...
end
end

这是我的装饰器

Spree::Order.class_eval do
# This is not working
_validators.delete(:promo_total)

# Nope
_validators.reject!{ |key, _| key == :promo_total }

# Nope
def validate
errors = self.errors.map {|attr, message| [attr, message]}
self.errors.clear
errors.each do |attr, message|
self.errors.add(attr, message) unless attr == "promo_total"
end
end
# ...
end

当我检查验证时,我在 :promo_total 上收到错误

self.valid?
#=> false

self.errors.messages
#=> {:promo_total=>["must be less than or equal to 0"]}

最佳答案

你应该试试这个 gem https://github.com/workarea-commerce/active_model-unvalidate

即使在最新版本中也应该可以工作

关于ruby-on-rails - 如何从 spree 中删除模型验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42304239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com