Re: Custom validations in rails: validates_number_range

Ye Dingding
Sun, 24 Jun 2007 20:13:24 -0000

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/.
>
> >