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
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 On 12/13/05, Lon Baker <lon-JCsVN+o+Xp98UrSeD/g0lQ@public.gmane.org> wrote:> 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 > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Sorry. I wasn''t clear. What I am trying to do is only validate the object for save, if either: Both objects are present OR Both objects are missing If on object exists, but the other doesn''t validate the presence is not useful. On Dec 13, 2005, at 4:14 PM, rails-request-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org wrote:> 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.-- Lon Baker http://www.speedymac.com AIM: spdemac
validates_each :field1 do |record, attr, value| record.errors.add "should not exist alone" if !value.blank? && record.field2.blank? end On 12/13/05, Lon Baker <lon-JCsVN+o+Xp98UrSeD/g0lQ@public.gmane.org> wrote:> Sorry. I wasn''t clear. > > What I am trying to do is only validate the object for save, if either: > > Both objects are present > > OR > > Both objects are missing > > If on object exists, but the other doesn''t validate the presence is > not useful. > > On Dec 13, 2005, at 4:14 PM, rails-request-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org wrote: > > > 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. > > -- > Lon Baker > http://www.speedymac.com > AIM: spdemac > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks! Worked wonderfully. Just had to add to add the attr after record.errors.add, like so: record.errors.add attr, "should not exist alone" if !value.blank? && On Dec 13, 2005, at 5:37 PM, rails-request-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org wrote:> validates_each :field1 do |record, attr, value| > record.errors.add "should not exist alone" if !value.blank? && > record.field2.blank? > end-- Lon Baker http://www.speedymac.com AIM: spdemac