2

I have this form (am forms plugin):

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

{% from _self import errorList %}

{% set form = craft.amForms.getForm('contactForm') %}

<form method="post" action="" accept-charset="UTF-8">

  {{ getCsrfInput() }}
  <input type="hidden" name="action" value="amForms/submissions/saveSubmission">
  <input type="hidden" name="handle" value="{{ form.handle }}">



   <label for="frm_message">{{ 'Nachricht' | t }}*</label>
   <textarea id="frm_message" name="fields[message]" value="{% if formHandle.message is defined %}{{ formHandle.message }}{% endif %}"></textarea>
    {% if form is defined %}
        {{ errorList(form.getErrors('message')) }}
    {% endif %}


  <input type="submit" value="Submit">

</form>

This is not displaying any errors if the field is empty. What do I miss? I made the field required in the CP but the macro is not showing when hitting "submit" with the field empty.

1

Ahh this is a bit misleading because the docs don't really spell it out. "formHandle" according to the docs really feels like it might be the variable you set your form to, but it's literally the form handle you got when you titled your form in the CMS.

So if your form starts with:

{% set apple = craft.amForms.getForm('fireTruck') %}

your error code needs to be this:

{% if fireTruck is defined %}
    {{ errorList(fireTruck.getErrors('someField')) }}
{% endif %}

and not this:

{% if apple is defined %}
    {{ errorList(apple.getErrors('someField')) }}
{% endif %}
0

I had the same issue this morning, after trial and error I got it working with the following code

{% set form = craft.amForms.getForm('contactForm') %}
{% from _self import errorList %}

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

{% if craft.request.getParam('submitted') == form.handle %}
    <p class="submit-notification">{{ form.afterSubmitText|default('Thanks for your submission.'|t) }}</p>

{% else %}
    {%- set submitValue = (form.submitButton ? form.submitButton : 'Submit'|t) -%}

    <form class="contact-form" method="post" action="#form" accept-charset="UTF-8">
        {{ getCsrfInput() }}

        {# This should always be here! #}
        <input type="hidden" name="action" value="amForms/submissions/saveSubmission">

        {# Insert your form's handle. #}
        <input type="hidden" name="handle" value="{{ form.handle }}">

        {# Optional: Anti-spam protection. #}
        {{ craft.amForms.displayAntispam() }}


        {# Place the HTML of your fields here #}

        <input type="text" id="frm_fullName" name="fields[fullName]" placeholder="Uw naam" value="{% if formHandle.fullName is defined %}{{ formHandle.fullName }}{% endif %}">
        {% if contactForm is defined %}
            {{ errorList(contactForm.getErrors('fullName')) }}
        {% endif %}

        <input type="text" id="frm_telephoneNumber" name="fields[telephoneNumber]" placeholder="Telefoonnummer" value="{% if formHandle.telephoneNumber is defined %}{{ formHandle.telephoneNumber }}{% endif %}">
        {% if contactForm is defined %}
            {{ errorList(contactForm.getErrors('telephoneNumber')) }}
        {% endif %}

        <textarea id="form_comment" name="fields[comment]" value="{% if formHandle.comment is defined %}{{ formHandle.comment }}{% endif %}" placeholder="Uw vraag" rows="4"></textarea>
        {% if contactForm is defined %}
            {{ errorList(contactForm.getErrors('comment')) }}
        {% endif %}

        <div class="submit-button">
            <input type="submit" value="{{ submitValue }}">
        </div>

    </form>

{% endif %}

Try to change your markup from

{% if form is defined %}
    {{ errorList(form.getErrors('message')) }}
{% endif %}

To

{% if contactForm is defined %}
    {{ errorList(form.getErrors('message')) }}
{% endif %}

Hopefully this helps

1
  • I tried this as well. But now the errors show already on page load not on submit. – KSPR Jul 18 '17 at 9:06

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.