shimon.amit-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-25 14:15 UTC
How does one validate a ''float'' field?
I would like to validate the following conditions on a numeric field: 1) That the user did not enter a non-numeric string where he was required to enter a number. 2) That the numeric value is zero or greater. There seems to be a conflict between these two requirements: If the user enters a non-numeric string (e.g "ABC"), by the time it reaches the validation function within the ActiveRecord object, it has already been marshalled into a numeric format. "ABC" will be converted to zero. According to condition #2, this is an acceptable value. According to condition #1, it is not. Is there anyway I can execute the validation methods on the raw string values, before they are "casted" into the ActiveRecord types? Otherwise, can you suggest an elegant solution for this problem? Thanks! Shimon Amit --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
unknown wrote:> I would like to validate the following conditions on a numeric field: > > 1) That the user did not enter a non-numeric string where he was > required to enter a number. > 2) That the numeric value is zero or greater. > > There seems to be a conflict between these two requirements: > > If the user enters a non-numeric string (e.g "ABC"), by the time it > reaches the validation function within the ActiveRecord object, it has > already been marshalled into a numeric format. "ABC" will be converted > to zero. According to condition #2, this is an acceptable value. > According to condition #1, it is not. > > Is there anyway I can execute the validation methods on the raw string > values, before they are "casted" into the ActiveRecord types? > Otherwise, can you suggest an elegant solution for this problem? > > Thanks! > > Shimon AmitLook through http://ar.rubyonrails.com/classes/ActiveRecord/Base.html for before_type_cast But maybe you are really looking for validates_numericality_of Stephan -- 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 -~----------~----~----~----~------~----~------~--~---
shimon.amit-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-25 18:30 UTC
Re: How does one validate a ''float'' field?
Thank you. That is exactly what I was looking for. Shimon Amit On Apr 25, 7:36 pm, Stephan Wehner <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> unknown wrote: > > I would like to validate the following conditions on a numeric field: > > > 1) That the user did not enter a non-numeric string where he was > > required to enter a number. > > 2) That the numeric value is zero or greater. > > > There seems to be a conflict between these two requirements: > > > If the user enters a non-numeric string (e.g "ABC"), by the time it > > reaches the validation function within the ActiveRecord object, it has > > already been marshalled into a numeric format. "ABC" will be converted > > to zero. According to condition #2, this is an acceptable value. > > According to condition #1, it is not. > > > Is there anyway I can execute the validation methods on the raw string > > values, before they are "casted" into the ActiveRecord types? > > Otherwise, can you suggest an elegant solution for this problem? > > > Thanks! > > > Shimon Amit > > Look through > > http://ar.rubyonrails.com/classes/ActiveRecord/Base.html > > for before_type_cast > > But maybe you are really looking for validates_numericality_of > > Stephan > > -- > Posted viahttp://www.ruby-forum.com/.- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---