Validate presence of one field or another (XOR)

Ruby on-Rails

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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
    return if charge.blank? ^ payment.blank?

    errors.add(:base, 'Specify a charge or a payment, not both')
  end
end

Solution 2 - Ruby on-Rails

I think this is more idiomatic in Rails:

e.g: For validating that one of user_name or email is present:

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

Solution 3 - Ruby on-Rails

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:

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

Solution 4 - Ruby on-Rails

Example for rails 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

Solution 5 - Ruby on-Rails

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

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 :if or :unless as latest item, however as declared in doc it will get called right before validation happens - so another checking will works after, if condition match.

Solution 6 - Ruby on-Rails

 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.

Solution 7 - Ruby on-Rails

I put my answer to this question below. In this example :description and :keywords are fields which one of this not be blank:

  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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBen OrozcoView Question on Stackoverflow
Solution 1 - Ruby on-RailssemanticartView Answer on Stackoverflow
Solution 2 - Ruby on-RailsjmanrubiaView Answer on Stackoverflow
Solution 3 - Ruby on-RailsKieran AndrewsView Answer on Stackoverflow
Solution 4 - Ruby on-RailsSeomanView Answer on Stackoverflow
Solution 5 - Ruby on-RailsoklasView Answer on Stackoverflow
Solution 6 - Ruby on-RailschaitanyaView Answer on Stackoverflow
Solution 7 - Ruby on-RailsNeodelfView Answer on Stackoverflow