Forum: Ruby on Rails Validate presence of 2 fields

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8c49880a7f274947b01d576c5828a5f6?d=identicon&s=25 Lon Baker (Guest)
on 2005-12-13 21:52
(Received via mailing list)
Is it possible top validate the presence of 2 field?

Maybe I am blind, but I can't figure out how to validate based on the
presence of 2 fields.

--
Lon Baker
http://www.speedymac.com
AIM: spdemac
4bd34a2216dc8bdbf1f017f64e4d59e8?d=identicon&s=25 Kyle Maxwell (Guest)
on 2005-12-13 22:04
(Received via mailing list)
Just do validates_presence_of on each?  I suspect that that's not what
you wanted to hear, and that I didn't understand your problem, so to
roll your own validation, you can use validates_each.  Example:

validates_each(:field1, :field2) do |record, field_name, field_value|
  #This block is run once for each field, so 2x in this example.
  record.errors.add "is invalid" if some_test.fails?
end
This topic is locked and can not be replied to.