Problem/Motivation

Max value can be less than min value for NumericItems.

(Ps. I find it a bit surprising how such an obvious bug could slip through, so I searched the Core number.module's issue queue before opening this thread.)

Steps to reproduce

Tested scenarios:

  1. Field type: Number (decimal)
    On these paths of entities:
    • Node: /admin/structure/types/manage/page/fields/node.page.field_number_decimal/storage
    • Taxonomy: /admin/structure/taxonomy/manage/forums/overview/fields/taxonomy_term.forums.field_number_decimal
    • User: /admin/config/people/accounts/fields/user.user.field_number_decimal
  2. Field type: Number (float)
    On these paths of entities:
    • Node: /admin/structure/types/manage/page/fields/node.page.field_number_float
    • Taxonomy: /admin/structure/taxonomy/manage/forums/overview/fields/taxonomy_term.forums.field_number_float
    • User: /admin/config/people/accounts/fields/user.user.field_number_float
  3. Field type: Number (integer)
    On these paths of entities:
    • Node: /admin/structure/types/manage/page/fields/node.page.field_number_integer
    • Taxonomy: /admin/structure/taxonomy/manage/forums/overview/fields/taxonomy_term.forums.field_number_integer
    • User: /admin/config/people/accounts/fields/user.user.field_number_integer

Test settings:

  • Min = 4
  • Max = -4
  • Default = 2

Test results:

  • Expected: form validation should deny submitted values
  • Result: settings saved into DB.

Proposed resolution

Add tests of min and max value.

Remaining tasks

Start with the patches from the duplicate issue;

Add tests
patch
review
commit

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3052663

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Balu Ertl created an issue. See original summary.

baluertl’s picture

Status: Needs review » Active

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.

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now 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.

quietone’s picture

Status: Active » Closed (duplicate)
Issue tags: +Bug Smash Initiative

Looking more closely at this, this is a duplicate of an earlier issue, #2838121: Numeric item min/max settings ignored if set to 0. The issue summary is well written and a search for duplicates was done, I'll move credit to the other issue.

quietone’s picture

Assigned: baluertl » Unassigned
Issue summary: View changes
Status: Closed (duplicate) » Active

I made a mistake. This is testing when the max is less than the min, so it is not a duplicate.

Also, un-assigning so everyone can work on this.

I closed #3327767: integer with minimum but no maximum causing ValueError: mt_rand(): Argument #2 ($max) must be greater than or equal to argument #1 ($min) as a duplicate, adding credit.

quietone’s picture

Title: Foolproofing Number field config: validate min, max, default values » Validate the min, max and default values for Numeric fields
Component: number.module » field system
joaopauloc.dev’s picture

Assigned: Unassigned » joaopauloc.dev
joaopauloc.dev’s picture

Assigned: joaopauloc.dev » Unassigned
Status: Active » Needs review
StatusFileSize
new5.43 KB

Hello everyone, the patches above are attached and tested, and everything works fine.
Also, I added validation on the form field submission to validate min and max values.

joaopauloc.dev’s picture

StatusFileSize
new5.84 KB
joaopauloc.dev’s picture

StatusFileSize
new7.42 KB

Fixing php cs issues.

joaopauloc.dev’s picture

StatusFileSize
new7.42 KB

Fixing php cs issues.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

public static function validateMinMax
1. can this be typehinted please

       '#description' => $this->t('The maximum value that should be allowed in this field. Leave blank for no maximum.'),
+
     ];

2. seems unintended

Did not test yet.

joaopauloc.dev’s picture

Status: Needs work » Needs review
StatusFileSize
new4.92 KB
new7.06 KB

Fixing type hint.

joaopauloc.dev’s picture

StatusFileSize
new770 bytes
new7.06 KB
kunalgautam’s picture

StatusFileSize
new114.08 KB

Patch #23 is applied and working fine for Drupal version 10.1.x. It can move for RTBC.

Patch

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @kkalashnikov but a screenshot of the patch does not provide value, we know that by the CI build. See https://www.drupal.org/about/core/policies/maintainers/how-is-credit-gra.... Removing credit for that.

Reviewing #23

Confirmed the fields mentioned in the IS does save invalid option (min: 4; max: -4)

Applied patch
Tested all the scenarios in the IS (integer, float, decimal) and I get a warning when trying to save those values.

Good work!

quietone’s picture

Status: Reviewed & tested by the community » Needs work

I took a look at the patch.

