form_validate_number uses PHP's is_numeric(), which checks if a value is numeric based on its own float notation: digits with a period as a decimal separator. We exclude about half the world's countries, where commas are the standard.

CommentFileSizeAuthor
#4 drupal_1964192_00.patch420 bytesxano

Comments

Niklas Fiekas’s picture

The idea was, that this is according to the HTML5 specification: http://www.w3.org/TR/html-markup/datatypes.html#common.data.float.

Browsers might very well allow input with a comma and then internally convert it. So this should only be a problem for browsers that don't do this. I just verified that Chrome does do it.

So the question is: Do we want to be and should we be more graceful with older browsers?

xano’s picture

Thanks for the explanation. In your opinion, would it make sense to document this in the code, or should it be ovbious that the validate function is for HTML5 numbers only?

Niklas Fiekas’s picture

Some points:

/**
* Form element validation handler for #type 'number'.
*
* Note that #required is validated by _form_validate() already.
*/
function form_validate_number(&$element, &$form_state) {
  // [...]
}

Looking at it, the docs explicitly say that it is meant to be used by #type 'number' elements. That is also reflected by the naming form_validate_[...] which is consistently used in such cases.

xano’s picture

Title: form_validate_number excludes countries that use commas » form_validate_number() excludes countries that use commas
Assigned: Unassigned » xano
Priority: Normal » Minor
Status: Active » Needs review
StatusFileSize
new420 bytes

True, but this is a great example of something that can be used in other contexts as well. We just described the function in terms of what we originally wanted to use it form rather than in terms of what is actually does, which is what we're supposed to do.

Here's a patch that fixes this.

dave reid’s picture

I disagree with this change. The documentation should state that this validation is for #number type elements. If you want to re-use this validation, we can add a note not in the documentation summary about the format expected, but let's not change the summary. This is consistent with our other form_validate_[elementname] callbacks and we shouldn't deviate for this one case.

xano’s picture

I believe code should be documented in terms of what it does, rather than in terms if what the author intended it to be used for, which means that the current documentation is wrong. However, I don't care enough to keep trying to convince people of this improvement.

dave reid’s picture

Status: Needs review » Needs work

I think the non-summary line can be improved with the expected format. But the summary line is exactly what this does. This is a validation callback for 'number' form input elements.

To be clear, I think we can improve the docs for what *format* of number is expected (linking to the HTML5 specs), but just not on the first line.

xano’s picture

Assigned: xano » Unassigned

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

norman.lol’s picture

Issue summary: View changes

This seems to be more of a browser issue than Drupal-related. Nonetheless here comes a quickfix for when you want to allow people to enter numbers with a comma as decimal separator on Firefox.

With a little bit of JS you can convert the value using dots instead of commas. People on Chrome or Safari won't even recognise it, the comma stays in the browser, Chrome and Safari automatically convert it under the hood already. But Firefox doesn't. The following snippet fixes this.

(function ($, Drupal) {
  Drupal.behaviors.firefoxNumberIssue = {

    attach: function (context, settings) {
      $('input[type=number]', context).once('firefoxNumberIssue').each(function () {
        $(this).change(function () {
          var $replace = $(this).val().toString().replace(/,/g, '.');
          $(this).val($replace);
        })
      });
    }
  };

})(jQuery, Drupal);

(Cross-posted generalized snippet with a little bit more background info on https://stackoverflow.com/a/60935441/2199525.)

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Needs work » Closed (works as designed)
Issue tags: +Bug Smash Initiative

The first comment and last comment state this is about browsers, not Drupal. In the middle there is discussion of adding a comment but there was no agreement. Looking at the code myself I don't see the need to add a comment or further explanation. Based on that and the lack of interest in this issue in 11 years, I am going to close this.
If that is wrong, reopen the issue, by setting the status to 'Active', and add a comment.

Thanks!