Forum: Ruby on Rails validates_format_of ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-05 03:46
Hi all,

I've got a form which accepts a "price" field and I'm attempting to
validate it and allow some flexibility in the way the user enters it.
It's a numeric field but I want the user to be able to enter dollar
signs and commas naturally in the data entry.

I'm trying to understand how to use validates_format_of, and I'm not
sure what I'm missing.  I'm a beginner at regular expressions but I've
been researching it.

I tried an extremely simple case:

validates_format_of :price, :with => /^\d$/

If I understand correctly, this validation should only succeed for a
single digit character and nothing else?  I added this to my model and
so far it passes everything--numbers, characters, whitespace, etc.

What am I doing wrong?  I figure I'll get this simplest case working and
move on from there.

Thanks.
Jeff Coleman
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-05 17:15
Jeff Coleman wrote:
>
> Hi all,
>
> I've got a form which accepts a "price" field and I'm attempting to
> validate it and allow some flexibility in the way the user enters it.
> It's a numeric field but I want the user to be able to enter dollar
> signs and commas naturally in the data entry.
>
> I'm trying to understand how to use validates_format_of, and I'm not
> sure what I'm missing.  I'm a beginner at regular expressions but I've
> been researching it.
>
> I tried an extremely simple case:
>
> validates_format_of :price, :with => /^\d$/
>
> If I understand correctly, this validation should only succeed for a
> single digit character and nothing else?  I added this to my model and
> so far it passes everything--numbers, characters, whitespace, etc.
>
> What am I doing wrong?  I figure I'll get this simplest case working and
> move on from there.

Just wanted to see if I'm doing something wrong here or not.  I'd really
like to get validates_format_of to validate a string value before I need
to turn it into an integer.  Is that possible?  I was able to get
predictable results when applying validates_format_of to a model
attribute which is a string, but it doesn't seem to work when applying
to an integer value.

What's the best way to allow the user to enter something like "$500,000"
and turn it into "500000"?

Thanks,
Jeff Coleman
17fa7869b96bf7243280fe0c158b78d3?d=identicon&s=25 Michael Lascarides (mlas)
on 2006-04-05 18:00
Jeff Coleman wrote:

> What's the best way to allow the user to enter something like "$500,000"
> and turn it into "500000"?

If you want to allow oddball data entry but clean it up as best as
possible before validation, declare a "before_validation" method on the
model, which rails will automatically call:

def before_validation
  money_amount = money_amount.gsub!(/[^\d\.]/, "").to_i
end

this method will strip money_amount of everything except numerals and
decimal points, then convert it to an integer. this is more polite to
your user than just validates_format_of, since it tries to fix their
input rather than just complain about it. you should still run
validations on the results, of course.

- michael
17fa7869b96bf7243280fe0c158b78d3?d=identicon&s=25 Michael Lascarides (mlas)
on 2006-04-05 18:01
whoops, should be:

def before_validation
  money_amount = money_amount.gsub(/[^\d\.]/, "").to_i
end
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-05 19:13
michael lascarides wrote:
>
> whoops, should be:
>
> def before_validation
>   money_amount = money_amount.gsub(/[^\d\.]/, "").to_i
> end

Thanks, Michael!

Jeff
This topic is locked and can not be replied to.