<p>To grab validation error of a <em>matrix field</em> -> <em>first block type</em> -> <em>field</em>, you must access entry's matrix field with the function <code>first()</code> and than access block type field using <code>getError("fieldHandle")</code> function at the end of <code>ElementCriteriaModel</code> like so.</p>

<pre><code>{{ entry.matrixField.first().getError("customFieldHandle") }}
</code></pre>

<p>or to the question related problem</p>

<pre><code>{{ entryError.cart.first().getError("productQuantity") }}
</code></pre>

<p>Doing this way you don't have to loop through Matrix's blocks and it make sense if you need to access first block type only or you have only one block type.</p>

<p>To show validation error under each <code>block.type</code> of a custom field you'll have to loop in <code>MatrixBlockModel</code> of the <code>cart</code> channel like so.</p>

<pre><code>{% if entryError is defined %}
    {% for block in entryError.cart %}
        {% if block.getErrors() %}
            {{ block.getError('productQuantity') }}
        {% endif %}
    {% endfor %}
{% endif %}
</code></pre>

<p>Like Brad pointed out to me in support ticket "you can decide how you want to display them".</p>

CC BY-SA 4.0
2018-10-17T08:59:24.420Z
2018-10-17T08:59:24.420Z
Dominik Krulak
<p>First I illustrate the way I set up guest entry form.</p>

<p>Let's say I have channels <del><code>blog</code></del> <code>product</code> and <del><code>comment</code></del> <code>cart</code>.</p>

<p>Guest entry submits to the <del><code>comment</code></del> <code>cart</code> channel. Form lives in template which I use to show <del>blog's</del> product's entry.</p>

<p><del>Now I just send required field title which won't get passed by validation unless it isn't blank.</del></p>

<p>According to <a href="https://github.com/craftcms/guest-entries#settings" rel="nofollow noreferrer">Guest Entries settings</a> to show my <del>blog's</del> product's content along with my guest entry form I have to change the name of <code>entryVariable</code> key from <code>entry</code> to whatever not taken variable name let's say <code>entryError</code> in an event of validation error to not overwrite <del>blog's</del> product's variable <code>entry</code>.</p>

<p><strong>In short this is how the template looks like when <del>blog</del> product entry load.</strong> This is just part of the form which is executed when first item is added to the <code>cart</code> matrix field in <code>cart</code> channel.</p>

