Hi, What is the simplest way to validate a positive integer? validates_numericality_of :foo, :integer_only => true how do I add the positive part? Do I need another validation statement for pattern matching or do I have to write a validate() funciton for my model? Thanks, Peter
Peter Michaux
2006-Jan-23 21:51 UTC
[Rails] Re: validates_numericality_of positive integer
anyone? On 1/19/06, Peter Michaux <petermichaux@gmail.com> wrote:> Hi, > > What is the simplest way to validate a positive integer? > > validates_numericality_of :foo, :integer_only => true > > how do I add the positive part? Do I need another validation statement > for pattern matching or do I have to write a validate() funciton for > my model? > > Thanks, > Peter >
Wilson Bilkovich
2006-Jan-23 22:49 UTC
[Rails] Re: validates_numericality_of positive integer
On 1/23/06, Peter Michaux <petermichaux@gmail.com> wrote:> anyone? > > On 1/19/06, Peter Michaux <petermichaux@gmail.com> wrote: > > Hi, > > > > What is the simplest way to validate a positive integer? > > > > validates_numericality_of :foo, :integer_only => true > > > > how do I add the positive part? Do I need another validation statement > > for pattern matching or do I have to write a validate() funciton for > > my model? > >I''d go with this, personally: def validate unless foo_before_type_cast.to_s =~ /^[+]?\d+$/ errors.add("foo", "is not a valid number") end end 500 and +500 are valid, -500, -500.00, 400.12, etc, are not.
Peter Michaux wrote:> anyone? > > On 1/19/06, Peter Michaux <petermichaux@gmail.com> wrote: >> Hi, >> >> What is the simplest way to validate a positive integer? >> >> validates_numericality_of :foo, :integer_only => true >> >> how do I add the positive part? Do I need another validation statement >> for pattern matching or do I have to write a validate() funciton for >> my model?Write your own function, I reckon. -- Alex
I hacked together a plugin to add a few parameters to validates_numericality_of based on your needs and my laziness for explicit checks.>From the README:Adds the following parameters: (plus the messages generated as an error) :gt => ## "must be greater than ##" :gte => ## "must be greater than or equal to ##" :eq => ## "must be equal to ##" :lt => ## "must be less than ##" :lte => ## "must be less than or equal to ##" :odd => ## "must be an odd number" :even => ## "must be an even number" :within => ##..## "must be within ## and ##" Usage: (in your case) validates_numericality_of :foo, :integer_only => true, :gt => 0 validates_numericality_of :age, :integer_only => true, :within => 13..100 If RoR team would like to integrate into Rails, have at it. I don''t do well with rejection, so I won''t offer it as a patch unless requested. Download it at: http://www.umesd.com/better_validates_numericality_of.zip I''ll get it downloadable via script/plugin in due time. Gotta write a few unit tests first. Enjoy, Bob Silva> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of Wilson Bilkovich > Sent: Monday, January 23, 2006 2:49 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Re: validates_numericality_of positive integer > > On 1/23/06, Peter Michaux <petermichaux@gmail.com> wrote: > > anyone? > > > > On 1/19/06, Peter Michaux <petermichaux@gmail.com> wrote: > > > Hi, > > > > > > What is the simplest way to validate a positive integer? > > > > > > validates_numericality_of :foo, :integer_only => true > > > > > > how do I add the positive part? Do I need another validation statement > > > for pattern matching or do I have to write a validate() funciton for > > > my model? > > > > > I''d go with this, personally: > def validate > unless foo_before_type_cast.to_s =~ /^[+]?\d+$/ > errors.add("foo", "is not a valid number") > end > end > > 500 and +500 are valid, -500, -500.00, 400.12, etc, are not. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Peter Michaux
2006-Jan-26 02:26 UTC
[Rails] Re: validates_numericality_of positive integer
Hi Bob, Thanks for the plugin. I think this should be included in rails as these are some very standard things to check. Peter On 1/23/06, Bob Silva <me@bobsilva.com> wrote:> I hacked together a plugin to add a few parameters to > validates_numericality_of based on your needs and my laziness for explicit > checks. > > >From the README: > > Adds the following parameters: > (plus the messages generated as an error) > > :gt => ## "must be greater than ##" > :gte => ## "must be greater than or equal to ##" > :eq => ## "must be equal to ##" > :lt => ## "must be less than ##" > :lte => ## "must be less than or equal to ##" > :odd => ## "must be an odd number" > :even => ## "must be an even number" > :within => ##..## "must be within ## and ##" > > Usage: (in your case) > > validates_numericality_of :foo, :integer_only => true, :gt => 0 > > validates_numericality_of :age, :integer_only => true, :within => 13..100 > > > If RoR team would like to integrate into Rails, have at it. I don''t do well > with rejection, so I won''t offer it as a patch unless requested. > > > Download it at: > > http://www.umesd.com/better_validates_numericality_of.zip > > I''ll get it downloadable via script/plugin in due time. Gotta write a few > unit tests first. > > Enjoy, > > Bob Silva > > > > > -----Original Message----- > > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > > bounces@lists.rubyonrails.org] On Behalf Of Wilson Bilkovich > > Sent: Monday, January 23, 2006 2:49 PM > > To: rails@lists.rubyonrails.org > > Subject: Re: [Rails] Re: validates_numericality_of positive integer > > > > On 1/23/06, Peter Michaux <petermichaux@gmail.com> wrote: > > > anyone? > > > > > > On 1/19/06, Peter Michaux <petermichaux@gmail.com> wrote: > > > > Hi, > > > > > > > > What is the simplest way to validate a positive integer? > > > > > > > > validates_numericality_of :foo, :integer_only => true > > > > > > > > how do I add the positive part? Do I need another validation statement > > > > for pattern matching or do I have to write a validate() funciton for > > > > my model? > > > > > > > > I''d go with this, personally: > > def validate > > unless foo_before_type_cast.to_s =~ /^[+]?\d+$/ > > errors.add("foo", "is not a valid number") > > end > > end > > > > 500 and +500 are valid, -500, -500.00, 400.12, etc, are not. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Peter Michaux
2006-Jan-26 02:27 UTC
Fwd: [Rails] Re: validates_numericality_of positive integer
Maybe the core could make a request? Peter ---------- Forwarded message ---------- From: Bob Silva <me@bobsilva.com> Date: Jan 23, 2006 7:34 PM Subject: RE: [Rails] Re: validates_numericality_of positive integer To: rails@lists.rubyonrails.org I hacked together a plugin to add a few parameters to validates_numericality_of based on your needs and my laziness for explicit checks.>From the README:Adds the following parameters: (plus the messages generated as an error) :gt => ## "must be greater than ##" :gte => ## "must be greater than or equal to ##" :eq => ## "must be equal to ##" :lt => ## "must be less than ##" :lte => ## "must be less than or equal to ##" :odd => ## "must be an odd number" :even => ## "must be an even number" :within => ##..## "must be within ## and ##" Usage: (in your case) validates_numericality_of :foo, :integer_only => true, :gt => 0 validates_numericality_of :age, :integer_only => true, :within => 13..100 If RoR team would like to integrate into Rails, have at it. I don''t do well with rejection, so I won''t offer it as a patch unless requested. Download it at: http://www.umesd.com/better_validates_numericality_of.zip I''ll get it downloadable via script/plugin in due time. Gotta write a few unit tests first. Enjoy, Bob Silva> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of Wilson Bilkovich > Sent: Monday, January 23, 2006 2:49 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Re: validates_numericality_of positive integer > > On 1/23/06, Peter Michaux <petermichaux@gmail.com> wrote: > > anyone? > > > > On 1/19/06, Peter Michaux <petermichaux@gmail.com> wrote: > > > Hi, > > > > > > What is the simplest way to validate a positive integer? > > > > > > validates_numericality_of :foo, :integer_only => true > > > > > > how do I add the positive part? Do I need another validation statement > > > for pattern matching or do I have to write a validate() funciton for > > > my model? > > > > > I''d go with this, personally: > def validate > unless foo_before_type_cast.to_s =~ /^[+]?\d+$/ > errors.add("foo", "is not a valid number") > end > end > > 500 and +500 are valid, -500, -500.00, 400.12, etc, are not. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
After speaking with a buddy of mine also doing Rails, I need to make some modifications. It shouldn''t have the :within since this can already be accomplished with validates_inclusion_of. As far as including in Rails, it depends what their definition of numericality is. With the current function name, it''s not appropriate to have these extra checks in them and they really aren''t critical, just saves some code to validate your input. But my second plugin, once I polish it off should definitely be included, but you''ll have to wait for that one :) Bob Silva http://www.railtie.net/> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of Peter Michaux > Sent: Wednesday, January 25, 2006 6:27 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Re: validates_numericality_of positive integer > > Hi Bob, > > Thanks for the plugin. I think this should be included in rails as > these are some very standard things to check. > > Peter > > > On 1/23/06, Bob Silva <me@bobsilva.com> wrote: > > I hacked together a plugin to add a few parameters to > > validates_numericality_of based on your needs and my laziness for > explicit > > checks. > > > > >From the README: > > > > Adds the following parameters: > > (plus the messages generated as an error) > > > > :gt => ## "must be greater than ##" > > :gte => ## "must be greater than or equal to ##" > > :eq => ## "must be equal to ##" > > :lt => ## "must be less than ##" > > :lte => ## "must be less than or equal to ##" > > :odd => ## "must be an odd number" > > :even => ## "must be an even number" > > :within => ##..## "must be within ## and ##" > > > > Usage: (in your case) > > > > validates_numericality_of :foo, :integer_only => true, :gt => 0 > > > > validates_numericality_of :age, :integer_only => true, :within => > 13..100 > > > > > > If RoR team would like to integrate into Rails, have at it. I don''t do > well > > with rejection, so I won''t offer it as a patch unless requested. > > > > > > Download it at: > > > > http://www.umesd.com/better_validates_numericality_of.zip > > > > I''ll get it downloadable via script/plugin in due time. Gotta write a > few > > unit tests first. > > > > Enjoy, > > > > Bob Silva > > > > > > > > > -----Original Message----- > > > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > > > bounces@lists.rubyonrails.org] On Behalf Of Wilson Bilkovich > > > Sent: Monday, January 23, 2006 2:49 PM > > > To: rails@lists.rubyonrails.org > > > Subject: Re: [Rails] Re: validates_numericality_of positive integer > > > > > > On 1/23/06, Peter Michaux <petermichaux@gmail.com> wrote: > > > > anyone? > > > > > > > > On 1/19/06, Peter Michaux <petermichaux@gmail.com> wrote: > > > > > Hi, > > > > > > > > > > What is the simplest way to validate a positive integer? > > > > > > > > > > validates_numericality_of :foo, :integer_only => true > > > > > > > > > > how do I add the positive part? Do I need another validation > statement > > > > > for pattern matching or do I have to write a validate() funciton > for > > > > > my model? > > > > > > > > > > > I''d go with this, personally: > > > def validate > > > unless foo_before_type_cast.to_s =~ /^[+]?\d+$/ > > > errors.add("foo", "is not a valid number") > > > end > > > end > > > > > > 500 and +500 are valid, -500, -500.00, 400.12, etc, are not. > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails