Be sure to checkout our newest add-on Speedy!

EE compatibility updates:

  • Publisher is EE5 compatible, but it does not currently support the Fluid field.

ExpressionEngine.com licenses:

  • If you purchased an add-on from expressionengine.com, be sure to visit boldminded.com/claim to add the license to your account here on boldminded.com.

Ticket: Custom Redirect via POST Param

Status Client Wait
Add-on / Version Custom System Messages 3.0.5
Severity
EE Version 5.3.2

Nicolas Burtnyk

May 05, 2020

Hi Brian,

got another question regarding the CSM addon:

is it possible to redirect to a custom URL after an error, e.g. the URL defined in one of the POST params like return=”” ?

Background:

We have multiple login forms on the site. They are all at different forms, all at different URLs, all use the exp:member:login_form, and all of these need to redirect to their respective pages when an error occurs.

So in the CSM addon, I define one custom action “(18) Member Login (On Error)” but then it asks me which template to redirect to, while I need it to use the POST param from the form that contains the particular redirect URL for a given login form submission…

Is the above possible to do via CSM ? If not, what would you suggest ?


Thanks.
- Mike Nagel
310.746.8131

#1

BoldMinded (Brian)

May 06, 2020

Hi, Mike. I’ve been thinking about this a bit, and unfortunately I don’t think CSM alone will do what you need it to do. You can try an add-on like Mo’ Variables which gives easy access to POST variables in the template, or you can just enable PHP in the error template and check the POST array to handle your redirect.

https://github.com/rsanchez/mo_variables

#2

Nicolas Burtnyk

May 14, 2020

Hi Brian,

we do have Mo’ Variables. Could you elaborate on how we’d use it in conjunction w/ CSM to redirect to the correct form page to then show the form error ?

Thanks.
Mike Nagel

#3

BoldMinded (Brian)

May 14, 2020

Something like

{if action == 10 && post:foo == "bar"} do something, display content, {redirect="whatever"} {/if}

https://docs.boldminded.com/custom-system-messages/docs/variables

#4

Nicolas Burtnyk

6 days ago

Hi Brian,

tried a few things, but can’t get this working, also no fully understanding which template needs the redirect hook.

Here’s a simplified example:
We have two login forms. If an error occurs in either, we need to redirect to the page of the form that was submitted, outputting errors via CSM.
I have CSM configured to render errors via a dedicated template /messages/error, but have removed all Custom Actions as they can always only direct to one template.

So in which template do I need to implement the redirect ?

I’ve tried it at the bottom of the /messages/error template (I used PHP for the redirect), but when it redirected to the correct form the form errors were not displayed via CSM…

Thanks.
Mike

#5

BoldMinded (Brian)

4 days ago

Can you do me a favor and share a screenshot of your CSM settings page?

#6

BoldMinded (Brian)

4 days ago

I tested this locally and the following settings and template should do what you want it to do. A login form that shows the errors above the form, or redirects to a different url if there is an error.

This is the account.group/index.html file:

{if csm:error}
    {!-- Redirect ... but if you do this the error messages will be lost b/c it's a full http request. --}
    {redirect="pages/index"}
    
    {!-- Or show the error above the login form --}
    <div class="form-error-section">
        <div class="error-message">
            <strong>The following errors were encountered:</strong>
            <ul class="error-fields">
                {csm:content}
            </ul>
        </div>
    </div>
{/if}

{exp:member:login_form return="account/index" action="account/index"}
    <div>
        <label for="signin-email">Email Addresss <span class="input-desc">(Username)</span><span class="input-required"> *</span></label>
        <input type="text" id="signin-email" name="username" />
    </div>
    <div>
        <label for="signin-password">Password <span class="input-required"> *</span></label>
        <input type="password" id="signin-password" name="password" />
        <button type="submit" name="submit" value="Sign In">Sign In</button>
    </div>
{/exp:member:login_form}
#7

BoldMinded (Brian)

4 days ago

The messages/error template in the default settings, which are not used in this case b/c of the overriding custom action setting.

<div class="form-error-section">
    <div class="error-message">
        <strong>The following errors were encountered:</strong>
        <ul class="error-fields">
            {csm:content}
        </ul>
    </div>
</div>
#8

BoldMinded (Brian)

4 days ago

Note that if you use an actual {redirect=”“} tag, or a php header(‘Location:’) function call to redirect, then the error messages will be lost b/c you’re doing a full http request. It only works when the “Redirect to defined error template” option is enabled.

#9

BoldMinded (Brian)

4 days ago

If you’re trying to do anything beyond the examples that I posted then I can’t really help b/c I don’t know what exactly you’re trying to do or it is beyond the functionality of CSM and can’t be supported.

#10

BoldMinded (Brian)

4 days ago

I updated the docs page so hopefully it is a little more clear. I hadn’t noticed that over time my example screenshot of the settings didn’t align anymore with the example code snippets. Hopefully now it is more clear. https://docs.boldminded.com/custom-system-messages/docs/template-tags

#11

Nicolas Burtnyk

4 days ago

yes, our current problem is that login form errors are lost when we redirect.

We have multiple login forms and CSM only allows one fixed target for all login form errors, although we need a dynamic redirect target based on which login form the user came from. This would be easily doable if CSM allowed to either redirected to the original page where the request came from or simply used (if present) a form POST var which can be set easily in the form, such as the return=”/somewhere” parameter.

Problem is as of right now all custom CSM actions only allow one fixed target.

#12

BoldMinded (Brian)

7 hours ago

Add this to line 358 of ext.system_messages.php

if (isset(ee()->TMPL->tagparams['csm_return'])) {
                $template = ee()->TMPL->tagparams['csm_return'];
            } else {
                $template = $template[1] == 'index' ? $template[0] : $template[0].'/'.$template[1];
            }

Should look like this:

Then add the param to your tag and see if it works. This might let you set 2 different return urls for each of your forms.

{exp:member:login_form return="account/index" action="account/index" csm_return="account/index/foo"}

Login to reply

Contact

For add-on support, please use the Support section. General inquries and pre-sale questions can be sent to support@boldminded.com.