Declarative user profile and boolean user attributes

Hi,

my custom theme used with Keycloak 21.1.1 contains a checkbox on the registration form and the account console, allowing users to (un-)subscribe to a newsletter.
The property value is submitted as 0 or 1 with the form, depending on the checkbox state and stored in a user attribute.

Keycloak 25 would allow to model this property using the declarative user profile and getting rid of the custom templates for those forms (especially as adapting the new account console is more complicated).

After looking through the documentation, my best shot was using yes/no select-radiobuttons (see snippet from User Profile → JSON editor below).

Is there a way to create a simple checkbox for a boolean attribute?

  {
      "name": "newsletter",
      "displayName": "I want to receive the newsletter",
      "validations": {
        "options": {
          "options": [
            "0",
            "1"
          ]
        }
      },
      "annotations": {
        "inputOptionLabels": {
          "0": "No",
          "1": "Yes"
        },
        "inputType": "select-radiobuttons"
      },
      "required": {
        "roles": [
          "admin",
          "user"
        ]
      },
      "permissions": {
        "view": [
          "admin",
          "user"
        ],
        "edit": [
          "admin",
          "user"
        ]
      },
      "multivalued": false
    }
  ],

hi @mashedpotatoes

from what I understand is that you want to add checkbox in the theme and the result of that checkbox will be saved in the user attribute section? is that it?

Hi @lamoboos223,

Exactly.

I am one step further already: this snippet will generate the checkbox as desired, but the unchecked state would never be stored as “newsletter=0” (this is what the currently stored data would look like), so I am missing a way to associate this value with the unticked checkbox.

    {
      "name": "newsletter2",
      "displayName": "Subscribe to newsletter",
      "validations": {
        "options": {
          "options": [
            "1"
          ]
        }
      },
      "annotations": {
        "inputOptionLabels": {
          "1": "Yes, I want to receive the newsletter"
        },
        "inputType": "multiselect-checkboxes"
      },
      "permissions": {
        "view": [],
        "edit": [
          "admin",
          "user"
        ]
      },
      "multivalued": false
    }

AFAIK it’s not possible to use checkbox for two values (0/1 or true/false). As you already experienced, a checkbox only stores the value, if it is checked. If not, no value is stored.
For a true/false, on/off or yes/no or similar field, you’ll have to use a select box or, as already mentioned by you, the select-radiobuttons.

why not create some server side code (custom extension) to set the radio button value if it is null to 0 ?

@mashedpotatoes

Thank you both for the input!
We will figure out which of the three options fits us best.

@lamoboos223 I tried adding the “0” value silently in my UserStorageProvider (specifically the implementation of AbstractUserAdapterFederatedStorage::getAttributes) but the registration immediately redirects to a “Update Account Information” page, stating that there is an “Invalid value” on the checkbox.
Similar things happen with existing accounts with that value. So when picking the “single checkbox option”, “0” values should obviously be deleted from the database then.