
Untitled
By: a guest on
May 22nd, 2012 | syntax:
None | size: 0.47 KB | hits: 17 | expires: Never
Rails : Validates_format_of for float not working
validates :price, :format => { :with => /^[0-9]{1,5}((.[0-9]{1,5})?)$/, :message => "should be float" }
attr_accessor :price_string
before_save :convert_price_string
protected
def convert_price_string
if price_string
begin
self.price = Kernel.Float(price_string)
rescue ArgumentError, TypeError
errors.add(ActiveRecord::Errors.default_error_messages[:not_a_number])
end
end