#3970·blockly

FieldNumber constructor ignores constraints if a config object is present

Author: mattbishopCreated Jun 14, 2020Updated Sep 29, 2026
Labelshelp wantedgood first issuearea: blocks & fieldsarea: docs & examples

Describe the bug

When constructing a number field, one passes in 4 constraints, a validator and a config object. If the config object is present, the constructor constraints are ignored, even if the config object does not replace them.

To Reproduce

javascript
        .appendField(new Blockly.FieldNumber(
          this.state.minLength.value, 0, Number.MAX_SAFE_INTEGER, 1,
          (value) => {
            if (this.state.maxLength.enabled && value > this.state.maxLength.value) {
              return null
            }
            this.state.minLength.value = value
            return value
          },
          { tooltip: 'Minimum length of the string' }))

This constructed field ignores the precision setting of 1. FieldNumber's constructor (lines 69-74) has this logic which produces the bug:

javascript
  Blockly.FieldNumber.superClass_.constructor.call(
      this, opt_value || 0, opt_validator, opt_config);

  if (!opt_config) {  // Only do one kind of configuration or the other.
    this.setConstraints(opt_min, opt_max, opt_precision);
  }

Expected behavior

Constructor constraints should be used unless overridden in the config.

Ideally one would have a separate constructor with just the config object providing the constraints and validators.

Desktop (please complete the following information):

  • OS:MacOS 10.15.5
  • Browser chrome 83, safari 13.1

Source: RaspberryPiFoundation/blockly