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,
The code is below, welcome to give me any advice on my blog. And also, more details information on the blog,
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
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,