Hi,
I've got a similar problem - I want to allow the customer to enter $ and
, in number fields.
validates_numericality_of fails if you start with a £ - fair enough.
validates_format_of doesn't seem to work - I think the value has been
converted to a number for this is checked and therefore it is too late.
before_validation seems the obvious answer.
Using the below code...
def before_validation
puts "#{self.price}"
self.price = self.price.to_s.gsub(/[^\d\.]/, "").to_i
puts "#{self.price}"
end
If I enter 999, then it correctly outputs 999 twice.
If I enter £999, then it outputs 0 twice - what!?
So I presume it has converted to a number before, my before_validation.
Anyone got aany suggestions?
I am really stunned that rails has such difficulty with something that
seems such a common problem - flexible user input.
Thanks,
James
Pat Maddox wrote:
> On 10/14/07, Mix Mix <[email protected]> wrote:
>> > -- gw
>> think that is because the validations are done before.. so i've put it
>> >
>>
>
> You can put your substitution code in before_validation instead of
> before_save. I'm sure you can guess when it will be executed :)
>
> Pat
--
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---