简体   繁体   中英

Validate presence of one field or another or both(OR)

So I'm looking at this link but I'm wondering how to do an 'OR' instead of an 'XOR'. Does anyone know how to do this? I am trying to do this using Rails 3.

There is short option for Rails 4 (not sure about Rails 3) :

validates :email, presence: { if: -> { username.blank? } }
validates :username, presence: { if: -> { email.blank? } }

I figured it out. all you have to change is the ^ to &&

private
def time_or_money
  if time.blank? && money.blank?
    errors[:base] << "Specify Time, Money or Both."
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM