Ruby-on-rails 验证一个或另一个字段的存在 (XOR)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2134188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:12:21  来源:igfitidea点击:

Validate presence of one field or another (XOR)

ruby-on-rails

提问by Ben Orozco

How do I validate the presence of one field or another but not both and at least one ?

如何验证一个或另一个字段的存在,但不能同时验证至少一个字段的存在?

回答by semanticart

Your code will work if you add conditionals to the numericality validations like so:

如果您将条件添加到数值验证中,您的代码将起作用,如下所示:

class Transaction < ActiveRecord::Base
    validates_presence_of :date
    validates_presence_of :name

    validates_numericality_of :charge, allow_nil: true
    validates_numericality_of :payment, allow_nil: true


    validate :charge_xor_payment

  private

    def charge_xor_payment
      unless charge.blank? ^ payment.blank?
        errors.add(:base, "Specify a charge or a payment, not both")
      end
    end

end

回答by jmanrubia

I think this is more idiomatic in Rails 3+:

我认为这在 Rails 3+ 中更为惯用:

e.g: For validating that one of user_nameor emailis present:

例如:用于验证user_nameemail存在其中之一:

validates :user_name, presence: true, unless: ->(user){user.email.present?}
validates :email, presence: true, unless: ->(user){user.user_name.present?}

回答by Kieran Andrews

class Transaction < ActiveRecord::Base
    validates_presence_of :date
    validates_presence_of :name

    validates_numericality_of :charge, allow_nil: true
    validates_numericality_of :payment, allow_nil: true


    validate :charge_xor_payment

  private

    def charge_xor_payment
      if [charge, payment].compact.count != 1
        errors.add(:base, "Specify a charge or a payment, not both")
      end
    end

end

You can even do this with 3 or more values:

您甚至可以使用 3 个或更多值来执行此操作:

if [month_day, week_day, hour].compact.count != 1

回答by Seoman

Example for rails 3.

导轨示例 3。

class Transaction < ActiveRecord::Base
  validates_presence_of :date
  validates_presence_of :name

  validates_numericality_of :charge, :unless => proc{|obj| obj.charge.blank?}
  validates_numericality_of :payment, :unless => proc{|obj| obj.payment.blank?}


  validate :charge_xor_payment

  private

    def charge_xor_payment
      if !(charge.blank? ^ payment.blank?)
        errors[:base] << "Specify a charge or a payment, not both"
      end
    end
end

回答by chaitanya

 validate :father_or_mother

#Father last name or Mother last name is compulsory

#父亲姓氏或母亲姓氏为必填项

 def father_or_mother
        if father_last_name == "Last Name" or father_last_name.blank?
           errors.add(:father_last_name, "cant blank")
           errors.add(:mother_last_name, "cant blank")
        end
 end

Try above simple example.

试试上面的简单例子。

回答by Neodelf

I put my answer to this question below. In this example :descriptionand :keywordsare fields which one of this not be blank:

我把我对这个问题的回答放在下面。在这个例子中,:description:keywords是其中一个不是空白的字段:

  validate :some_was_present

  belongs_to :seo_customable, polymorphic: true

  def some_was_present
    desc = description.blank?
    errors.add(desc ? :description : :keywords, t('errors.messages.blank')) if desc && keywords.blank?
  end

回答by oklas

Validation using a Proc or Symbol with :if and :unlesswill get called right before validation happens.

使用带有 :if 和 :unlessProc 或 Symbol进行验证将在验证发生之前被调用。

So presence one of both fields may be like this:

所以两个字段之一的存在可能是这样的:

validates :charge,
  presence: true,
  if: ->(user){user.charge.present? || user.payment.present?}
validates :payment,
  presence: true,
  if: ->(user){user.payment.present? || user.charge.present?}

The (example snippet) code has :ifor :unlessas latest item, however as declared in docit will get called right before validation happens - so another checking will works after, if condition match.

(示例代码段)代码具有:if:unless作为最新项目,但是正如在doc 中声明的那样,它将在验证发生之前被调用 - 因此,如果条件匹配,另一个检查将在此之后工作。