首页 > 解决方案 > 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/脚本来呈现它们并让它们发挥作用他们在后端做手艺吗?

谢谢你。

标签: formscraftcms

解决方案


推荐阅读