Resolve Macros in HTML Form Template

Matt Fothergill asked on September 5, 2019 11:03

I have a custom/alternative form layout that displays the password policy violation message which itself contains 2 nested macros.

CMS -> Settings -> Security & Membership -> Password -> Policy violation message:

<p>Example text {% Settings.CMSPolicyMinimalLength %}.</p>
<p>More text {% Settings.CustomSettingName %}.</p>

Form layout snippet:

<div class="password">
    <div class="helper-panel">
        {% Settings.GetValue(&quot;CMSPolicyViolationMessage&quot;, &quot;&quot;) #%}
    </div>
    $$label:UserPassword$$ $$input:UserPassword$$ $$validation:UserPassword$$
</div>

So, the first macro resolves fine, however $$validation:UserPassword$$ doesn't resolve the nested macros within the password policy violation message and so ends up just displaying the unresolved message.

I have tried the following but all just display the text e.g. '{%', etc around the message.

{% $$validation:UserPassword$$ %}
{% $$validation:UserPassword$$ #%}
{% ResolveMacros($$validation:UserPassword$$) %}

What is the correct syntax to resolve a form field validation message in a HTML form layout?

Correct Answer

Matt Fothergill answered on September 27, 2019 09:43

I’ve managed to track the issue to a Kentico Form Control called 'PasswordConfirmator'. The issue is with the code behind this control as it doesn’t resolve macros after retrieving the the CMSPolicyViolationMessage using AuthenticationHelper.GetPolicyViolationMessage(SiteContext.CurrentSiteName).

I have fixed the issue by creating a custom form control based on the built in 'PasswordConfirmator' and updating the code behind to call CMS.MacroEngine.MacroContext.CurrentResolver.ResolveMacros passing in the string returned from AuthenticationHelper.GetPolicyViolationMessage(SiteContext.CurrentSiteName).

0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on September 5, 2019 14:44

Have you tried simply using

{% Settings.CMSPolicyViolationMessage @%}

in your form layout vs. using the .GetValue() method?

FYI, wrapping the form field labels and values around macro syntax will cause things to break. So leave the form field labels and values without the leading and trailing {% %}.

0 votesVote for this answer Mark as a Correct answer

Matt Fothergill answered on September 5, 2019 15:26 (last edited on December 10, 2019 02:31)

Using {% Settings.CMSPolicyViolationMessage @%} works instead of {% Settings.GetValue(&quot;CMSPolicyViolationMessage&quot;, &quot;&quot;) |(identity)GlobalAdministrator%}.

However, the issue is that the $$validation:UserPassword$$ value contains macros and these aren't being resolved.

So if the user leaves the password field blank, the $$validation:UserPassword$$ returns a basic string message set in the validation field for the form property e.g. "Please enter a password.", but if the user enters a password that doesn't meet the password policy $$validation:UserPassword$$ returns the Settings.CMSPolicyViolationMessage message but doesn't resolve the nested macros.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on September 5, 2019 15:29

For the password field, how did you enter your validation error message? This is typically done right on the field in the validation area. You need to click the small black > to enter the macro properly otherwise it won't be resolved.

0 votesVote for this answer Mark as a Correct answer

Matt Fothergill answered on September 6, 2019 10:18 (last edited on December 10, 2019 02:31)

The validation message is set on the alternative form field in the validation message box using the small black arrow to enter the macro {% Settings.CMSPolicyViolationMessage |(identity)GlobalAdministrator%}.

I can't work out where or why the message is being loaded differently depending on whether the password fields are blank or populated with password that doesn't meet the policy requirements.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.