Hi, there. I have two validations in the model: validates_numericality_of :value, :only_integer=>true, :allow_blank=>true validates_size_of :value, :is=>9, :message=>"must be 5-digit number", :if=>Proc.new{|u| u.value.is_a?(Numeric)} They work as expected except when the :value is character/string like "a" "abc", the second validation will also output error message "must be 5-digit number". What I want is the second validation displays error message only when the :value is a number. I think :if=>Proc.new{|u| u.value.is_a?(Numeric)} would take care of it, but it didn''t. The second problem is about the custom validation. I got a very simple custom validation. Whenever there is an error, it catches it and shows the error message. But the field with error is not highlighted. I checked the source of the page, the "fieldwitherror" was not generated on the field checked by the custom validation. I tried the validation_reflection plugin. It catched the error but there''s no error message under the field. Normally that''s where the message should be. I guess they are related but I am not sure what causes it. Any help will be appreciated. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> validates_numericality_of :value, :only_integer=>true, > :allow_blank=>true > validates_size_of :value, :is=>9, :message=>"must be 5-digit number", > :if=>Proc.new{|u| u.value.is_a?(Numeric)}Try it as u.is_a?(Numeric) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
E. Litwin wrote:>> validates_numericality_of :value, :only_integer=>true, >> :allow_blank=>true >> validates_size_of :value, :is=>9, :message=>"must be 5-digit number", >> :if=>Proc.new{|u| u.value.is_a?(Numeric)} > > Try it as u.is_a?(Numeric)Thanks Litwin. I tried that before but it made the second validation stop working. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> validates_size_of :value, :is=>9, :message=>"must be 5-digit number", > :if=>Proc.new{|u| u.value.is_a?(Numeric)}Looks corrent to me. Maybe the name of the field "value" is causing a conflict somewhere. Try to rename the field to something else just to test. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sharagoz -- wrote:>> validates_size_of :value, :is=>9, :message=>"must be 5-digit number", >> :if=>Proc.new{|u| u.value.is_a?(Numeric)} > Looks corrent to me. Maybe the name of the field "value" is causing a > conflict somewhere. Try to rename the field to something else just to > test.Thanks Sharagoz. I tried different names for the field but none of those worked. I just wrote a custom validation to workaround it although still can''t figure it out. Anyone has any idea about the second problem? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Feb 9, 2010 at 4:03 PM, Ichiro Saga <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, there. > I have two validations in the model: > > validates_numericality_of :value, :only_integer=>true, > :allow_blank=>true > validates_size_of :value, :is=>9, :message=>"must be 5-digit number", > :if=>Proc.new{|u| u.value.is_a?(Numeric)} > >Ichiro, you have a bit of duplication within your validates_*. For example, you''re testing whether or not :value is numeric within both validates_* methods. Next, the :is compares the number of bytes and not the number of characters you should be able to do the following: validates_numericality_of :value, :only_integer => true, :allow_blank => true # make sure that it''s an integer validate :number_of_digits # make sure that we have five digits private def number_of_digits unless self.value.to_s.size == 5 errors.add :value, "should be 5 digits" end end Good luck, -Conrad> They work as expected except when the :value is character/string like > "a" "abc", the second validation will also output error message "must be > 5-digit number". What I want is the second validation displays error > message only when the :value is a number. I think :if=>Proc.new{|u| > u.value.is_a?(Numeric)} would take care of it, but it didn''t. > > The second problem is about the custom validation. I got a very simple > custom validation. Whenever there is an error, it catches it and shows > the error message. But the field with error is not highlighted. I > checked the source of the page, the "fieldwitherror" was not generated > on the field checked by the custom validation. I tried the > validation_reflection plugin. It catched the error but there''s no error > message under the field. Normally that''s where the message should be. > I guess they are related but I am not sure what causes it. > > Any help will be appreciated. > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> validates_numericality_of :value, :only_integer => true, :allow_blank > => > true # make sure that it''s an integer > validate :number_of_digits > # make sure that we have five digits > > private > > def number_of_digits > unless self.value.to_s.size == 5 > errors.add :value, "should be 5 digits" > end > end > > Good luck, > > -ConradHi, Conrad. Thank you for the explanation. My workaround looks similar to your code and works. I still have no clue why :if=>Proc.new{...} didn''t work though. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.