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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tobiberlin’s picture

Ok... my workaround is the following: as I use the form API for my own form I just set a custom element validation function with

 '#element_validate' => array('_numberfield_validation')

In this validation function I try something like this

$fieldvalue = str_replace(',', '.', $form_state['values']['fieldname']);

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.

dman’s picture

It 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?

barraponto’s picture

This 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.

barraponto’s picture

Status: Active » Needs review
Dave Reid’s picture

Status: Needs review » Postponed (maintainer needs more info)

This 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.

tobiberlin’s picture

There 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?

Dave Reid’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

I 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.

barraponto’s picture

Project: Elements » Drupal core
Version: 7.x-1.3 » 8.x-dev
Component: Code » forms system
Status: Closed (won't fix) » Active

Ok, moving to core and updating the issue summary.

Dave Reid’s picture

Project: Drupal core » Elements
Version: 8.x-dev » 7.x-1.3
Component: forms system » Code
Status: Active » Closed (won't fix)

Then 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.

Dave Reid’s picture

Issue summary: View changes

Updated issue summary.