<p>Thanks to a teammate, got this working!</p>

<p>Needed to change the conditional wrapping my error messages from </p>

<pre><code>{% if entry is defined %}
</code></pre>

<p>to</p>

<pre><code>{% if entry is defined and entry is not null %}
</code></pre>

CC BY-SA 4.0
2019-07-16T21:05:59.190Z
2019-07-16T21:05:59.190Z
Katie Fritz
<p>I'm using a front end form so logged-in users can create a new entry ("post") or edit an existing entry. Here's the controller for figuring out whether it's a new or existing entry:</p>

<pre><code>{# Get the entry being edited from the url parameter #}
{% set entry_id = craft.app.request.getParam('id') %}
{% if entry_id %}
  {% set post = craft.entries({
    id: entry_id,
    status: null
  }).one() %}
{% else %}
  {% set post = null %}
{% endif %}
</code></pre>

<p>For the form fields, I'm using Twig partials and <code>with { 'entry' : post }</code>. </p>

<p>This works great for editing a post, and I can create a new post as long as I enter the info that Craft expects. However, there are some required fields, and I want to show error messages when those fields are missing, rather than the post silently failing to be created.</p>

<p>Per the <a href="https://docs.craftcms.com/v3/dev/examples/entry-form.html" rel="nofollow noreferrer">entry form documentation</a>, I'm including the suggested macro and this chunk of code with my required <code>headline</code> field: </p>

<pre><code>{% if entry is defined %}
  {{ errorList(entry.getErrors('headline')) }}
{% endif %}
</code></pre>

<p><strong>Desired behavior:</strong> 
if I try to save a new entry that's missing a headline, I should get the error message for that field.</p>

<p><strong>Actual behavior:</strong>
This gives me a runtime error: <code>Impossible to invoke a method ("getErrors") on a null variable.</code></p>

<p>I've tried setting <code>post = {}</code> for an empty object, but Twig seems to see that as an array and can't invoke <code>getErrors</code> on that either.</p>

<p>How should I go about addressing this?</p>

<variable><validation><frontend-form><error-handling>
How do I show errors for a new front end entry form?
# How do I show errors for a new front end entry form?

(Asked by: username_0 on 2019-07-16)

I'm using a front end form so logged-in users can create a new entry ("post") or edit an existing entry. Here's the controller for figuring out whether it's a new or existing entry:
{# Get the entry being edited from the url parameter #}
{% set entry_id = craft.app.request.getParam('id') %}
{% if entry_id %}
  {% set post = craft.entries({
    id: entry_id,
    status: null
  }).one() %}
{% else %}
  {% set post = null %}
{% endif %}

For the form fields, I'm using Twig partials and with { 'entry' : post }. 
This works great for editing a post, and I can create a new post as long as I enter the info that Craft expects. However, there are some required fields, and I want to show error messages when those fields are missing, rather than the post silently failing to be created.
Per the entry form documentation, I'm including the suggested macro and this chunk of code with my required headline field: 
{% if entry is defined %}
  {{ errorList(entry.getErrors('headline')) }}
{% endif %}

Desired behavior: 
if I try to save a new entry that's missing a headline, I should get the error message for that field.
Actual behavior:
This gives me a runtime error: Impossible to invoke a method ("getErrors") on a null variable.
I've tried setting post = {} for an empty object, but Twig seems to see that as an array and can't invoke getErrors on that either.
How should I go about addressing this?

**Tags:** variable, validation, frontend-form, error-handling

---

## 1 answers

### Answer by username_0 on 2019-07-16. Score: 1 | Accepted answer

Thanks to a teammate, got this working!
Needed to change the conditional wrapping my error messages from 
{% if entry is defined %}

to
{% if entry is defined and entry is not null %}


---

