integer with minimum but no maximum causing ValueError: mt_rand(): Argument #2 ($max) must be greater than or equal to argument #1 ($min)

Created on 17 December 2022, about 2 years ago
Updated 9 September 2024, 3 months ago

Problem/Motivation

Error page when attempting to use layout builder with integer fields with minimum value >= 1000 and no maximum.

ValueError: mt_rand(): Argument #2 ($max) must be greater than or equal to argument #1 ($min) in mt_rand() (line 108 of core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php).

Steps to reproduce

  1. Add Number (integer) field to a content type.
  2. Set minimum value to 1000 or higher. Leave maximum field blank.
  3. Enable layout builder module.
  4. Manage Display for content type -> Use Layout Builder and save.
  5. Click Manage Layout.

Proposed resolution

Ensure generateSampleValue() maximum default is >= minimum value.

 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
     $min = $field_definition->getSetting('min') ?: 0;
-    $max = $field_definition->getSetting('max') ?: 999;
+    $max = $field_definition->getSetting('max') ?: $min + 999;
     $values['value'] = mt_rand($min, $max);
     return $values;
   }
🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Field 

Last updated 2 days ago

Created by

🇺🇸United States scottsperry

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024