forms - Craft docs 中的输入表单示例不起作用
问题描述
我是 Craft 的新手,来自 ExpressionEngine 世界。我正在学习如何创建前端输入表单。我已经设置了一个部分,创建了一些字段并为表单制作了一个模板以进行条目。
{% macro errorList(errors) %}
{% if errors %}
<ul class="errors">
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}
{# If there were any validation errors, an `entry` variable will be passed to the
template, which contains the posted values and validation errors. If that’s not
set, we’ll default to a new entry. #}
{% set entry = entry ?? create('craft\\elements\\Entry') %}
<form method="post" accept-charset="UTF-8">
{{ csrfInput() }}
{{ actionInput('entries/save-entry') }}
{{ redirectInput('missed-connections/{slug}') }}
{{ hiddenInput('sectionId', '1') }}
{{ hiddenInput('enabled', '1') }}
<label for="title">Title</label>
{{ input('text', 'title', entry.title, {
id: 'title',
class: entry.hasErrors('title') ? 'error',
}) }}
{{ _self.errorList(entry.getErrors('title')) }}
<label for="body">Body</label>
{{ tag('textarea', entry.body, {
id: 'body',
name: 'body',
class: entry.hasErrors('body') ? 'error',
}) }}
{{ _self.errorList(entry.getErrors('body')) }}
<input type="submit" value="Publish">
</form>
上面的代码是直接从工艺文档中复制的,并针对Section等进行了调整。渲染页面失败,出现两个错误:
public function tagFunction(string $type, array $attributes = []): string
class: entry.hasErrors('body') ? 'error',
创建的字段名称为“body”,sectionID 正确。
当我将 body 字段的代码替换为以下内容(我在本网站其他地方找到的一个想法)时,页面可以正常工作,我可以提交一个条目:
<label for="body">Body</label>
<textarea id="body" name="fields[body]" rows="20"></textarea>
我的问题是为什么文档中的示例代码在静态 html 字段(上图)起作用时不起作用?
如果静态 html 字段是要走的路,那么我如何让错误报告工作,此外,对于更高级的字段,例如文件上传和标签,我如何加载正确的 css/脚本来呈现它们并让它们发挥作用他们在后端做手艺吗?
谢谢你。
解决方案
推荐阅读
- hazelcast - 自动 Hazelcast WAN 同步
- javascript - 自定义维度不适用于 gtag.js
- mongodb - 我们如何使用查询通过带有一些过滤器的聚合来查找字段总和?
- swift - 如何在每个选择器视图值前面包含一个单词
- powerbi - 使用 DAX 在 Powerbi 中计算平均速度
- python - Pandas 数据框:如何将索引后的值设置为 0
- excel - 如何获取将数据从 sheet2 复制到 sheet1 的代码?
- java - 如何使用 Selenium 获取元素标签的请求
- html - Soundcloud iframe 实现到 bootstrap4 问题
- sorbet - 关于冰糕设置的问题:如何消除步骤 4 中的错误