【问题标题】:validates_presence_of if condition on rails 3.2 and mongoid + simple_formvalidates_presence_of if 在 rails 3.2 和 mongoid + simple_form 上的条件
【发布时间】:2012-05-08 15:11:49
【问题描述】:

如果属性:shipping 等于true,我想验证这两个属性:shipping_cost:shipping_cost_anywhere 的存在。如果

我的模型中有这个,但对我来说不能正常工作:

validates_presence_of :shipping_cost, :shipping_cost_anywhere, :allow_blank => "true" if :shipping == "true"

这是我的 :shipping 属性:

field :shipping, :type => Boolean, :default => "false"

我该怎么做?

谢谢!

已编辑。

我正在使用 mongoid 和 simple_form gems

【问题讨论】:

  • 请更具体地说明它对您的影响。这使您更容易猜测您试图表达的内容。 :)
  • 谢谢,如果我将字段 :shipping 设置为 true,则字段 :shipping_cost:shipping_cost_anywhere 不会被验证

标签: ruby-on-rails ruby-on-rails-3 validation


【解决方案1】:
validates_presence_of :shipping_costs_anywhere, :if => :should_be_filled_in?

def should_be_filled_in?
  shipping_costs_anywhere == "value"
end

该方法在语句中被调用时将返回 true 或 false。 无需在 shipping_costs_anywhere 前面加冒号。

【讨论】:

  • 我用这个方法undefined method should_be_filled_in? for #<Item:0xfd43028>
  • 方法应该是should_be_filled_in?而不是 should_be_filled_in
  • 谢谢你,我已经检查过了,但它对我来说效果不佳。谢谢!
  • 当使用:should_be_filled_in?它检查 2 个值,无论是真还是假,确保将其分配给表单中的 :shipping_costs_anywhere。当返回为真时,您想问自己:shipping_costs_anywhere == true。如果您想检查它是否填写为空白,您可能希望为 :shipping_costs_anywhere != ""
  • 条件验证需要方法的符号
【解决方案2】:

对我来说这个问题的解决方法是下一个代码:

validates :shipping_cost, :shipping_cost_anywhere, :presence => true, :if => :shipping?

感谢大家的帮助,但任何答案都对我有用。谢谢!

【讨论】:

    【解决方案3】:

    今天偶然发现了这个,并认为我会更新答案。正如其他人提到的,您可以将逻辑放入函数中。但是,您也可以将其放入 proc 中。

    validates_presence_of :shipping_costs_anywhere, :if => Proc.new { |o|
      o.shipping_costs_anywhere == "value"}
    

    http://guides.rubyonrails.org/active_record_validations.html#using-a-symbol-with-if-and-unless

    【讨论】:

      【解决方案4】:

      validates 现在优先于 validates_presences_of 等。正如 hyperjas 提到的,您可以这样做:

      validates :shipping_cost,
        :shipping_cost_anywhere,
        :presence => true, :if => :shipping?
      

      但是,这会限制:shipping_cost:shipping_cost_anywhere 的整个验证。为了更好的可维护性,我更喜欢为每个属性单独声明validate

      更重要的是,您可能会遇到多个验证条件不同的情况(例如一个用于存在,另一个用于长度、格式或值)。你可以这样做:

      validates :shipping_cost,
        presence: { if: :shipping? },
        numericality: { greater_than: 100, if: :heavy? }
      

      你也可以让 rails 计算一个 ruby​​ 字符串。

      validates :shipping_cost,
        presence: { if: "shipping?" },
        numericality: { greater_than: 100, if: "shipping? and heavy?" }
      

      最后,可选择添加单独的自定义消息:

      validates :shipping_cost,
        presence: { if: "shipping?", message: 'You forgot the shipping cost.' },
        numericality: { greater_than: 100, if: "shipping? and heavy?", message: 'Shipping heavy items is $100 minimum.' }
      

      等等。希望对您有所帮助。

      【讨论】:

        【解决方案5】:

        我无法测试它,但我认为语法更像:

        validates_presence_of :shipping_cost, :shipping_cost_anywhere, :allow_blank => "true", :if => "shipping.nil?"
        

        见:

        http://guides.rubyonrails.org/active_record_validations_callbacks.html#conditional-validation

        【讨论】:

        • 您也可以按照其他人的建议定义方法。顺便说一句,我还注意到 Rails 指南提到存在验证器忽略 :allow_blank 选项,但如果这对你有用,我不会争论。
        • 根据您的问题和稍后的评论,我不清楚您是否要验证何时发货为真,或何时为真。要验证何时发货为真,请使用:unless。如果您想验证何时发货不是为真,请使用:if
        • 谢谢你的问题是如果我删除:allow_blank => "true"总是验证字段,条件被忽略,但如果我添加:allow_blank => "true"从不验证表单条件被忽略...
        【解决方案6】:

        这是我为我工作的代码。在 if 条件下调用方法而不是比较

         validates :prefix, :allow_blank => true, :uniqueness => { :case_sensitive => true } ,:if => :trunk_group_is_originating?
        
        
                def trunk_group_is_originating?
                  if self.direction == "originating"
                    true
                  else
                    false
                  end
                end
        

        希望对你有帮助......

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多