Registry Mirror
Skip to content

FieldNumber constructor ignores constraints if a config object is present #3970

Open

Description

@mattbishop

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

        .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:

  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

Activity

  1. added
    status: triage
    on Jun 14, 2020
  2. mattbishop commented on Jun 14, 2020

    @mattbishop
    Author

    This may be a documentation issue as the internal comment suggests, though it would be helpful to throw an error explaining the rule too.

  3. added and removed
    status: triage
    on Jun 16, 2020
  4. added this to the Bug Bash Backlog milestone on Jun 16, 2020
  5. added theissue type on Jul 8, 2026
  6. zspriggs commented on Sep 25, 2026

    @zspriggs
    Contributor

    Here's some more specific steps for anyone who wants to work on this issue:

    • Add a warning to the FieldNumber constructor if both a constraints and a config option are present. The warning should say something about how the constructor constraints will be ignored/overwritten if a config is present. You can use console.warn for this.
    • Add information to the "Creation" section of the number field guides.
      • Link the reference docs for the FieldNumber constructor on the text "the number constructor"
      • "An optional config object" should be added to the bulleted list, and this item should link the reference docs
      • Just below this bulleted list, a line should be added that the constructor will ignore constructor constraints if a config is present.
  7. added
    good first issue
    help wanted
    and removed
    good first issue
    on Sep 25, 2026
  8. 25x55a0405-glitch commented on Sep 29, 2026

    @25x55a0405-glitch

    Hi! I'd like to work on this. The steps in the comments are really clear - I'll add the console.warn when both constraint args and a config object are passed, plus update the docs. Should have a PR up soon.

  9. zspriggs commented on Sep 29, 2026

    @zspriggs
    Contributor

    @25x55a0405-glitch Sounds great, thanks for working on it!

  10. Anshmittal86 commented on Nov 9, 2026

    @Anshmittal86
    Contributor

    Hi @25x55a0405-glitch, are you still working on this? If not, I’d be happy to pick it up. No worries either way!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: blocks & fieldsarea: docs & examplesgood first issuehelp wanted

    Type

    Projects

    • Status
      Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions