So I have a contact form in my layout (just using the contact form plugin from the Craft guys) and on 404 pages I'm getting the following:

Impossible to invoke a method ("getErrors") on a string variable ("") in "_layout"

What's the best way around that and to still get the contact form to show up on an 404 page?

Many thanks in advance!

Template code is in the footer of _layout.html and looks like this:

<!-- unrelated stuff here... -->

<!-- CONTACT -->

    {% set asset = globalContact.contactBackground.first() %}
    <section id="contact" class="strip contact" style="background-image: url({{ asset.url }});" data-stellar-background-ratio="0.2" data-stellar-responsive="true" data-stellar-horizontal-offset="0">
        <div class="overlay"></div>
        <div class="heading">
                <span>{{ globalContact.contactPreHeader }}</span><br />
                {{ globalContact.contactHeader }}
        <div class="inner">
            {% macro errorList(errors) %}
                {% if errors %}
                    <ul class="errors">
                        {% for error in errors %}
                            <li>{{ error }}</li>
                        {% endfor %}
                {% endif %}
            {% endmacro %}

            {% from _self import errorList %}
            <form method="post" action="" class="contact-form" accept-charset="UTF-8">
                {{ getCsrfInput() }}
                <input type="hidden" name="action" value="contactForm/sendMessage">
                <input type="hidden" name="redirect" value="thanks">

                <div class="contact-name">
                    <input type="text" id="fromName" name="fromName" placeholder="Name" />
                    {% if message is defined %}{{ errorList(message.getErrors('fromName')) }}{% endif %}
                <div class="contact-email">
                    <input type="text" id="fromEmail" name="fromEmail" placeholder="Email" />
                    {% if message is defined %}{{ errorList(message.getErrors('fromEmail')) }}{% endif %}
                <div class="contact-phone">
                    <input type="text" id="phone" name="message[Phone]" placeholder="Phone" />
                <div class="contact-message">
                    <textarea rows="8" cols="40" id="message" name="message[body]" placeholder="Message">{% if message is defined %}{{ message.message }}{% endif %}</textarea>
                    {% if message is defined %}{{ errorList(message.getErrors('message')) }}{% endif %}
                <input id="shewbotsshew" name="shewbotsshew" type="text">
                <div class="contact-submit">
                    <input type="submit" value="Submit" />

<!-- unrelated stuff here... -->
  • I'm guessing you've got the contact form code in a footer or header include on your site? Can you update the question with the template code?
    – Brad Bell
    Oct 13 '14 at 16:27
  • Done - hope that helps. Oct 13 '14 at 16:36

Anywhere you've currently got something that looks like this:

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

Try changing that to:

{{ message is defined and message ? errorList(message.getErrors('fromEmail')) }}

And you'll want to do that for the other attributes, too ('fromName', etc.).


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.