Re: Custom validations in rails: validates_number_range

Ye Dingding
Wed, 27 Jun 2007 09:35:29 -0000

Today i looked through the validations.rb in rails edge. I''m glad to
find
that validates_numericality_of method already has the support for range
check.

The related options are

   - greater_than
   - greater_than_or_equal_to
   - equal_to
   - less_than
   - less_than_or_equal_to
   - odd
   - even

With these, life are more better, :)


On 6/25/07, Ye Dingding
<yedingding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
>
> No, that''s not i want to do. Sorry, my blog(
> http://sishen.lifegoo.com/?p=42) gives more information, so i gave less
> information in the mail.
>
> My scenario is that I want to check the uploaded file size, for example,
> less than 5M. But what validates_length_of is the size, when is a fixnum,
is
> 4. So validates_length_of can''t be work for me.
>
> Using validates_number_range, now i can code as below:
>
> validates_number_range :file_size,
>                          :maximum => 5242880,
>                          :message => "you can only upload file less
than
> 5M",
>                          :on => :create
>
>
> On 6/25/07, Matt Sir
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > wrote:
> >
> >
> > If I understood what you are trying to do correctly
> > validates_length_of :user_name, :within => 6..20, :too_long =>
"pick a
> > shorter name", :too_short => "pick a longer name"
> >
> > from
> >
http://railsmanual.com/module/ActiveRecord::Validations::ClassMethods/validates_length_of/1.1.2
> >
> >
> > Matt
> >
> > Ye Dingding wrote:
> > > http://sishen.lifegoo.com/?p=42
> > >
> > > I don''t know whether there exists a builtin validation
that can check
> > > the
> > > number range. I think the requirement is normal, but indeed, i
can''t
> > > find
> > > it.  So i wrote a custome validation: validates_number_range,
whose
> > > prototype is validates_length_of. If there already exists one,
pls
> > tell
> > > me,
> > > :)
> > >
> >
> > --
> > Posted via http://www.ruby-forum.com/.
> >
> > > >
> >