Skip to content

Empty optional number-field macros produce NaN in calculated-field formulas #644

@eclipses

Description

@eclipses

Summary

When a calculated field's formula references a macro for an optional number field that the user leaves empty, JetFormBuilder substitutes an empty string into the expression. The resulting JS is malformed and evaluates to NaN, which then propagates through any dependent calculated fields and renders the visible total as 0 (or NaN).

This is not a jfb-is-valid-inputs-field issue — it reproduces against JetFormBuilder alone.

Minimal reproduction

  1. Create a form with:
    • Number field arequired, default 4
    • Number field bnot required, no default
    • Calculated field with data-formula="%a%*%b%" and data-precision="0"
  2. Submit/render the form without filling b.

Expected: the calculated field renders 0 (or a documented well-defined value), or the formula is short-circuited.
Actual: the substituted expression becomes 4* (empty after *), evaluates to NaN, and the field renders 0 due to precision rounding — silently hiding the failure.

Real-world example

Live page: https://www.bakingwiththeory.com/calculators/advanced/manual-calculator-round-pizza-ingredients-direct-dough/

The "Total weight of dough" field has:

data-formula="%is_valid% ? %pcs%*%weight%+%field_totale_scarto% : 0"

where field_totale_scarto has:

data-formula="%pcs%*%weight%*%waste%/100"

waste is an optional field (no required attribute, min=0). If the user fills only the required fields and leaves waste empty, field_totale_scarto evaluates to NaN, which propagates and renders the total as 0 g.

Design question for maintainers

What should an empty number-field macro expand to? Options I see:

  • 0 — fixes additive formulas, breaks division
  • null (treated as 0 numerically) — same as above
  • The field's declared default — most correct but bigger change
  • A per-calculated-field "coerce empty operands to 0" toggle — opt-in, no compat risk

Happy to open a PR once there's direction on which behavior you'd accept.

Cross-reference

Originally reported here: https://github.com/Crocoblock/jfb-is-valid-inputs-field/issues/

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions