Entry Form example from Craft docs doesn't work

274 Views Asked by At

I'm very new to Craft and come from the ExpressionEngine world. I'm learning how to create a front-end entry form. I've setup a section, created some fields and made a template for the form to make entries.

{% 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> 

The above code is copied directly from craft documentation and adjusted for Section, etc. Rendering the page fails with two errors:

public function tagFunction(string $type, array $attributes = []): string

class: entry.hasErrors('body') ? 'error',

The created field name is "body" and the sectionID is correct.

When I replace the code for the body field with the following (an idea I found elsewhere on this site) the page works and I can submit an entry:

  <label for="body">Body</label>
  <textarea id="body" name="fields[body]" rows="20"></textarea>

My question is why doesn't the example code from the documentation work when the static html field (above) does?

If static html fields are the way to go then how do I get error reporting to work, additionally, for more advanced fields such as file uploads and tags, how do I load the proper css/scripts to render them and have them to function as they do in the back-end of craft?

Thank you.

0

There are 0 best solutions below