<p>It's hard to be more specific without some more context for each of the snippets you post, but:</p>

<pre><code>{% if errors is defined %}
    &lt;ul class="errors"&gt;
        {% for error in errors %}
            &lt;li&gt;{{ error }}&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
{% endif %}
</code></pre>

<p>That's checking if a variable named <code>errors</code> has been passed into the template, and if so it assumes it's an array and loops through the array to display each error in an unordered list.</p>

<pre><code>{% if errorMessage is defined %}
    &lt;p class="error"&gt;{{ errorMessage }}&lt;/p&gt;
{% endif %}
</code></pre>

<p>That's checking is a variable named <code>errorMessage</code> has been passed into the template and if so it assumes it's a string and just displays the single error message.</p>

<pre><code>{% if model and model.getErrors('firstName') %}
   &lt;span class="error"&gt;{{ model.getErrors('firstName')|join }}&lt;/span&gt;
{% endif %}
</code></pre>

<p>That's checking if a variable named <code>model</code> has been passed into the template and assumes it's an instance of <a href="https://craftcms.com/classreference/models/BaseModel" rel="nofollow">BaseModel</a>.  Then it calls the <a href="http://www.yiiframework.com/doc/api/1.1/CModel#getErrors-detail" rel="nofollow">getErrors</a> method of the class passing in <code>firstName</code> as a parameter to specifically check if any errors have been set on its <code>firstName</code> attribute.  If so, it will take that array of errors and display them using Twig's <a href="http://twig.sensiolabs.org/doc/filters/join.html" rel="nofollow">join</a> filter.</p>

CC BY-SA 3.0
2016-08-03T17:06:58.410Z
2016-08-03T17:06:58.410Z
Brad Bell
<p>As a newbie to Craft and Craft Commerce. I wondered if someone could explain the difference between all these error codes that I have seen either in the docs, this forum or the templates and when they get set and when I should use each method in which situations? Apologies if this is a broad question...</p>

<pre><code>{% if errors is defined %}
    &lt;ul class="errors"&gt;
        {% for error in errors %}
            &lt;li&gt;{{ error }}&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
{% endif %}


{% if errorMessage is defined %}
   &lt;p class="error"&gt;{{ errorMessage }}&lt;/p&gt;
{% endif %}

{% if model and model.getErrors('firstName') %}
   &lt;span class="error"&gt;{{ model.getErrors('firstName')|join }}&lt;/span&gt;
{% endif %}
</code></pre>

<plugin-craftcommerce><error-message><error>
Getting to grips with errors and flash messages
# Getting to grips with errors and flash messages

(Asked by: username_0 on 2016-08-03)

As a newbie to Craft and Craft Commerce. I wondered if someone could explain the difference between all these error codes that I have seen either in the docs, this forum or the templates and when they get set and when I should use each method in which situations? Apologies if this is a broad question...
{% if errors is defined %}
    
        {% for error in errors %}
            {{ error }}
        {% endfor %}
    
{% endif %}


{% if errorMessage is defined %}
   {{ errorMessage }}
{% endif %}

{% if model and model.getErrors('firstName') %}
   {{ model.getErrors('firstName')|join }}
{% endif %}

**Tags:** plugin-craftcommerce, error-message, error


---

## 1 answers

### Answer by username_1 on 2016-08-03. Score: 1

It's hard to be more specific without some more context for each of the snippets you post, but:
{% if errors is defined %}
    
        {% for error in errors %}
            {{ error }}
        {% endfor %}
    
{% endif %}

That's checking if a variable named errors has been passed into the template, and if so it assumes it's an array and loops through the array to display each error in an unordered list.
{% if errorMessage is defined %}
    {{ errorMessage }}
{% endif %}

That's checking is a variable named errorMessage has been passed into the template and if so it assumes it's a string and just displays the single error message.
{% if model and model.getErrors('firstName') %}
   {{ model.getErrors('firstName')|join }}
{% endif %}

That's checking if a variable named model has been passed into the template and assumes it's an instance of BaseModel.  Then it calls the getErrors method of the class passing in firstName as a parameter to specifically check if any errors have been set on its firstName attribute.  If so, it will take that array of errors and display them using Twig's join filter.


---

