The bug : impossible to validate a number field with firefox
Firefox Screenshot

The elements :

  • For long time I thought firefox had trouble rendering number widgets.
  • Then my collegue, with same FFx version (FFx 59.0.2 - 64-bit on Linux) had a different result
  • I discovered a strange value in the step attribute (Number with 2 decimal, for currency values) :

    <input ... step="0.0100000000000000019" min="0" max="99999" placeholder="" class="form-number" type="number">
  • I understood this is PHP, in Drupal core :
    NumberWidget.php
        switch ($this->fieldDefinition->getType()) {
          case 'decimal':
            $element['#step'] = pow(0.1, $field_settings['scale']);
    
  • It seems that depending on your PHP configuration, pow(0.1,2) value, that is supposed to be 0.01, is actually 0.0100000000000000019
    (For instance, setting your PHP precision to 18 instead of 14)
  • Maybe a related issue #2230909: Simple decimals fail to pass validation ?

Here is a patch, that at least bypasses the problem.

Comments

pguillard created an issue. See original summary.

pguillard’s picture

Title: Impossible to validate a number field with decimals » Bad step value with decimals
pguillard’s picture

StatusFileSize
new762 bytes
pguillard’s picture

Issue summary: View changes
pguillard’s picture

Issue summary: View changes
pguillard’s picture

StatusFileSize
new785 bytes

I did a mistake

joachim’s picture

Status: Needs review » Needs work

This needs an inline comment to explain that it's a workaround.

mahtab_alam’s picture

StatusFileSize
new868 bytes
pguillard’s picture

Status: Needs work » Needs review
StatusFileSize
new934 bytes
new815 bytes

I guess this needs some more explicit explanation, what do you think of this one ?

joachim’s picture

Status: Needs review » Reviewed & tested by the community

Yup, that'll do I think!

pguillard’s picture

Issue tags: +DevDaysLisbon
xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests, +Needs manual testing

Thanks for your work on this issue! Good find and good sleuthing.

Based on the issue description and the issue summary of #2230909: Simple decimals fail to pass validation, it sounds like this might be a duplicate of that issue. Can we test this with the patch from that issue and see if it resolves the problem?

Here are a couple things we should change with this issue as it currently stands:

  1. We should add some sort of automated test for this issue. The test suite won't be able to actually test FF, but we should be able to add some manner of unit or kernel test for the widget's expected input/output in this scenario.
  2. We should also manually test the before-and-after in Drupal 8's supported browsers to confirm the issue doesn't have side effects on them.
  3. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php
    @@ -78,7 +78,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
    +        // Depending of PHP precision setting, we can have strange values there.
    +        // Use of number_format() to make sure it doesn't happen.
    

    This comment is a little odd and unspecific. ("Depending of" also isn't quite correct in English; normally it is "depending on"). I'd say something like:

    If the PHP precision setting insert_php_setting_name_here is configured with [insert specifics here], this can result in invalid step intervals, which breaks the functionality on some browsers. For example, if insert_php_configuration_setting is 18, pow(0.1,2) might return 0.0100000000000000019 instead of 0.01. Use number_format() to prevent this issue.

If the issue turns out to be a duplicate of the other, this issue should be marked "Closed (duplicate)" and we can consolidate our efforts over there.

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.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.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.

darvanen’s picture

Status: Needs work » Closed (cannot reproduce)
Issue tags: +Bug Smash Initiative
StatusFileSize
new22.77 KB

This issue was discussed in the #bugsmash slack channel, in particular its probability as a duplicate of #2230909: Simple decimals fail to pass validation.

However, upon investigating I was unable to reproduce this problem. On Drupal 9.4.1, with or without the patch from 2230909 applied (and with all caches cleared each time) the step value printed on the input remained the same and correct at 0.01.

Considering the length of time this has been dormant - 4 years with no activity - I'm going to close this as "cannot reproduce".

If this is wrong please feel free to reopen with steps to reproduce.