Love AJAX
2007-Nov-20 11:50 UTC
Other validations fail even if there is no validates_presence_of
Hi, I have a set of validations like: validates_format_of :city, :with => /^[a-zA-Z\s\''\&]+$/, :message => "is not a valid name" validates_format_of :zip, :with => /^[\d]{5}+$/, :allow_nil => true, :message => "is not a valid zip code" But I don''t have a validates_presence_of :city, :zip. I want to validate the format only if user has entered some value into the fields. But both the _format_of hooks are trying to validate an empty value. Any idea how I can stop this behavior? Thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Nov-20 15:53 UTC
Re: Other validations fail even if there is no validates_presence_of
Add :allow_nil => true to the city validation. You only have it on the zip code validation. -Bill Love AJAX wrote:> Hi, > > I have a set of validations like: > validates_format_of :city, > :with => /^[a-zA-Z\s\''\&]+$/, > :message => "is not a valid name" > > validates_format_of :zip, > :with => /^[\d]{5}+$/, > :allow_nil => true, > :message => "is not a valid zip code" > > But I don''t have a validates_presence_of :city, :zip. > > I want to validate the format only if user has entered some value into > the fields. But both the _format_of hooks are trying to validate an > empty value. Any idea how I can stop this behavior? > > Thanks. > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Love AJAX
2007-Nov-21 06:12 UTC
Re: Other validations fail even if there is no validates_presence_of
:allow_nil => true didn''t work on these validations. Instead I had to do something like :if => Proc.new{|profile| profile != ""}. This works fine. So my code now looks like this: validates_format_of :zip, :with => /^[\d]{5}+$/, :if => Proc.new {|profile| profile.zip != ""}, :message => "is not a valid zip code" validates_format_of :city, :with => /^[a-zA-Z\s\''\&]+$/, :if => Proc.new {|profile| profile.city != ""}, :message => "is not a valid name" See the last sentence of http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000958. Thanks much. On 11/20/07, William Pratt <billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> > Add :allow_nil => true to the city validation. You only have it on the > zip code validation. > > -Bill > > Love AJAX wrote: > > Hi, > > > > I have a set of validations like: > > validates_format_of :city, > > :with => /^[a-zA-Z\s\''\&]+$/, > > :message => "is not a valid name" > > > > validates_format_of :zip, > > :with => /^[\d]{5}+$/, > > :allow_nil => true, > > :message => "is not a valid zip code" > > > > But I don''t have a validates_presence_of :city, :zip. > > > > I want to validate the format only if user has entered some value into > > the fields. But both the _format_of hooks are trying to validate an > > empty value. Any idea how I can stop this behavior? > > > > Thanks. > > > > > > > > > -- > Sincerely, > > William Pratt > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---