Re: [ORUG] Form submission type conversion

0 views
Skip to first unread message

Scott Moe

unread,
May 7, 2009, 10:48:07 PM5/7/09
to orug-di...@googlegroups.com
I had a similar problem with money fields.  I kept the the validates_numericality_of
so a type cast "0" will not validate.

The garbage entered by the user is still available with object.attributes_before_type_cast[field_name]. In a rather overdone view helper method I build an input tag with the value set to what the user typed, if object.errors.on field_name is true.  I'm willing to show you the code but the error handling is a small part of that mess. Those ActiveRecord methods may be all you need.

Scott Moe


Chris Booth wrote:
I'm probably making this more complicated than it needs to be, but I'm
stumped.

I have a form with a regular text box for the user to enter a dollar
amount in.  The model field for that box is a decimal.  The model has
a validates_numericality_of for that field.  If the user types
'$1,000.00' into the field and submits, the value gets put in as 0.

I've tried stripping the $ and , in a before_validation method using
self.<field>.to_s.gsub!('$',''), but this doesn't work, because
self.<field> is already set to 0 at this point.

Where would I strip the $ and , out of the value so that it will pass
the validates_numericality_of?  Or should I even be using that
validation?

--Chris

  
Reply all
Reply to author
Forward
0 new messages