Skip to content

Fix UserPreference default value from null to false - #631

Open
najbo wants to merge 2 commits into
rainlab:masterfrom
najbo:fix/user-preference-default-false
Open

najbo wants to merge 2 commits into
rainlab:masterfrom
najbo:fix/user-preference-default-false

Conversation

@najbo

@najbo najbo commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Problem

When no preference is stored for a user and a given key, getPreference() returned null as the default. This caused an inconsistency with checkbox form inputs:

  • Expected behavior: Absence of a record means false (unchecked, opt-out by default)
  • Actual behavior: Absence of a record sometimes means true by default in templates, but returns null from code

Additionally, the form field value and the database value were inverted:

  • When checkbox was checked, it stored false
  • When checkbox was unchecked, it deleted the preference entirely
  • Reloading the page showed inverted checkbox state

Solution

Changed the default return value in getPreference() from null to false. This ensures consistent behavior where:

  • No record = false (opt-out by default)
  • Record with true = true (explicitly opted-in)
  • Record with false = false (explicitly opted-out)

Breaking Change Warning ⚠️

This is a breaking change for any code that:

  • Calls getPreference() without an explicit default parameter
  • Expects null to be returned when no preference is stored
  • May have been written to work around the previous inverted behavior

Recommendation: Review all code calling getPreference() and explicitly pass a default value if the previous behavior was intentional. This is especially important for applications with existing user preference data.

Related

This fix works in conjunction with the fix to rainlab/vanilla-theme for the promotional emails checkbox.


🤖 Generated with Claude Code

When no preference is stored for a user, getPreference now returns false
instead of null. This fixes an issue where user preferences would have
inverted values displayed in checkboxes.

BREAKING CHANGE: Code that relied on the previous default value of null
may need adjustment. Users should audit their code that calls getPreference
without an explicit default parameter.
Adjust assertions to expect false instead of null when no preference is stored.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant