luci-base: override the load() function to return boolean for FlagValue
authorPaul Donald <[email protected]>
Fri, 25 Apr 2025 15:17:51 +0000 (17:17 +0200)
committerPaul Donald <[email protected]>
Fri, 25 Apr 2025 15:18:48 +0000 (17:18 +0200)
In the underlying uci system, all variables are effectively strings, so
for those configuration values which serve as 'boolean' flags, we need
to coerce the various forms into a real boolean. Only the following
string values result in true: 1|on|true|yes|enabled. Otherwise, false.

Checkboxes now fill correctly.

"Unchanged" configurations may write changed values as the Flag values
are coerced to '1' or '0' on write, but the configuration behaviour
remains synonymous.

Signed-off-by: Paul Donald <[email protected]>
(cherry picked from commit 1355a6fa253d765140f5571354939d2e93e6d967)

modules/luci-base/htdocs/luci-static/resources/form.js

index e1fc3ff3e9dd39520ac3d1c4ceda9bbdb341c109..3ad5a3cc2b4262ba04951dc806240eb03f1d206d 100644 (file)
@@ -4187,6 +4187,19 @@ const CBIFlagValue = CBIValue.extend(/** @lends LuCI.form.FlagValue.prototype */
         * @default 'ℹ️';
         */
 
+       /**
+        * Coerce the various forms of a 'boolean' string into a true/false value.
+        *
+        * @override
+        */
+       load() {
+               // 
+               let load = this.super('load', arguments);
+               if (typeof(load) == 'string')
+                       load = ['1', 'on', 'true', 'yes', 'enabled'].includes(load.toLowerCase());
+               return load;
+       },
+
        /** @private */
        renderWidget(section_id, option_index, cfgvalue) {
                let tooltip = null;