Skip to content

Calculations ​

Phase 8 evaluates numeric formulas on the server (CalculationEngine) and in the browser (assets/frontend/js/form.js). Posted calculation values are never trusted.

The parser is a recursive-descent evaluator. It does not call eval(), create_function(), or similar.

Field types ​

Number (number) ​

Numeric input (type="number") with optional min, max and step (any or a positive number). Empty values are allowed unless the field is required.

Calculation (calculation) ​

Read-only result of settings.formula. Extra settings: decimals (0–6, default 2), prefix, suffix.

json
{
  "id": "field_total",
  "type": "calculation",
  "key": "total",
  "label": "Total",
  "required": false,
  "settings": {
    "formula": "{qty} * {price} * 1.19",
    "decimals": 2,
    "prefix": "€ ",
    "suffix": ""
  }
}

Formula syntax ​

Operators: + - * / and parentheses. Variables:

FormMeaning
{qty}Field key qty
{field:qty}Same, merge-tag style
qtyBare field key

Missing or non-numeric values count as 0. Hidden fields are empty before the formula runs, so they also count as 0. A field must not use its own key (self-reference is 0).

Functions: min, max, abs, round, floor, ceil. round(x, n) uses n decimal places (0–6).

Invalid expressions, unknown functions, division by zero and oversized results evaluate to 0 on the public form. Saving a form rejects a non-empty invalid formula.

Place quantity/price fields above totals, and totals above fields whose visibility depends on the result.

Runtime ​

  1. SubmitService sanitizes inputs and discards posted calculation values.
  2. The engine fills calculation keys in definition order.
  3. Visibility (Phase 7) runs; hidden keys are removed.
  4. Calculations run again so hidden sources do not affect stored totals.
  5. Visible fields are validated. Number min/max apply; calculation fields do not.

{all_fields} and {field:total} in email actions use the server-computed number.

REST catalogue: GET /wp-json/ad-form/v1/calculations (capability edit_ad_form_forms).

PHP:

  • Dingfelder\AdForm\Calculations\CalculationEngineInterface
  • Dingfelder\AdForm\Calculations\CalculationEngine

Digitale Lösungen. Persönlich. Zukunftssicher.