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 <input type="hidden" name="entryId" value=""> 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 %}
George Bleasdale
  • 343
  • 7
  • 16

1 Answers1

1

I didn't realise that the 2 in {{ hiddenInput('sectionId', '2') }} had to be changed for the ID of the channel - not the handle.

For anyone else stuck with this, the ID can be found in the URL when you're editing the channel in the dashboard settings.

More details here >>

DarkBee
  • 16,592
  • 6
  • 46
  • 58
George Bleasdale
  • 343
  • 7
  • 16