X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fcbi.lua;h=0fc93b25cd3dce1fa8f504fe743d0fccbfe3c405;hb=be385bfcc63a4e75698ea7b564b0c1f5e50ba4ed;hp=5fa992dee8e740a88efde18d3163440553dcb2bb;hpb=fbaabd7d93e72506756e18988f98fca139e202e6;p=oweals%2Fluci.git diff --git a/libs/web/luasrc/cbi.lua b/libs/web/luasrc/cbi.lua index 5fa992dee..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 @@ -1641,25 +1656,64 @@ function DynamicList.value(self, key, val) table.insert(self.vallist, tostring(val)) end -function DynamicList.write(self, ...) - self.map.proceed = true - return AbstractValue.write(self, ...) +function DynamicList.write(self, section, value) + 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 + + return AbstractValue.write(self, section, value) +end + +function DynamicList.cfgvalue(self, section) + local value = AbstractValue.cfgvalue(self, section) + + if type(value) == "string" then + local x + local t = { } + for x in value:gmatch("%S+") do + if #x > 0 then + t[#t+1] = x + end + end + value = t + end + + return value end function DynamicList.formvalue(self, section) local value = AbstractValue.formvalue(self, section) - value = (type(value) == "table") and value or {value} - local valid = {} - for i, v in ipairs(value) do - if v and #v > 0 - and not self.map:formvalue("cbi.rle."..section.."."..self.option.."."..i) - and not self.map:formvalue("cbi.rle."..section.."."..self.option.."."..i..".x") then - table.insert(valid, v) + if type(value) == "string" then + local x + local t = { } + for x in value:gmatch("%S+") do + t[#t+1] = x end + value = t end - return valid + return value end