I have a phone number field with the following validation: validates_format_of :phone, :with => /^\d\d\d-\d\d\d-\d\d\d\d$/, :message => "The phone number must be of the format: ###-###-####" It works fine, when the phone number is known. But, sometimes the phone number is not known, which causes an error. Is there an elegant way to validate the format only if the field in not empty? I''d rather not write conditional logic into all the forms that use this. -- Best Regards, -Larry "Work, work, work...there is no satisfactory alternative." --- E.Taft Benson --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Larry Kelly wrote:> It works fine, when the phone number is known. But, sometimes the phone > number is not known, which causes an error. Is there an elegant way to > validate the format only if the field in not empty? I''d rather not > write > conditional logic into all the forms that use this. > > -- > Best Regards, > -Larry > "Work, work, work...there is no satisfactory alternative." > --- E.Taft Bensonvalidates_presence_of doesn''t catch it when it''s empty? -- 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 -~----------~----~----~----~------~----~------~--~---
I''ve wondered about this too. Haven''t done anything with it though. It''s kind of hackish, but couldn''t you do something like with => /(^\d\d\d-\d\d\d-\d\d\d\d$|^$)/ to match the correct format OR an empty string? I''ve just confirmed that that does work. Maybe I''ll use that in my models. Thanks. Larry Kelly wrote:> I have a phone number field with the following validation: > > validates_format_of :phone, :with => /^\d\d\d-\d\d\d-\d\d\d\d$/, > :message => "The phone number must be of the format: ###-###-####" > > It works fine, when the phone number is known. But, sometimes the > phone number is not known, which causes an error. Is there an elegant > way to validate the format only if the field in not empty? I''d rather > not write conditional logic into all the forms that use this. > > -- > Best Regards, > -Larry > "Work, work, work...there is no satisfactory alternative." > --- E.Taft Benson > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Thu, 2006-12-28 at 08:07 -0800, Larry Kelly wrote:> I have a phone number field with the following validation: > > validates_format_of :phone, :with => /^\d\d\d-\d\d\d-\d\d\d\d > $/, :message => "The phone number must be of the format: ###-###-####" > > It works fine, when the phone number is known. But, sometimes the > phone number is not known, which causes an error. Is there an elegant > way to validate the format only if the field in not empty? I''d rather > not write conditional logic into all the forms that use this.---- most of the validations provide for :allow_nil but I don''t see that listed in the API for validates_format_of I do see listed :if => which is probably what you are looking for Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the responses, and the friendly help from this list. And thanks Jason for reminding me how incredibly powerful regular expressions are to those who know how to use them. - Larry On 12/28/06, Jason Norris <jasonmnorris-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I''ve wondered about this too. Haven''t done anything with it though. It''s > kind of hackish, but couldn''t you do something like > with => /(^\d\d\d-\d\d\d-\d\d\d\d$|^$)/ to match the correct format OR > an empty string? > I''ve just confirmed that that does work. Maybe I''ll use that in my > models. Thanks. > > Larry Kelly wrote: > > I have a phone number field with the following validation: > > > > validates_format_of :phone, :with => /^\d\d\d-\d\d\d-\d\d\d\d$/, > > :message => "The phone number must be of the format: ###-###-####" > > > > It works fine, when the phone number is known. But, sometimes the > > phone number is not known, which causes an error. Is there an elegant > > way to validate the format only if the field in not empty? I''d rather > > not write conditional logic into all the forms that use this. > > > > -- > > Best Regards, > > -Larry > > "Work, work, work...there is no satisfactory alternative." > > --- E.Taft Benson > > > > > > > >-- Best Regards, -Larry "Work, work, work...there is no satisfactory alternative." --- E.Taft Benson --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---