`return_to` on registration form also redirect when registration fails

Hi there,

I’ve set the return_to of my registration form to a custom page but I get redirected there even if the form submission fail (email address already taken, etc…).

That’s how I’ve set it: {%- form 'create_customer', return_to: '/collections/all' -%}

How should I manage this?

Hey @baggio_giacomo, I assume you are following the documentation here regarding that redirect. One thought from me is does the form include a check for the form_errors? If not then maybe that is why it is ignoring them.

Another idea here as a potential workaround, is to check for the customer presence on page load and if not there, use JS to set a storage value then check for that value when the presence is finally there and if the storage value is found then redirect immediately accordingly.

I mean of course the form approach should just work as expected but in the meantime that workaround should have similar functionality to what you were wanting.

Hi @RobDukarski

Yes, I’m just printing the errors if form.errors is present:

{%- form 'create_customer', return_to: '/collections/all' -%}
    {%- if form.errors -%}
      <ul id="amplius-registration-errors">
        {%- for field in form.errors -%}
          <li>
            {%- if field == 'form' -%}
              <p>{{ form.errors.messages[field] }}</p>
            {%- else -%}
              <p>
                {{ form.errors.translated_fields[field] | capitalize }}
                {{ form.errors.messages[field] }}
              </p>
            {%- endif -%}
          </li>
        {%- endfor -%}
      </ul>
    {%- endif -%}
  ...

{%- endform -%}

Thanks for the workaround, I’ll try to implement it to satisfy my needs!

Hey @baggio_giacomo, figured I would drop in a quick example for you.

{% if request.page_type == 'customers/register' %}
  {% unless customer %}
    <script>
      if (!localStorage.getItem('hasRegistered')) {
        localStorage.setItem('hasRegistered', false);
      }
    </script>
  {% endunless %}
{% else %}
  {% if customer %}
    <script>
      if (localStorage.getItem('hasRegistered')) {
        localStorage.removeItem('hasRegistered');
        location.href = '{{ routes.all_products_collection_url }}';
      }
    </script>
  {% endif %}
{% endif %}

You could take that a different route and use cart attributes instead of storing the data in the web browser, keeping the condition checking only via Liquid, but if you did and the customer placed an order without logging in then that data would be part of the order.

Hi @RobDukarski, thanks for the example!

To give you more context:

I have an app that lets the merchant collect additional data by creating custom registration and account form.

On the registration form builder, there is the possibility to set the redirect path.
The registration form is added to the theme through theme app extension and it renders this piece of code

{%- form 'create_customer' -%}
    {% if registration_form.redirect_path %}
      <input type="hidden" name="return_to" value="{{ registration_form.redirect_path }}">
    {% endif %}

  ...

{%- endform -%}

I’ll keep this topic without a solution since the real problem here is that the return_to redirects even if the form submission fail. I will let you know if your workaround works correctly!