[BUG] Content component throws ReferenceError: field is not defined when HTML contains words matching i18n keys

Open
#6,300 1 comment 0 reactions 1 assignee View on GitHub

@Victoriya25 is already working on this.

Since May 6, 2026.

Assessment

This issue has not been assessed yet.

Description

Environment

  • Formio.js version: 4.21.0 (introduced in PR #4564 / commit 761fabb)
  • Working version: 4.14.0

Steps to Reproduce

  1. Create a Content component with HTML pasted from a rich text editor (Google Docs, Microsoft Word, etc.)
  2. The pasted HTML wraps individual words in separate <span> tags with inline styles (this is standard behavior for rich text editors)
  3. Ensure the HTML body text contains common English words like "required", "unique", "pattern", "select", "min", or "max"
  4. Render the form

Example JSON:

{
  "components": [
    {
      "type": "content",
      "key": "content",
      "html": "<p><span style=\"color:#3b3b00;\">should this be </span><span style=\"color:#3b3b00;\">required</span><span style=\"color:#000000;\">. </span></p>"
    }
  ]
}

Observed Behavior

Console error:

Evaluator.js: Error interpolating template ReferenceError: field is not defined

The Content component fails to render.

Expected Behavior

The Content component should render the HTML as-is without errors.

Root Cause Analysis

PR #4564 (FIO-3939) changed Element.js:572 from:

if (typeof string !== 'function' && this.component.content
    && !FormioUtils.Evaluator.templateSettings.interpolate.test(string)) {

to:

if (typeof string !== 'function' && (this.component.content || this.component.html)
    && !FormioUtils.Evaluator.templateSettings.interpolate.test(string)) {

This was done to enable i18n translation for Content components (which use component.html, not component.content). However, it introduced a regression:

  1. The Content component's body HTML now passes through translateHTMLTemplate().
  2. translateHTMLTemplate walks the DOM tree and calls translateDeepTag → translateElemValue on each element.
  3. Since SPAN is not in the inTextTags list, the function recurses into each <span> individually.
  4. When a <span> contains only the text "required", this.t("required") is called.
  5. FormIO's default i18n maps "required" → "{{field}} is required".
  6. The translated HTML now contains {{field}}, which lodash _.template() tries to evaluate as JavaScript.
  7. field is not in the evaluation context ({utils, util, user, _, moment, ...}), so it throws ReferenceError: field is not defined.

This affects any word in the HTML that is isolated in its own element and exactly matches a FormIO i18n key. The most common collisions with natural English text are: required, unique, pattern, select, min, max, complete, error, cancel, submit, next, previous.

Rich text editors (Google Docs, Word) routinely wrap every few words in separate <span> tags with per-word styling, making this collision very likely for any form with legal/consent text.

Suggested Fix

The fix in translateHTMLTemplate/translateElemValue should either:

  1. Skip translations whose output introduces {{ }} template expressions (to prevent lodash interpolation failures), or
  2. Check if the translated value is a known validation message template before applying it, or
  3. Add SPAN to the inTextTags list so spans are not translated individually

Simply reverting to this.component.content (removing || this.component.html) works but disables Content component i18n entirely.

Related Issues

  • #3531 — Same category: i18n translations bleeding into content they shouldn't
  • #5874 — Related: {{field}} not being resolved in validation messages
  • #4608 — Related: HTML component interpolation issues
Dominant language
JavaScript
Stars
2.1k
Forks
1.1k
Avg merge
3d 8h
Merged PRs (30d)
1

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from formio/formio.js

All issues in formio/formio.js

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.