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 Resolved
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

May 21, 2020

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)

May 22, 2020

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

#6

BoldMinded (Brian)

May 22, 2020

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)

May 22, 2020

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)

May 22, 2020

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)

May 22, 2020

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)

May 22, 2020

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

May 22, 2020

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)

May 26, 2020

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"}
#13

Nicolas Burtnyk

May 27, 2020

Hi Brian,

I tried the above and it did not work. The

csm_return
param (that I’ve set in the form definition, as per your example) is not part of
ee()->TMPL->tagparams

, hence the first condition does not kick in.

Playing around with this, I substituted

ee()->TMPL->tagparams['csm_return']
w/
$_POST['RET']
, and that works as intended if the return=”” param is defined in the
exp:member:login_form

definition.

Lmk if there is any concern on your end w/ using

$_POST['RET']

here.

Also, please keep me updated if you ever officially implement this in the addon, otherwise we’ll have to redo the mod for every new CSM version.

Thanks !
Mike

#14

BoldMinded (Brian)

May 27, 2020

I tested my code change yesterday, it worked as I expected it to and allowed 2 different forms to redirect to different urls and display the error message above the form. Are you positive you tested it correctly?

Using RET is risky b/c it changes the behavior of that code, which has been like that for almost 10 years. I don’t want to break people’s sites by adding that functionality, which is why I added the csm_return parameter option. I probably won’t be making any changes that refer to the RET parameter. If that works for you though then you’re welcome to use it.

#15

Nicolas Burtnyk

May 27, 2020

here’s what I did:

our form already had a return param, so I added the action and csm_return param as per your example.

in ext.system_messages.php @ line 358, when I print_r(ee()->TMPL->tagparams), I get this output

Array ( [form_id] => login_form [return] => /dashboard [class] => alert alert-error )

As you can see, the csm_return key is not set, hence the if condition in your code never kicks in…

What else could it be ?

#16

BoldMinded (Brian)

Jun 02, 2020

It’s hard to tell b/c I don’t know what else is going on with your site, but I step debugged it locally and csm_return was definitely in the tagparams array when it needed to be.

#17

BoldMinded (Brian)

Jun 12, 2020

Nicolas, I’m going to close this ticket out. As mentioned in my previous comment the csm_return param worked for me in what I understand to be the same scenario that you’re trying to make work. I don’t know if you have an additional redirect happening somewhere that is removing the csm_return parameter or what. If you edited the ticket and provided CP and FTP access, with detailed steps to replicate the issue, I can take another look at it, but right now I don’t see any further action I can take.

Login to reply

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