<pre><code>{# Product's title #}
{{ entry.title }}

{# Product's text #}
{{ entry.text }}


{# Show form error #}
{% macro errorList(errors) %}
    {% if errors %}
        &lt;ul class="errors"&gt;
            {% for error in errors %}
                &lt;li&gt;{{ error }}&lt;/li&gt;
            {% endfor %}
        &lt;/ul&gt;
    {% endif %}
{% endmacro %}

{% from _self import errorList %}


&lt;form method="post" accept-charset="UTF-8"&gt;
    {{ getCsrfInput() }}
    &lt;input type="hidden" name="action" value="guestEntries/saveEntry"&gt;
    &lt;input type="hidden" name="sectionId" value="2"&gt;

    &lt;input type="hidden" name="title" value="Some dynamic title"&gt;


    {# Matrix field "cart" #}
    {# Block type field we want the product to save in. #}
    &lt;input type="hidden" name="fields[cart][new1][type]" value="product"&gt;

    &lt;input type="hidden" name="fields[cart][new1][fields][productTitle]" value="{{ entry.title }}"&gt;

    {# Quantity #}
    &lt;label&gt;Quantity *&lt;/label&gt;
    &lt;select id="qntySlct" name="fields[cart][new1][fields][productQuantity]"&gt;
        &lt;option value=""&gt;--Please, select the quantity--&lt;/option&gt;
        {% for i in 1..10 %}
            &lt;option value="{{ i }}"{% if productQuantity == i %} selected{% endif %}&gt;{{ i }}&lt;/option&gt;
        {% endfor %}
        &lt;option value=""&gt;I need more&lt;/option&gt;
    &lt;/select&gt;

    {# Error message #}
    {% if entryError is defined %}
        &lt;div class="errMsg"&gt;
            {{ errorList(entry.getErrors('productQuantity')) }}
        &lt;/div&gt;
    {% endif %}

&lt;/form&gt;
</code></pre>

<p>Form submits if <del>title</del> quantity is defined and if not <strong>I get</strong> <code>entryError</code> <strong>defined but don't get the <del>title's</del> quantity's  error shown</strong>.</p>

<p>The warning of <del>title's</del> quantity's error is logged in the Application Log.<br>
Have anyone came accross this unlikely issue?</p>

<p><del><strong>CORRECTION</strong></del> <strong>UPDATE</strong></p>

<p>Thanks to the Robin's comment bellow to debug it with <code>{{ dump() }}</code> function I found out that <del>input</del> quantity field <del>with variable <code>title</code></del> <strong>is returning the error value</strong>. <del>However I didn't mentioned in my question that <strong>I'm actually posting/retrieving to/from matrix field</strong> as I didn't want to make the question pointlessly complicated with all those fields I've set.</del> </p>

<p><del>I've been misleaded/figured that to access field's variable in </del></p>

<p><code>{{ errorList(entry.getErrors('variable')) }}</code> for example, is not valid for matrix field. </p>

<p>Now I can't find any info on <strong>how to access matrix field for entry validation</strong> using: </p>

<p><code>{% macro errorList(errors) %}{% endmacro %}</code>.</p>

CC BY-SA 4.0
2018-10-12T21:33:41.267Z
What happens when you dump `entryError` and especially `entryError.getErrors()` is it your entry and does the array contain the errors? By the way your form is missing its type id
Robin Schambach
CC BY-SA 4.0
2018-10-13T09:20:36.357Z
@RobinSchambach Good catch about missing the `id`. Anyway, check the question update. You've got me on right track. Here is what I got for title: `array(2) { ["title"]=> array(1) { [0]=> string(22) "Title cannot be blank." }` and here is what I've got for matrix field accessed just by matrix field `handle` : `["event"]=> array(1) { [0]=> string(32) "Correct the errors listed above." } }`.
Dominik Krulak
CC BY-SA 4.0
2018-10-15T22:00:41.387Z
@DomK I think you can take the Guest Entries plugin out of the picture. It seems like "how do I show matrix validation errors on the front-end" is what you're looking for, correct? This sums it up pretty nicely, I think: https://craftcms.stackexchange.com/a/11005/57
Brad Bell
<guest-entries><craft2>
Matrix field validation on guest entry submission
# Matrix field validation on guest entry submission

(Asked by: username_0 on 2018-10-11)

First I illustrate the way I set up guest entry form.
Let's say I have channels blog product and comment cart.
Guest entry submits to the comment cart channel. Form lives in template which I use to show blog's product's entry.
Now I just send required field title which won't get passed by validation unless it isn't blank.
According to Guest Entries settings to show my blog's product's content along with my guest entry form I have to change the name of entryVariable key from entry to whatever not taken variable name let's say entryError in an event of validation error to not overwrite blog's product's variable entry.
In short this is how the template looks like when blog product entry load. This is just part of the form which is executed when first item is added to the cart matrix field in cart channel.
{# Product's title #}
{{ entry.title }}

{# Product's text #}
{{ entry.text }}


{# Show form error #}
{% macro errorList(errors) %}
    {% if errors %}
        
            {% for error in errors %}
                {{ error }}
            {% endfor %}
        
    {% endif %}
{% endmacro %}

{% from _self import errorList %}



    {{ getCsrfInput() }}
    
    

    


    {# Matrix field "cart" #}
    {# Block type field we want the product to save in. #}
    

    

    {# Quantity #}
    Quantity *
    
        --Please, select the quantity--
        {% for i in 1..10 %}
            {{ i }}
        {% endfor %}
        I need more
    

    {# Error message #}
    {% if entryError is defined %}
        
            {{ errorList(entry.getErrors('productQuantity')) }}
        
    {% endif %}



Form submits if title quantity is defined and if not I get entryError defined but don't get the title's quantity's  error shown.
The warning of title's quantity's error is logged in the Application Log.
Have anyone came accross this unlikely issue?
CORRECTION UPDATE
Thanks to the Robin's comment bellow to debug it with {{ dump() }} function I found out that input quantity field with variable title is returning the error value. However I didn't mentioned in my question that I'm actually posting/retrieving to/from matrix field as I didn't want to make the question pointlessly complicated with all those fields I've set. 
I've been misleaded/figured that to access field's variable in 
{{ errorList(entry.getErrors('variable')) }} for example, is not valid for matrix field. 
Now I can't find any info on how to access matrix field for entry validation using: 
{% macro errorList(errors) %}{% endmacro %}.

**Tags:** guest-entries, craft2
> **Comments:**
> **username_1**: What happens when you dump `entryError` and especially `entryError.getErrors()` is it your entry and does the array contain the errors? By the way your form is missing its type id  
> **username_0**: @RobinSchambach Good catch about missing the `id`. Anyway, check the question update. You've got me on right track. Here is what I got for title: `array(2) { ["title"]=> array(1) { [0]=> string(22) "Title cannot be blank." }` and here is what I've got for matrix field accessed just by matrix field `handle` : `["event"]=> array(1) { [0]=> string(32) "Correct the errors listed above." } }`.  
> **username_2**: @DomK I think you can take the Guest Entries plugin out of the picture. It seems like "how do I show matrix validation errors on the front-end" is what you're looking for, correct? This sums it up pretty nicely, I think: https://craftcms.stackexchange.com/a/11005/57  

---

## 1 answers

### Answer by username_0 on 2018-10-17. Score: 1

To grab validation error of a matrix field -> first block type -> field, you must access entry's matrix field with the function first() and than access block type field using getError("fieldHandle") function at the end of ElementCriteriaModel like so.
{{ entry.matrixField.first().getError("customFieldHandle") }}

or to the question related problem
{{ entryError.cart.first().getError("productQuantity") }}

Doing this way you don't have to loop through Matrix's blocks and it make sense if you need to access first block type only or you have only one block type.
To show validation error under each block.type of a custom field you'll have to loop in MatrixBlockModel of the cart channel like so.
{% if entryError is defined %}
    {% for block in entryError.cart %}
        {% if block.getErrors() %}
            {{ block.getError('productQuantity') }}
        {% endif %}
    {% endfor %}
{% endif %}

Like Brad pointed out to me in support ticket "you can decide how you want to display them".


---

