X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fcbi.lua;h=0a63d6090a8ffddcd9fb862b630d10f49cd53d42;hp=8dd16b181eb67ee7cd5519248ae15f5eba18c8ff;hb=8038cbf00489fcd0d8406602e5155d2e63b6ad7f;hpb=ede4aca4b95c9e664e4830fd43c54b627b122538 diff --git a/libs/web/luasrc/cbi.lua b/libs/web/luasrc/cbi.lua index 8dd16b181..0a63d6090 100644 --- a/libs/web/luasrc/cbi.lua +++ b/libs/web/luasrc/cbi.lua @@ -1232,6 +1232,24 @@ function AbstractValue.mandatory(self, value) self.rmempty = not value end +function AbstractValue.add_error(self, section, type, msg) + self.error = self.error or { } + self.error[section] = msg or type + + self.section.error = self.section.error or { } + self.section.error[section] = self.section.error[section] or { } + table.insert(self.section.error[section], msg or type) + + if type == "invalid" then + self.tag_invalid[section] = true + elseif type == "missing" then + self.tag_missing[section] = true + end + + self.tag_error[section] = true + self.map.save = false +end + function AbstractValue.parse(self, section, novld) local fvalue = self:formvalue(section) local cvalue = self:cfgvalue(section) @@ -1253,20 +1271,14 @@ function AbstractValue.parse(self, section, novld) end if fvalue and #fvalue > 0 then -- If we have a form value, write it to UCI - fvalue = self:transform(self:validate(fvalue, section)) + local val_err + fvalue, val_err = self:validate(fvalue, section) + fvalue = self:transform(fvalue) + if not fvalue and not novld then - if self.error then - self.error[section] = "invalid" - else - self.error = { [section] = "invalid" } - end - if self.section.error then - table.insert(self.section.error[section], "invalid") - else - self.section.error = {[section] = {"invalid"}} - end - self.map.save = false + self:add_error(section, "invalid", val_err) end + if fvalue and not (fvalue == cvalue) then if self:write(section, fvalue) then -- Push events @@ -1282,13 +1294,9 @@ function AbstractValue.parse(self, section, novld) --luci.util.append(self.map.events, self.events) end elseif cvalue ~= fvalue and not novld then - self:write(section, fvalue or "") - if self.error then - self.error[section] = "missing" - else - self.error = { [section] = "missing" } - end - self.map.save = false + -- trigger validator with nil value to get custom user error msg. + local _, val_err = self:validate(nil, section) + self:add_error(section, "missing", val_err) end end end @@ -1326,8 +1334,13 @@ end -- Return the UCI value of this object function AbstractValue.cfgvalue(self, section) - local value = (self.error and self.error[section] == "invalid") - and self:formvalue(section) or self.map:get(section, self.option) + local value + if self.tag_error[section] then + value = self:formvalue(section) + else + value = self.map:get(section, self.option) + end + if not value then return nil elseif not self.cast or self.cast == type(value) then