1

I have a multi-pages form which is submitting using Sprig plugin. The action is targeting a controller in a custom module. Each step of the form is loading using the current step stored in session.

The end of this controller is returning the URL to redirect: return $this->redirect($entry->url);

However, the redirect does not happen, and the form is reloaded instead of the doing the redirect.

I tried to edit the end of the controller to this: return $this->asSuccess(true); Then, catch the success on front-end, before to redirect from the template using {% redirect %} or {% do sprig.redirect(entry.url) %}. But as I have a multi-pages form, the success is returned at every step, so the check doesn't work on final submit.

I also tried to add the classic {{ redirectInput(entry.url) }} on the last page of the form, but the direct also does not happen, and redirect to the same page.

How to make the redirect works after submitting the last step of this form (supposing there's no validation error)?

2 Answers 2

1

The easiest way to do this would be to use the sprig.redirect(url) function.

{% if success %}
    {% do sprig.redirect('/new/page') %}
{% else %}
    ...
{% endif %}

Alternatively, you can follow the pattern used in the redirect() method, placed inside your custom module controller, to achieve this.

Craft::$app->getResponse()->getHeaders()->set('HX-Redirect', $url);
2

I am wondering if the best way to do this is to set a variable (lastStep) to true for example on last "continue click" and then check for that value and submit success. That way you avoid redirecting after after every "continue click" in your multipage form. I am not sure how you are doing the multipage form. If you are conditionally showing fields based on the step you are on then a hidden input like this would work:

<input type="hidden" name="lastStep" value="true">

Then have this in the top of your component:

{% set lastStep = lastStep ?? false %}

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.