Michael Schuerig
2005-Aug-24 01:20 UTC
validates_numericality_of: minimum, maximum, ranges
Surprisingly, validates_numericality_of has no option to check the value range of attributes. Has anyone by chance written such a thing? Michael -- Michael Schuerig The more it stays the same, mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org The less it changes! http://www.schuerig.de/michael/ --Spinal Tap, The Majesty of Rock
Michael Schuerig wrote:> Surprisingly, validates_numericality_of has no option to check the value > range of attributes. Has anyone by chance written such a thing? > > Michael >Haven''t tried this but from the docs[1] couldn''t you use the if parameter? "* if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value." [1] http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000649
Michael Schuerig
2005-Aug-24 09:20 UTC
Re: validates_numericality_of: minimum, maximum, ranges
On Wednesday 24 August 2005 10:43, Chris Roos wrote:> Michael Schuerig wrote: > > Surprisingly, validates_numericality_of has no option to check the > > value range of attributes. Has anyone by chance written such a > > thing? > > > > Michael > > Haven''t tried this but from the docs[1] couldn''t you use the if > parameter?I think I don''t understand what you mean. How does :if help? I want validation to run always for the attribute. Michael -- Michael Schuerig Nothing is as brilliantly adaptive mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org as selective stupidity. http://www.schuerig.de/michael/ --A.O. Rorty, The Deceptive Self
Chris Roos
2005-Aug-24 09:41 UTC
Re: Re: validates_numericality_of: minimum, maximum, ranges
Michael Schuerig wrote:> On Wednesday 24 August 2005 10:43, Chris Roos wrote: > >>Michael Schuerig wrote: >> >>>Surprisingly, validates_numericality_of has no option to check the >>>value range of attributes. Has anyone by chance written such a >>>thing? >>> >>>Michael >> >>Haven''t tried this but from the docs[1] couldn''t you use the if >>parameter? > > > I think I don''t understand what you mean. How does :if help? I want > validation to run always for the attribute. > > Michael >My mistake, sorry. On quick inspection, I was thinking that :if allowed for an additional constraint to be checked (I was thinking something like num > min && num < max), where it actually checks to see if the validation should be checked at all. Oops. Chris
Michael Schuerig wrote:> Surprisingly, validates_numericality_of has no option to check the value > range of attributes. Has anyone by chance written such a thing? > > Michael >Just had another thought as this is something I''ve done in the past. Create your own protected validate method in your model and add your custom error in there. In my case I wanted to ensure that a job_number was greater than zero. protected def validate errors.add(:job_number, "must be greater than 0") if job_number.nil? || job_number <= 0 end I can see that this isn''t great if you are wanting this min/max clause to be applicable on lots of models but it was fine in my case. Chris
Patrick McCafferty
2005-Aug-24 19:51 UTC
Re: validates_numericality_of: minimum, maximum, ranges
Try: validates_numericality_of :foo validates_inclusion_of :foo, :in => 1..100 I wouldn''t like the logic for the second one in the first, personally. On Aug 24, 2005, at 10:20 AM, Michael Schuerig wrote:> > Surprisingly, validates_numericality_of has no option to check the > value > range of attributes. Has anyone by chance written such a thing? > > Michael > > -- > Michael Schuerig The more it stays the same, > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org The less it changes! > http://www.schuerig.de/michael/ --Spinal Tap, The Majesty of Rock > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Michael Schuerig
2005-Aug-24 20:45 UTC
Re: validates_numericality_of: minimum, maximum, ranges
On Wednesday 24 August 2005 21:51, Patrick McCafferty wrote:> Try: > > validates_numericality_of :foo > validates_inclusion_of :foo, :in => 1..100 > > I wouldn''t like the logic for the second one in the first, > personally.That only works for integers. Michael -- Michael Schuerig Airtight arguments have mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org vacuous conclusions. http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions
Cuong Tran
2005-Aug-24 22:56 UTC
Re: Re: validates_numericality_of: minimum, maximum, ranges
it would be fairly trivial to add support for min/max parameters. On 8/24/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> On Wednesday 24 August 2005 21:51, Patrick McCafferty wrote: > > Try: > > > > validates_numericality_of :foo > > validates_inclusion_of :foo, :in => 1..100 > > > > I wouldn''t like the logic for the second one in the first, > > personally. > > That only works for integers. > > Michael > > -- > Michael Schuerig Airtight arguments have > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org vacuous conclusions. > http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Instead of validates_numericality_of, how about validates_as_number?
> Instead of validates_numericality_of, how about validates_as_number?See http://dev.rubyonrails.com/ticket/2031 -- and think long and hard about whether it feels worth repeating the high emotion here ;) -- David Heinemeier Hansson http://www.loudthinking.com -- Broadcasting Brain http://www.basecamphq.com -- Online project management http://www.backpackit.com -- Personal information manager http://www.rubyonrails.com -- Web-application framework
Julian ''Julik'' Tarkhanov
2005-Aug-25 20:12 UTC
Re: Re: validates_numericality_of: minimum, maximum, ranges
On 24-aug-2005, at 22:45, Michael Schuerig wrote:> On Wednesday 24 August 2005 21:51, Patrick McCafferty wrote: > >> Try: >> >> validates_numericality_of :foo >> validates_inclusion_of :foo, :in => 1..100 >> >> I wouldn''t like the logic for the second one in the first, >> personally. >> > > That only works for integers.In both cases (if it doesn''t validate floats AND if it doesn''t typecast strings) it deserves a ticket I believe (and a one-liner fix) -- Julian "Julik" Tarkhanov
Wow... that thread was hilarious, in an octopodian kind of way On Aug 25, 2005, at 7:30 AM, David Heinemeier Hansson wrote:>> Instead of validates_numericality_of, how about validates_as_number? >> > > See http://dev.rubyonrails.com/ticket/2031 -- and think long and hard > about whether it feels worth repeating the high emotion here ;) > -- > David Heinemeier Hansson > http://www.loudthinking.com -- Broadcasting Brain > http://www.basecamphq.com -- Online project management > http://www.backpackit.com -- Personal information manager > http://www.rubyonrails.com -- Web-application framework > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >Matt Pelletier pelletierm-A1PILTyJ15gXhy9q4Lf3Ug@public.gmane.org
Okay :-) how about "validates_cardinality_of" Just as an alternative. I quite like numericality_of... and I''m not big on changing frameworks once they''re in use. However adding additional ways to do things always seems a plus to me. Respect! Julian. On 25/08/2005, at 9:30 PM, David Heinemeier Hansson wrote:>> Instead of validates_numericality_of, how about validates_as_number? >> > > See http://dev.rubyonrails.com/ticket/2031 -- and think long and hard > about whether it feels worth repeating the high emotion here ;) > -- > David Heinemeier Hansson_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> how about "validates_cardinality_of"No, because that sounds like "check how many there are of these", not "check that this is a number".> However adding additional > ways to do things always seems a plus to me.That''s exactly the sort of thinking that''ll get you languages like Perl. *shudder* - M
You''re right! Sorry about that. Julian. On 28/08/2005, at 8:16 PM, Matt Powell wrote:>> how about "validates_cardinality_of" >> > > No, because that sounds like "check how many there are of these", > not "check that this is a number". > > >> However adding additional ways to do things always seems a plus to >> me. >> > > That''s exactly the sort of thinking that''ll get you languages like > Perl. > > *shudder* > > - M > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >