1

I set up a single page "Add Post" in the CP.

In following the example here https://craftcms.com/docs/templating/entry-form my form title input field is pre-populated with "Add Post" This is obviously not the desired result. When I include an entry id for modifying the post it grabs the title from the post as it should.

How do I create a new post without having a form pre-populated with "Add Post" in the title input field?

{% macro errorList(errors) %}
    {% if errors %}
        <ul class="errors">
            {% for error in errors %}
                <li>{{ error }}</li>
            {% endfor %}
        </ul>
    {% endif %}
{% endmacro %}

{% from _self import errorList %}

<label for="title">Title</label>
<input id="title" type="text" name="title"
    {%- if entry is defined %} value="{{ entry.title }}"{% endif -%}>
  • How do you mean, pre-populated? Does the title field contain Add Post, or do you entries get saved with Add Post as title? Please post your form to shine some light on your problem. – Paul Aug 29 '14 at 15:57
1

This example code makes use of the entry variable set by Craft to retrieve previously posted data. The variable is set if you encounter validation errors or if you want to modify upon an already saved entry (→ include the entryId in a hidden input field).

The problem is, that you already have an entry variable available to your template because every entry page comes pre-populated with one (if the URI is matched).

Easiest fix would probably be to make this single entry not match the URI set in the section's CP settings. Another idea would be to only fix that title problem and modify the example code:

{% if entry is defined %} value="{{ entry.title }}"{% endif %}

and use this instead:

{% if entry.errors is defined %} value="{{ entry.title }}"{% endif %}
  • The {%if errors %} seems to have fixed things. Thanks. – David A McInnis Aug 29 '14 at 20:06
  • @David I corrected my answer, error messages are provided as a property of the entry variable, sorry for the confusion. – carlcs Sep 4 '14 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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