1

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 %}
        <ul class="errors">
            {% for error in errors %}
                <li>{{ error }}</li>
            {% endfor %}
        </ul>
    {% endif %}
{% endmacro %}

{% from _self import errorList %}


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

    <input type="hidden" name="title" value="Some dynamic title">


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

    <input type="hidden" name="fields[cart][new1][fields][productTitle]" value="{{ entry.title }}">

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

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

</form>

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 %}.

3
  • 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 Oct 12, 2018 at 21:33
  • @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." } }. Oct 13, 2018 at 9:20
  • 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: craftcms.stackexchange.com/a/11005/57
    – Brad Bell
    Oct 15, 2018 at 22:00

1 Answer 1

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".

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.