libs/web: better server side handling of DynList values
[oweals/luci.git] / libs / web / luasrc / cbi.lua
index 6c9e7a544f2d2cad8ee5721dc6ff65032570693e..0fc93b25cd3dce1fa8f504fe743d0fccbfe3c405 100644 (file)
@@ -1657,13 +1657,27 @@ function DynamicList.value(self, key, val)
 end
 
 function DynamicList.write(self, section, value)
-       if self.cast == "string" and type(value) == "table" then
-               value = table.concat(value, " ")
-       elseif self.cast == "table" and type(value) == "string" then
-               local x, t = { }
-               for x in value:gmatch("%S+") do
+       local t = { }
+
+       if type(value) == "table" then
+               local x
+               for _, x in ipairs(value) do
+                       if x and #x > 0 then
+                               t[#t+1] = x
+                       end
+               end
+       elseif self.cast == "table" then
+               local x
+               for x in util.imatch(value) do
                        t[#t+1] = x
                end
+       else
+               t = { value }
+       end
+
+       if self.cast == "string" then
+               value = table.concat(t, " ")
+       else
                value = t
        end
 
@@ -1677,7 +1691,9 @@ function DynamicList.cfgvalue(self, section)
                local x
                local t = { }
                for x in value:gmatch("%S+") do
-                       t[#t+1] = x
+                       if #x > 0 then
+                               t[#t+1] = x
+                       end
                end
                value = t
        end