Hi, I am using following to validate the contact name like : validates_format_of :merchant_contact_firstname, :with => /^([a-zA-Z ]+)$/, :message=>''First name is not valid.'' But it gives error even if it is left blank. Acually, it is optional but if it is present then it can have only alphabets. How can I validate both presence and format separately in model? Thanks. -- 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 -~----------~----~----~----~------~----~------~--~---
Hi, I would use: validates_format_of :merchant_contact_firstname, :if => Proc.new {|c| not c.merchant_contact_firstname.blank?}, :with => /^([a-zA-Z]+)$/, :message=>''First name is not valid.'' Meaning that when the merchant_contact_firstname is not blank it gets evaluated with the regex. Kind regards, Nick Snels -- http://www.railshostinginfo.com Compare Rails hosting companies -- 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 -~----------~----~----~----~------~----~------~--~---
toulax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-16 12:21 UTC
Re: how to validate only presence or format
I believe tweaking the regexp just a little bit is easier, like this: /^(|[a-zA-Z ]+)$/ or /^([a-zA-Z ]*)$/ Judging by a quick test done in irb both seem to work just fine. Hope it helps! On Feb 16, 5:14 am, Nick Snels <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > I would use: > > validates_format_of :merchant_contact_firstname, :if => Proc.new {|c| > not c.merchant_contact_firstname.blank?}, :with => /^([a-zA-Z]+)$/, > :message=>''First name is not valid.'' > > Meaning that when the merchant_contact_firstname is not blank it gets > evaluated with the regex. > > Kind regards, > > Nick Snels > --http://www.railshostinginfo.com > Compare Rails hosting companies > > -- > 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 -~----------~----~----~----~------~----~------~--~---