X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fcbi.lua;h=0fc93b25cd3dce1fa8f504fe743d0fccbfe3c405;hb=be385bfcc63a4e75698ea7b564b0c1f5e50ba4ed;hp=17ca18c9411493d1ff65e35eba715029162bdf50;hpb=70706cf388f8ac100778831e9ef9d7b1eb74c752;p=oweals%2Fluci.git diff --git a/libs/web/luasrc/cbi.lua b/libs/web/luasrc/cbi.lua index 17ca18c94..0fc93b25c 100644 --- a/libs/web/luasrc/cbi.lua +++ b/libs/web/luasrc/cbi.lua @@ -1281,7 +1281,7 @@ function AbstractValue.parse(self, section, novld) self:add_error(section, "invalid", val_err) end - if fvalue and not (fvalue == cvalue) then + if fvalue and (self.forcewrite or not (fvalue == cvalue)) then if self:write(section, fvalue) then -- Push events self.section.changed = true @@ -1358,20 +1358,35 @@ end -- Validate the form value function AbstractValue.validate(self, value) - if self.datatype and value and datatypes[self.datatype] then - if type(value) == "table" then - local v - for _, v in ipairs(value) do - if v and #v > 0 and not datatypes[self.datatype](v) then - return nil - end + if self.datatype and value then + local args = { } + local dt, ar = self.datatype:match("^(%w+)%(([^%(%)]+)%)") + + if dt and ar then + local a + for a in ar:gmatch("[^%s,]+") do + args[#args+1] = a end else - if not datatypes[self.datatype](value) then - return nil + dt = self.datatype + end + + if dt and datatypes[dt] then + if type(value) == "table" then + local v + for _, v in ipairs(value) do + if v and #v > 0 and not datatypes[dt](v, unpack(args)) then + return nil + end + end + else + if not datatypes[dt](value, unpack(args)) then + return nil + end end end end + return value end @@ -1642,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 @@ -1662,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