0

I'm using global fields on a multi language site to get the appropriate content depending on the locales used. Sof far so good.

However on a form page (sproutforms) these globals empty out after the form is submitted while an (input) error occurred.

How to overcome this issue?

                {# FORM #}
            <section class="col-sm-6 col-md-6" style="padding-bottom: 40px;">

            {# Prepare our error macro #}
            {% macro errorList(errors) %}
                {# To display errors, you want to make sure errors exist, and then you can loop through and display them #}
                {% if errors %}
                    {% for error in errors %}
                        {{ error }}</br>
                    {% endfor %}
                {% endif %}
            {% endmacro %}

            {% from _self import errorList %}


            {# Check if we should display or form or thank you message #}
            {% if craft.request.getParam('message') == ('thank-you' or 'bedankt-voor-uw-aanvraag') %}

                {# Check for the last entry submitted #}
                {% set lastEntry = craft.sproutForms.lastEntry() %}

                {% if lastEntry %}

                    {# If the last entry exists, we can output any of the variables that were present in the form submission. #}

                    <div class="alert fade in">
                        <i class="fa fa-cloud-download alert-icon"></i>
                        <button type="button" class="close" data-dismiss="alert">×</button>
                        <p>Thanks <strong>{{ lastEntry.fullName }}</strong>! We'll be in touch soon! Did you know you can display any of the values that were submitted in the form?  Here's a list of all the values in the last form submission:</p>
                    </div>


                {% endif %}

            {% else %}

                <form id="contactform" class="form-box register-form contact-form" method="post" accept-charset="utf-8">
                    {{ getCsrfInput() }}
                    <input type="hidden" name="action" value="sproutForms/entries/saveEntry">
                    <input type="hidden" name="handle" value="contact">
                    {% if craft.locale == 'en_us' %}
                    <input type="hidden" name="redirect" value="/en/contact?message=thank-you">
                    {% endif %}
                    {% if craft.locale == 'nl_nl' %}
                    <input type="hidden" name="redirect" value="/contact?message=bedankt-voor-uw-aanvraag">
                    {% endif %}

                    <h3 class="title">{{ formsAndButtons.contact }}</h3>
                    <div id="success"></div>

                    <label class="control-label" >{{ formsAndButtons.yourName }} <span class="required">*</span></label>
                    <input class="form-control"  type="text" id="textName" name="fields[fullName]" {%- if contact is defined %}value="{{ contact.fullName }}"{% endif -%}>
                    {% if contact is defined %}
                    <div class="error">
                        {{ errorList(contact.getErrors('fullName')) }}
                    </div>
                    {% endif %}

                    <label>{{ formsAndButtons.email }} <span class="required">*</span></label>
                    <input class="form-control" type="email" id="textEmail" name="fields[email]" {%- if contact is defined %}value="{{ contact.email }}"{% endif -%}>
                    {% if contact is defined %}
                    <div class="error">
                        {{ errorList(contact.getErrors('email')) }}
                    </div>
                    {% endif %}

                    <label>{{ formsAndButtons.telephone }} </label>
                    <input class="form-control"  type="text" id="textPhone" name="fields[phone]" {%- if contact is defined %}value="{{ contact.phone }}"{% endif -%}>
                    {% if contact is defined %}
                    <div class="error">
                        {{ errorList(contact.getErrors('phone')) }}
                    </div>
                    {% endif %}

                    <label>{{ formsAndButtons.comment }} <span class="required">*</span></label>
                    <textarea class="form-control" id="fields-message" name="fields[message]">{%- if contact is defined %}{{ contact.message }}{% endif -%}</textarea>
                    {% if contact is defined %}
                    <div class="error">
                        {{ errorList(contact.getErrors('message')) }}
                    </div>
                    {% endif %}

                    <div class="clearfix"></div>

                    <div class="buttons-box clearfix">
                        <input class="btn btn-default" type="submit" value="{{ login.submit }}">
                        <span class="required"><b>*</b> {{ login.requiredField }}</span>
                    </div>
                </form>

            {% endif %}
            </section>
1
  • Sounds like a Sprout Forms bug to me, no? Have you tried contacting them?
    – Brad Bell
    Commented Jan 8, 2015 at 19:04

1 Answer 1

2

TL;DR
This issue has been resolved and will be available in the next release of Sprout Forms (0.8.4)

The issue was caused by our use of context switching for custom content tables in combination with renderObjectTemplate(), the latter of which initializes the twig environment and loads globals if not already loaded. This initialization is context sensitive so since we needed to switch context and then use renderObjectTemplate() before we could switch the context back, globals would inherit the content model from the form entry itself, not their actual content.

We fixed this issue by initializing the environment before any context switching is done.

Selvin Ortiz
Barrel Strength Design

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.