So, it's not a real number, just numerical text with commas?
If so, use something like this instead (regex might need tweaking):
validates_format_of :name, :with => /[0-9\,]*/
-- gw
So, it's not a real number, just numerical text with commas?
If so, use something like this instead (regex might need tweaking):
validates_format_of :name, :with => /[0-9\,]*/
-- gw
Mix Mix --
No answer, but a possible clue...
I am having a similar problem; validates_numericality_of seems pretty limited. But I think there are two issues you face: * localization of acceptable number formats (1,234.56 is right in US, not in Italy; 1.234,56 is right in Italy, not in US), and * implicit recognition of numeric formatting, or coersion of display formats between (to and from) integers, floats, decimals, and hey, how about currency, while we're at it!
I think replacing validates_numericality_of with validates_format_of :name :with => /[0-9]\,\./ would accept any format, but it's not really complete (if would accept 123.4556,789.10,1 and other funky variants). You could write one or more nasty regex's to handle any particular local format, but that would only get you so far: before adding to the database, it needs to be coerced into a number.
I have been attempting to figure out how to use the model_formatter
plugin in order to allow users flexibility in entering numeric
(currency) data, but I haven't had luck getting it to do what I want.
I want my :decimal model field to be presented (in forms, displays,
etc.) as currency, and automagically converted to proper numeric
format for the database. I am still getting my head around a lot of
this; perhaps you'll find the model_formatter plugin useful. And if
you figure out how to get it to do what I want, please let me know
Tom
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
When I've dealt with this in other languages, I've typically had the luxury of user preferance settings so that the system knew it was dealing with one numeric style or the other for both input and putput formatting.
If there's no way to determine a preset mode, the style can usually be derived with these rules: - if there's one period and no commas -- US - if there's one comma and no periods -- Euro - if there's one comma and one period, select the right most of the two, if it is a period -- US, else Euro - if there's > 1 commas and 1 period -- US - if there's > 1 periods and 1 comma -- Euro
As far as the user input filter goes, I allow 0-9, periods, commas, and currently symbols. That way there's none of this lame "use numbers and periods only" instruction nonsense. Then I use strip out the characters I actually want. So for US mode that would 0-9 and periods. For Italy that would be 0-9 and commas. This preserves precision. I strip out the thousands separators so the data can be cast to decimal and stored, etc. Then I format it on output.
Whether Rails has tools built-in for that I don't know, but it's not too bad to build yourself, and once you've done it, you always have it.
-- gw