Checkbox input custom user attributes broken between 22.0.3 and 23.0.5

Hey folks,

I have a custom register.ftl file which defines checkboxes for accepting terms and marketing emails. The code below worked in Keycloak version 22.0.3, but no longer works after updating to 23.0.5. By “works” I mean that the marketingAccepted and/or termsAccepted attribute keys are associated with the user’s account when they register with one or both checkboxes checked. The actual value of the attributes is empty, but the fact that the key exists indicates that the user checked the box. If neither box is checked, neither attribute key is added.

In version 23.0.5, the attribute keys are never added to the user’s account even if the checkboxes are checked. I cannot find what changed between versions to cause this problem.

If anyone has any insight here, it would be greatly appreciated! If anyone has added terms/marketing email acceptance using some other method, I’d love to hear how you did that too.

<div class="${properties.kcFormGroupClass!}">
    <div class="${properties.kcInputWrapperClass!}">
        <label>
            <input 
                type="checkbox"
                id="user.attributes.marketingAccepted" 
                name="user.attributes.marketingAccepted" 
                value="${(register.formData['user.attributes.marketingAccepted']!'')}"
                checked/>
            Send me occasional emails about software updates and news (you can unsubscribe at any time)
        </label>
    </div>
    <div class="${properties.kcInputWrapperClass!}">
        <label>
            <input 
                type="checkbox"
                required
                id="user.attributes.termsAccepted" 
                name="user.attributes.termsAccepted" 
                value="${(register.formData['user.attributes.termsAccepted']!'')}"/>
            I accept the <a href="https://www.mydomain.com/terms-of-use" target="_blank">Terms</a> and the <a href="https://www.mydomain.com/privacy-policy" target="_blank">Privacy Policy</a>
        </label>
    </div>
</div>

Thanks for any help you can provide,

Peter

Update on this, I found out that setting a value of “true” on the checkbox input gets this working again. Then the marketingAccepted and/or termsAccepted attribute keys show up in the user’s profile with values of “true” when the boxes are checked, and do not show up at all when the boxes are unchecked. This fixes my issue, since I can tell if the users checked the boxes or not.

<!-- Terms Acceptance and Marketing checkboxes -->
<div class="${properties.kcFormGroupClass!}">
    <div class="${properties.kcInputWrapperClass!}">
        <label>
            <input 
                type="checkbox"
                id="user.attributes.marketingAccepted" 
                name="user.attributes.marketingAccepted" 
                value="true"
                checked/>
            Send me occasional emails about software updates and news (you can unsubscribe at any time)
        </label>
    </div>
    <div class="${properties.kcInputWrapperClass!}">
        <label>
            <input 
                type="checkbox"
                required
                id="user.attributes.termsAccepted" 
                name="user.attributes.termsAccepted" 
                value="true"/>
            I accept the <a href="https://www.mydomain.com/terms-of-use" target="_blank">Terms</a> and the <a href="https://www.mydomain.com/privacy-policy" target="_blank">Privacy Policy</a>
        </label>
    </div>
</div>

Perhaps you want to update your Keycloak to a most recent version (currently, as of writing this it’s 26.1.1) and use the declarative User Profile option: https://www.keycloak.org/docs/latest/server_admin/index.html#user-profile
Additionally, a more recent version has more security fixes available than a one year old version which is out of support…

See also here:

1 Like

Thank you for sharing this Niko! This video is a very helpful introduction to the new declarative User Profile option. I am hoping to update Keycloak past version 23 soon. Keeping up with Keycloak updates can be challenging for us, since we must evaluate each version to ensure that any breaking changes are remedied before applying the update. We have many web services which use Keycloak, and sometimes small changes to Keycloak can affect the way these services function.

I selfishly wish that Keycloak versions were supported with non-breaking security fixes for more than a few months at a time. But I understand that supporting older versions for a longer period of time can be a drain on developer resources. And I do appreciate all the new features the developers have been adding into Keycloak!

Well, another reason to jump directly to 26:

1 Like

I’m glad to hear that Keycloak is moving in this direction! I had not seen this article.

1 Like