+++ b/core/modules/field/tests/src/Functional/Number/NumberFieldTest.php
@@ -197,7 +197,7 @@ public function testNumberIntegerField() {
+    // Submit a valid integer.

@@ -207,7 +207,7 @@ public function testNumberIntegerField() {
+    // Try to set a value below the minimum value.

@@ -215,7 +215,7 @@ public function testNumberIntegerField() {
+    // Try to set a decimal value.

@@ -223,7 +223,7 @@ public function testNumberIntegerField() {
+    // Try to set a value above the maximum value.

These changes are out of scope

  1. +++ b/core/modules/field/tests/src/Functional/Number/NumberFieldTest.php
    @@ -428,6 +428,64 @@ public function testCreateNumberDecimalField() {
    +  public function testCreateDecimalMaxLessThanMinValidation() {
    

    These and the following should be incorporated into an existing test. There is no need to add additional setup costs for a functional test here.

  2. +++ b/core/modules/field/tests/src/Functional/Number/NumberFieldTest.php
    @@ -428,6 +428,64 @@ public function testCreateNumberDecimalField() {
    +    $this->helperValidateFieldConfig('integer', $edit);
    ...
    +    $this->helperValidateFieldConfig('integer', $edit);
    

    Can we use the same field and not re-create one?

  3. +++ b/core/modules/field/tests/src/Functional/Number/NumberFieldTest.php
    @@ -451,4 +509,31 @@ public function assertSetMinimumValue(FieldConfigInterface $field, $minimum_valu
    +   * Helper function to test form config submit.
    

    This should tell me what it does. See Coding standards for summary lines for more details.

  4. +++ b/core/modules/field/tests/src/Functional/Number/NumberFieldTest.php
    @@ -451,4 +509,31 @@ public function assertSetMinimumValue(FieldConfigInterface $field, $minimum_valu
    +  public function helperValidateFieldConfig($type, $edit) {
    

    The title should reflect what the function is doing. It isn't doing validation.

joaopauloc.dev’s picture

Assigned: Unassigned » joaopauloc.dev
joaopauloc.dev’s picture

Assigned: joaopauloc.dev » Unassigned
Status: Needs work » Needs review
deepalij’s picture

StatusFileSize
new190.9 KB

Able to reproduce the issue using IS
Verified and tested MR #27 on Drupal 10.1.x-dev
The patch applied cleanly
The issue gets resolved after applying the patch.
Refer to the attached after patch screenshot

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Moving back to RTBC. The changes in #26 appear to be addressed
26.1 = was removed
26.2 = helper function was removed and test simplified
26.3 = the test is existing so not sure we should update here
26.4 = help function was removed

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Usability

This is changing the UI, adding tag.

Version: 10.1.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, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joaopauloc.dev’s picture

Status: Needs work » Needs review

Hello @quietone, I'm not sure if I understood or fixed the issues about moving the unit test to a kernel class.
Also, I didn't find any other way to test validateMinAndMaxConfig so I tested the method statically.
Maybe, we can also add the test back that I implemented in the other mr
Waiting for some feedback to update the code if necessary.

smustgrave’s picture

Status: Needs review » Needs work

Left a small comment on MR 4709 about adding an additional test set.

The kernel test looks good but don't see any reason to not include the additional asserts from MR 3436. If it were it's own function/bootstrap then maybe but that's expanding an existing tests. @quietone thoughts?

joaopauloc.dev’s picture

Status: Needs work » Needs review

I added the test min 1 max 0.
Also, the Functional test with a tiny improvement where I added an assert to check the numbers as string values for the Integer type field. Now, as we have the kernel test I don't think we need to test for every number type. Just for integer is enough, but if you guys think is necessary I can add for each number field type.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Sweet! Seems to address the failure from the previous MR.

xjm’s picture

Status: Reviewed & tested by the community » Needs work

Hiding patches; I just wasted 20 minutes reviewing an old thing. :P

xjm’s picture

NW for many things as per my MR comments. Thanks everyone!

joaopauloc.dev’s picture

This issue Field UI doesn't validate a field's default value on create is very similar, should I fix this issue here added as related issue or it's better to fix in another branch?
Working on code review comments

joaopauloc.dev’s picture

Status: Needs work » Needs review

smustgrave’s picture

Status: Needs review » Needs work

Small tweak.

joaopauloc.dev’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Seems feedback has been addressed!

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Related issues: +#2907520: Field UI doesn't validate a field's default value on create

Thanks @joaopauloc.dev and @smustgrave!

We should avoid mixed if at all possible. PHP 8 supports union types so we can say int|string based on the current test. (This fix includes an API addition and is therefore not backportable, so we don't need to worry about legacy PHP 7 compatibility for D9.)

Interesting aside that does not actually affect the MR here -- note the behavior of int typehints with integer strings:
https://3v4l.org/LnQvW

Regarding #41, I added that to the related issues for now.

joaopauloc.dev’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

That was my mistake for suggesting it. Didn't know we wanted to avoid mixed but now I do!

quietone’s picture

Status: Reviewed & tested by the community » Needs work

Found a few things, commented in the MR. Of most concern is the behavior change which now prevents setting the min = max.

joaopauloc.dev’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Think this is good for RTBC again.

lauriii made their first commit to this issue’s fork.

  • lauriii committed 2ceafd06 on 11.x
    Issue #3052663 by joaopauloc.dev, smustgrave, quietone, Balu Ertl, xjm,...

  • lauriii committed f17b0d60 on 10.2.x
    Issue #3052663 by joaopauloc.dev, smustgrave, quietone, Balu Ertl, xjm,...
lauriii’s picture

Version: 11.x-dev » 10.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 2ceafd0 and pushed to 11.x. Also cherry-picked to 10.2.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.