I would like to validate a number within a certain range. To first test that a entry is a valid number i use validates_numericality_of :my_number, :message =>''sorry, not a number'' to test if the number is within a certain range i use validates_inclusion of :my_number, :in => min..max, message => ''outside range!'' If the numericality test fails, I don''t want to do the second range test, otherwise both error messages will be displayed. At the moment, both tests with be performed regardless. Any ideas on how to do this? Thanks. -- Posted via http://www.ruby-forum.com/.
What would happen if you just take off the validates_numericality_of, and left only validates_inclusion? I know about two ways to solve your problem: a) you can define a validate method in your method, do the validations by yourself, and if you found any error, you can add them to the array errors, example: def validate if (price == nil) errors.add "There is not price!!!" else if (price < 0) errors.add "The price is negative!!!" end end b) You can check what errors to display, in the view, by inspecting modelobject.errors Rodrigo Dominguez ? Iplan Networks ???????????????Datos Personales rdominguez@iplan.com.ar ??????rorra@rorra.com.ar www.iplan.com.ar ?????????????www.rorra.com.ar 5031-6303 ????????????????????15-5695-6027 -----Mensaje original----- De: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] En nombre de montyman Enviado el: Mi?rcoles, 21 de Junio de 2006 10:14 a.m. Para: rails@lists.rubyonrails.org Asunto: [Rails] validation help I would like to validate a number within a certain range. To first test that a entry is a valid number i use validates_numericality_of :my_number, :message =>''sorry, not a number'' to test if the number is within a certain range i use validates_inclusion of :my_number, :in => min..max, message => ''outside range!'' If the numericality test fails, I don''t want to do the second range test, otherwise both error messages will be displayed. At the moment, both tests with be performed regardless. Any ideas on how to do this? Thanks. -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hey Monty, if you goal is to validate that a field is within a certain range, then I would recommend using ''validates_inclusion''. -Conrad On 6/21/06, Rodrigo Dominguez <rails@rorra.com.ar> wrote:> > What would happen if you just take off the validates_numericality_of, > and left only validates_inclusion? > > I know about two ways to solve your problem: > > a) you can define a validate method in your method, do the validations > by yourself, and if you found any error, you can add them to the array > errors, example: > > def validate > if (price == nil) > errors.add "There is not price!!!" > else if (price < 0) > errors.add "The price is negative!!!" > end > end > > b) You can check what errors to display, in the view, by inspecting > modelobject.errors > > Rodrigo Dominguez > > Iplan Networks Datos Personales > rdominguez@iplan.com.ar rorra@rorra.com.ar > www.iplan.com.ar www.rorra.com.ar > 5031-6303 15-5695-6027 > > > -----Mensaje original----- > De: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] En nombre de montyman > Enviado el: Mi?rcoles, 21 de Junio de 2006 10:14 a.m. > Para: rails@lists.rubyonrails.org > Asunto: [Rails] validation help > > I would like to validate a number within a certain range. > To first test that a entry is a valid number i use > validates_numericality_of :my_number, :message =>''sorry, not a number'' > > to test if the number is within a certain range i use > validates_inclusion of :my_number, :in => min..max, message => ''outside > range!'' > > If the numericality test fails, I don''t want to do the second range > test, otherwise both error messages will be displayed. At the moment, > both tests with be performed regardless. Any ideas on how to do this? > > Thanks. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > 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 >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060621/11e932b6/attachment.html
thank you conrad for posting that tip! Conrad Taylor wrote:> Hey Monty, if you goal is to validate that a field is within a certain > range, then I would recommend using ''validates_inclusion''. > > -Conrad-- 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 -~----------~----~----~----~------~----~------~--~---