Form submission type conversion

0 views
Skip to first unread message

Chris Booth

unread,
May 7, 2009, 3:13:25 PM5/7/09
to Orlando Ruby Users Group Discussion
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

Tim Rosenblatt

unread,
May 7, 2009, 4:03:41 PM5/7/09
to orug-di...@googlegroups.com
Worst case, you could write a custom validation for that.
--
Tim Rosenblatt
http://www.timrosenblatt.com

Justin Blake

unread,
May 7, 2009, 4:55:43 PM5/7/09
to Orlando Ruby Users Group Discussion
I'd put a custom setter on the model and strip it there.

-Justin

Greg Pederson

unread,
May 9, 2009, 12:48:21 PM5/9/09
to orug-di...@googlegroups.com
In my model I did something like:

before_validation :clean_phone_number

def clean_phone_number
    self.phone.gsub!(/\D/,'')
  end

to strip out the non-numeric stuff and then run the regular rails validation stuff.

Greg

On Thu, May 7, 2009 at 3:13 PM, Chris Booth <regi...@boothclan.com> wrote:



--
Greg Pederson
Founder and Technical Director

Nsight Development, LLC.
www.NsightDevelopment.com

Gr...@NsightDevelopment.com
407-641-0327

Quis custodiet ipsos custodes

Greg.P...@gmail.com
Reply all
Reply to author
Forward
0 new messages