1

I'm having trouble with additional message fields in my Craft Contact Form using Craft 3 and the Pixel & Tonic Contact Form plugin.

I've added two additional fields by splitting up the message field as described in the plugin documentation. If all the fields are filled in, everything goes smoothly. But if the form is submitted with any blank required fields, I get a PHP error "Array to string conversion." Live example at http://striveforfive.creativeforthepeople.org/contact.

EDIT: The code is:

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

    {% from _self import errorList %}

    <form class="contact-form" method="post" action="" accept-charset="UTF-8">
        <div class="form-group">
            {{ csrfInput() }}
            <input type="hidden" class="form-control" name="action" value="contact-form/send">
            {{ redirectInput('contact-ty') }}

            <input id="from-name" class="form-control" type="text" name="fromName" value="{{ message.fromName ?? '' }}" placeholder="your name">
            {{ message is defined and message ? errorList(message.getErrors('fromName')) }}

            <input id="from-email" class="form-control" type="email" name="fromEmail" value="{{ message.fromEmail ?? '' }}" placeholder="your email">
            {{ message is defined and message ? errorList(message.getErrors('fromEmail')) }}

            <input id="affiliation" class="form-control" type="text" name="message[Affiliation]" value="" placeholder="your school / organization / home locations">

            <select id="position" class="form-control" type="text" name="message[Position]" value="">
                <option value="" selected disabled>your position</option>
                <option>Educator / Care Giver</option>
                <option>Family Child Care Provider</option>
                <option>Center Director</option>
                <option>Student Teacher</option>
                <option>Other</option>
            </select>

            <textarea rows="5" cols="40" id="message" class="form-control" name="message[body]" placeholder="your message">{{ message.message ?? '' }}</textarea>
            {{ message is defined and message ? errorList(message.getErrors('message')) }}

            <input id="bluebeard" name="bluebeard" type="text">

            <input id="contact-submit" type="submit" class="btn btn-outline-primary" value="Send">
        </div>
    </form>

All help is appreciated!

1
  • Can you share the contact form code you're using on the backend?
    – Jalen Davenport
    Commented Apr 11, 2018 at 13:52

1 Answer 1

1

I think this:

{% for error in errors %}
    <li>{{ error }}</li>
{% endfor %}

... is where the error originates.

I memory serves me right, errors is an array like this:

[
    ['fieldHandle' => ['Something went terribly wrong!', 'Really, two errors in ONE FIELD?']],
    ['orderFieldHandle' => 'I AM REQUIRED, y\'all!'],
];

In which case {{ error }} would dictate Twig (and therefore PHP) to echo an array (['fieldHandle' => ['Something went terribly wrong!', 'Really, two errors in ONE FIELD?']]), resulting in the error you mentioned.

You can fix this, by using the following code:

{% for fieldHandle, fieldErrors in errors %}
    <li>{{ fieldHandle }}: {{ fieldErrors |join(', ') }}</li>
{% endfor %}

My original answer was not what caused the error (so, apparently, errors was not what I thought it was).

This type of error is caused by an array being echoed by PHP, and not a string. In this case, the cause of the problem is {{ message.message ?? '' }}, which turned into an array because it contains multiple field values.

7
  • Not exactly... One field can have several different errors thus ['fieldHandle' => ['Something went terribly wrong!' , 'an something else too']] Commented Apr 12, 2018 at 13:27
  • Will edit that in, but it's still the reason for the error ;)
    – Paul
    Commented Apr 12, 2018 at 13:28
  • I never said something else :P Commented Apr 12, 2018 at 13:28
  • Thanks so much! Forgive my limited understanding of the issue, but could you suggest a way to fix the code?
    – Clay Kilby
    Commented Apr 13, 2018 at 16:01
  • Added a suggestion to solve the error :)
    – Paul
    Commented Apr 13, 2018 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.