Number element currently relies on PHP's is_numeric to validate the input. But localized number representations aren't supported by is_numeric, leading to errors from users that use a comma as decimal separator (there probably are other locale issues). Should the number form element provide support for those cases?
Original submission:
Hi,
I found the following problem: I have a German project where people need to fill in a number input field with float values. In Germany commas are used for floats, not points. In Google Chrome it is ok - it seems that the browser translates comma floats into point float. But in Firefox the modules validation function function elements_validate_number(&$element, &$form_state)
fires a form error ("FIELD NAME must be a number.") as the comma float like "24,5" is not seen as a number - in Chrome the sam input does not fire the module's validation form error.
Can sombody tell me how I can fix this problem?
Best,
Tobias
Comment | File | Size | Author |
---|---|---|---|
#3 | elements-validate_comma_float_notation-1962156-3.patch | 860 bytes | barraponto |
Comments
Comment #1
tobiberlinOk... my workaround is the following: as I use the form API for my own form I just set a custom element validation function with
In this validation function I try something like this
to get a point instead of a comma before validation - even in Firefox which sends the original comma float. But it seems not really satisfying to me.
Comment #2
dman CreditAttribution: dman commentedIt looks like you've found the right way to apply this fix.
If a user enters input that would not be properly interpreted by the system, you clean up the input quietly, correctly, before it causes any problems.
What is not satisfying about that?
Comment #3
barraponto CreditAttribution: barraponto commentedThis could be handled from the module itself. I don't think i18n should weight in, because we might have a German user filling an English American form. We should make their experience as good as possible out of the box.
Comment #4
barraponto CreditAttribution: barraponto commentedComment #5
Dave ReidThis is up to the browser to clean up the HTML5 input values to the spec before it gets submitted, unfortunately. I'm not sure this is appropriate here. What if I entered an american value like '2,500.25'? This would get converted to '2.500.25' which is invalid.
Comment #6
tobiberlinThere is a module Number Format (http://drupal.org/project/format_number) - maybe this could be used in some way?
This is what I found not so satisfying - my solution meets the needs for my project which is German and where I can be sure that the users enter a comma for decimal points. In general my solution could still lead to confusions. The main problem as I found out as well is that the browsers handles this issue in very different ways. Maybe it would be better to supply a JavaScript with this module which ensures an expected number format?
Comment #7
Dave ReidI would prefer the HTML5 elements stay in sync with exactly what we have in core. There is nothing stopping anyone from altering the validation/processing of the numberelement field provided by this module. I think we either need to alter what we have here from other modules, or propose fixes for core, which would then be backported here.
Comment #8
barraponto CreditAttribution: barraponto commentedOk, moving to core and updating the issue summary.
Comment #9
Dave ReidThen this is a duplicate of #1964192: form_validate_number() excludes countries that use commas. Let's move this back to the Elements module for now so that people in the issue queue can still find this issue.
Comment #9.0
Dave ReidUpdated issue summary.