Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 320
Doctor06 Avatar asked Oct 20 '14 16:10

Doctor06


2 Answers

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

validates :email, presence: { if: -> { username.blank? } }
validates :username, presence: { if: -> { email.blank? } }
like image 151
Vitalii Prodan Avatar answered Oct 13 '22 19:10

Vitalii Prodan


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
like image 24
Doctor06 Avatar answered Oct 13 '22 18:10

Doctor06