1

I'm pretty new to craft and creating my first front end entry form using the demo code provided by craft. For some reason I'm getting a 'Request missing required body param' error. I've tried adding but that didn't solve the issue.

Here's my code:

{% extends "_layout.twig" %}

{% macro errorList(errors) %}
  {% if errors %}
    {{ ul(errors, {class: 'errors'}) }}
  {% 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') %}

{# Add `enctype="multipart/form-data"` to `<form>` if you’re 
   uploading files. #}
<form method="post" accept-charset="UTF-8" enctype="multipart/form-data">
  {{ csrfInput() }}
  {{ actionInput('entries/save-entry') }}
  {{ redirectInput('viewentry/{slug}') }}
  {{ hiddenInput('friends', '2') }}

<input type="hidden" name="entryId" value="">

<div class="mb-6">
  <label for="title" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your Dog's Name</label>
  {{ input('text', 'title', entry.title, {
    id: 'title',
    class:'bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500',
  }) }}
  {{ _self.errorList(entry.getErrors('title')) }}
</div>
  
<div class="mb-6">
<label for="featureImage" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Feature Image</label>

  {{ input('file', 'featureImage', entry.featureImage, {
    id: 'featureImage',
  }) }}
  {{ _self.errorList(entry.getErrors('featureImage')) }}
</div>

<div class="mb-6">
  <label for="postContent" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Tell us about your dog</label>
  {{ tag('textarea', {
    id: 'postContent',
    name: 'fields[postContent]',
    class:'bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500',
  }) }}
  {{ _self.errorList(entry.getErrors('postContent')) }}
</div>


<div class="mb-8">
<label for="postCategories" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Breed</label>
<select name="fields[postCategories]" id="postCategories">
    {# Create a category query with the 'group' parameter #}
{% set myCategoryQuery = craft.categories()
  .group('blog') %}
{# Fetch the categories #}
{% set categories = myCategoryQuery.all() %}
  {# display post categories #}
  {% if entry.postCategories|length %}
  <div class="border-t py-2 mb-6">
    {% for category in entry.postCategories.all() %}
      <a href="{{ category.url }}" class="inline-block border rounded px-2 py-1 text-sm">
        {{- category.title -}}
      </a>
    {% endfor %}
  </div>
  {% endif %}
  {% nav category in categories %}
    <li>
      <option>{{ category.title }}</option>
    </li>
  {% endnav %}  
</select>
</div>
{#
{% set field = craft.app.fields.getFieldByHandle('postCategories') %}

{{ hiddenInput('fields[postCategories]', '') }}

<select multiple name="fields[postCategories][]">
  {% for option in field.options %}
      {% set selected = entry is defined
        ? entry.postCategories.contains(option.value)
        : option.default %}

      <option value="{{ option.value }}"
        {% if selected %} selected{% endif %}
      >
        {{ option.label }}
      </option>
  {% endfor %}
</select>
#}
<div class="mb-6">
  <button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Publish</button>
</div>
</form>

{% endblock %}

1 Answer 1

1

You need to add sectionId to your form:

{{ hiddenInput('sectionId', xx) }}

If you have multiple entry types, you can also add typeId to the form:

{{ hiddenInput('typeId', xx) }}

For both examples, replace xx with an actual value, of course :)

3
  • I did have that in initially but the craft example says to switch 'sectionId' for the id of the section you're posting to. So I changed it to 'blog' but got the error code. Am I supposed to leave {{ hiddenInput('sectionId', '2') }} untouched and specify the blog somewhere else? Commented Dec 1, 2022 at 12:04
  • 1
    You need to change 2 to your actual section id. You can find that by going to Settings → Sections, click your section then look at the number in your address bar. Alternatively, you'll find this in your database's sections table.
    – Oli
    Commented Dec 1, 2022 at 12:33
  • That was what I was missing, thank you!! Commented Dec 2, 2022 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.