# 表单模板编辑器中的字段在编辑预览中丢失了换行符

**URL:** <https://meta.discourse.org/t/form-template-composer-fields-lose-their-newlines-in-the-edit-preview/411146>\
**Category:** Bug\
**Tags:** form-templates\
**Created:** [2026年八月28日 13:24 UTC](https://meta.discourse.org/t/form-template-composer-fields-lose-their-newlines-in-the-edit-preview/411146 "2026-08-28T13:24:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ScaryLarryGames1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/scarylarrygames1/32/575669_2.png) [@ScaryLarryGames1](https://meta.discourse.org/u/ScaryLarryGames1)\
**Post date:** [2026年八月28日 13:24 UTC](https://meta.discourse.org/t/form-template-composer-fields-lose-their-newlines-in-the-edit-preview/411146/1 "2026-08-28T13:24:25Z")

</div>

在编辑使用表单模板的帖子时，编辑器预览会将任何多行 `composer` 字段渲染为一行连续文本：标题与其后的句子粘连在一起，列表和 BBCode 则显示为纯文本。保存后的帖子渲染正常——只有预览是错误的。

**原因：** `form-template-field/composer.gjs` 将每个 composer 字段镜像到一个隐藏的 `<input type="text">` 中，以便进行原生验证：

```plaintext
<input id={{this.validationInputId}} type="text" name={{@id}} value={{this.composerValue}} ... class="form-template-field__composer-hidden-input" />

```

编辑器回复——即预览所渲染的内容——是由这些输入框组装而成的。根据 HTML 值净化规则，`type="text"` 输入框在赋值时会剥离 CR/LF（回车/换行符），因此在预览看到该字段值时，其中的所有换行符都已丢失。保存时读取的是编辑器本身，因此存储的原始内容不受影响。

**复现步骤** （2026.9.0-latest）：任何包含 `composer` 字段的表单模板 → 输入包含空行的文本（例如 `## 标题`、一个段落、一个列表） → 打开已保存帖子的编辑模式 → 预览显示内容被扁平化为单行，而帖子本身渲染正常。

**我们作为主题正在使用的变通方案：** 将隐藏输入框改为 `type="hidden"`（这在赋值时会保留换行符），从字段编辑器中修复已被扁平化的值，并触发一个 input 事件以重建回复。这可以完全恢复预览。

核心修复注意事项：`type="hidden"` 会跳过原生的 `required` 约束验证，因此替代控件最好是一个视觉上隐藏的 `<textarea>`，而不是文本输入框。

---

<div class="post-metadata">

**Author:** ![tannerabread](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tannerabread/32/526153_2.png) [@tannerabread](https://meta.discourse.org/u/tannerabread)\
**Post date:** [2026年八月31日 20:43 UTC](https://meta.discourse.org/t/form-template-composer-fields-lose-their-newlines-in-the-edit-preview/411146/3 "2026-08-31T20:43:11Z")

</div>

你好 @ScaryLarryGames1，感谢你报告此问题。

最终的修复方案正是你所建议的 `textarea` 方案，并且刚刚已经合并。请再等几分钟，让 CI/CD 流程完成并将其发布到 `latest` 分支。

> <https://github.com/discourse/discourse/pull/43043>
>
> Follow up to #42192, which switched the composer field's hidden validation stand…-in from \`type="hidden"\` to \`type="text"\` so \`required\` would fire on submit. Text inputs strip CR/LF on value assignment per the HTML value-sanitization rules, and the reply body is assembled from these controls via \`new FormData(form)\`, so multi-line composer input came out flattened in the edit preview. Saved posts were unaffected because the raw source is read from the editor directly.
> 
> Swap the stand-in to a visually hidden \`\<textarea\>\`. Textareas participate in constraint validation just like text inputs, preserving the required-field enforcement from #42192, but do not sanitize newlines, so multi-line content survives in the preview.
