Robert B
Robert B

Reputation: 2883

Validate presence of field only if another field is blank - Rails

I have a form with a mobile/cell number and a home phone number.

I want to have only validate presence of mobile/cell number if the phone number has been left blank or vice versa.

My current validations for these fields are as follows.

validates_presence_of :mobile_number
validates_presence_of :home_phone

validates_length_of :home_phone, :minimum => 12, :maximum => 12
validates_length_of :mobile_number, :minimum => 10, :maximum => 10, :allow_blank => true

validates_format_of :home_phone, :with => /\A[0-9]{2}\s[0-9]{4}\s[0-9]{4}/, :message => "format should be 02 9999 9999"

I thought I could have something like the following but not sure how to do this exactly.

validates_presence_of :mobile_number, :unless => :home_phone.blank?

I'm using Rails 3.

Upvotes: 71

Views: 58682

Answers (7)

epicrato
epicrato

Reputation: 8408

Tested in Rails 7, this works flawlessly:

validates :mobile_number, presence: { unless: :home_phone }

Upvotes: 9

shime
shime

Reputation: 9008

In newer versions of Rails, instead of relying on old validates_presence_of, you should use validates and list validations for each attribute:

validates :mobile_number, presence: { if: -> { home_phone.present? } }

Upvotes: 1

Rob Davis
Rob Davis

Reputation: 15772

You don't need a lambda. This will do:

validates_presence_of :mobile_number, :unless => :home_phone?

Also, all of the validators take the same if/unless options, so you can make them conditional at will.

Update: Looking back at this answer a few days later, I see that I should explain why it works:

  • If you set a validator's :unless option to be a symbol, Rails will look for an instance method of that name, invoke that method on the instance that's being validated -- at validation time -- and only perform the validation if the method returns false.
  • ActiveRecord automatically creates question mark methods for each of your model's attributes, so the existence of a home_phone column in your model's table causes Rails to create a handy #home_phone? method. This method returns true if and only if home_phone is present (i.e. not blank). If the home_phone attribute is nil or an empty string or a bunch of white space, home_phone? will return false.

UPDATE: Confirmed that this old technique continues to work in Rails 5.

Upvotes: 138

IAmNaN
IAmNaN

Reputation: 10582

Starting in Rails 4, you can pass a block to presence. Concisely:

validates :mobile_number, presence: {unless: :home_phone?}

Also, :home_phone? returns false for nil or blank.

Upvotes: 9

John
John

Reputation: 397

a short solution:

validates_presence_of :mobile_number, unless: -> { home_phone.blank? }

Upvotes: 1

Chris Hough
Chris Hough

Reputation: 3558

Here is another way that works in rails 4

  validates_presence_of :job, if: :executed_at?

  validates :code,
            presence: true,
            length:  { minimum: 10, maximum: 50 },
            uniqueness: { case_sensitive: false },
            numericality: { only_integer: true }

Upvotes: 4

Ryan Bigg
Ryan Bigg

Reputation: 107718

You must use a lambda / Proc object:

validates_presence_of :mobile_number, :unless => lambda { self.home_phone.blank? }

Upvotes: 23

Related Questions