I have a model with a home and work phone number and I just want to validate that one of the two are present and correct. Would I have to write my own validation method? If so what is wrong with below?: def validate errors.add("Either home email or work email need to be entered.") unless home_email.nil? or work_email.nil? end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Seth Buntin wrote:> I have a model with a home and work phone number and I just want to > validate that one of the two are present and correct. Would I have to > write my own validation method? If so what is wrong with below?: > > def validate > errors.add("Either home email or work email need to be entered.") > unless home_email.nil? or work_email.nil? > endIt would be simpler to do something like: validates_presence_of :home_email, :work_email, :message => "Either home email or work email need to be entered." -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Seth Buntin wrote:> def validate > errors.add("Either home email or work email need to be entered.") > unless home_email.nil? or work_email.nil? > endOverriding validate directly isn''t recommended. Try: validate_on_create do |record| record.errors.add("", "Either home email or work email need to be entered.") if record.home_email.blank? || record.work_email.blank? end J. Yaunches wrote:> validates_presence_of :home_email, :work_email, :message => "Either home > email or work email need to be entered."That would trigger an error unless both are provided. I think Seth wants an error only if none are given. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> validate_on_create do |record| > record.errors.add("", "Either home email or work email need to be > entered.") if record.home_email.blank? || record.work_email.blank? > endSorry change the "||" in the if statement to "&&" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Actually I wanted to test that at least one are provided.> > That would trigger an error unless both are provided. I think Seth > wants an error only if none are given.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
validate_on_create do |record| record.errors.add("Home or work email", "need to be entered.") if record.home_email.blank? && record.work_email.blank? end should work then. Your original ''unless'' expression was saying they both needed to be provided. On May 5, 10:52 pm, Seth Buntin <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Actually I wanted to test that at least one are provided. > > > > > That would trigger an error unless both are provided. I think Seth > > wants an error only if none are given. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---