libs/web: fix various issues in cbi option error handling, call validator for missing options to obtain error message
This commit is contained in:
parent
ba5e6a9e63
commit
8038cbf004
1 changed files with 30 additions and 19 deletions
|
@ -1232,6 +1232,24 @@ function AbstractValue.mandatory(self, value)
|
||||||
self.rmempty = not value
|
self.rmempty = not value
|
||||||
end
|
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)
|
function AbstractValue.parse(self, section, novld)
|
||||||
local fvalue = self:formvalue(section)
|
local fvalue = self:formvalue(section)
|
||||||
local cvalue = self:cfgvalue(section)
|
local cvalue = self:cfgvalue(section)
|
||||||
|
@ -1258,17 +1276,9 @@ function AbstractValue.parse(self, section, novld)
|
||||||
fvalue = self:transform(fvalue)
|
fvalue = self:transform(fvalue)
|
||||||
|
|
||||||
if not fvalue and not novld then
|
if not fvalue and not novld then
|
||||||
val_err = val_err or "invalid"
|
self:add_error(section, "invalid", val_err)
|
||||||
|
|
||||||
self.error = self.error or { }
|
|
||||||
self.error[section] = val_err
|
|
||||||
|
|
||||||
self.section.error = self.section.error or { }
|
|
||||||
self.section.error[section] = self.section.error[section] or { }
|
|
||||||
table.insert(self.section.error[section], val_err)
|
|
||||||
|
|
||||||
self.map.save = false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if fvalue and not (fvalue == cvalue) then
|
if fvalue and not (fvalue == cvalue) then
|
||||||
if self:write(section, fvalue) then
|
if self:write(section, fvalue) then
|
||||||
-- Push events
|
-- Push events
|
||||||
|
@ -1284,13 +1294,9 @@ function AbstractValue.parse(self, section, novld)
|
||||||
--luci.util.append(self.map.events, self.events)
|
--luci.util.append(self.map.events, self.events)
|
||||||
end
|
end
|
||||||
elseif cvalue ~= fvalue and not novld then
|
elseif cvalue ~= fvalue and not novld then
|
||||||
self:write(section, fvalue or "")
|
-- trigger validator with nil value to get custom user error msg.
|
||||||
if self.error then
|
local _, val_err = self:validate(nil, section)
|
||||||
self.error[section] = "missing"
|
self:add_error(section, "missing", val_err)
|
||||||
else
|
|
||||||
self.error = { [section] = "missing" }
|
|
||||||
end
|
|
||||||
self.map.save = false
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1328,8 +1334,13 @@ end
|
||||||
|
|
||||||
-- Return the UCI value of this object
|
-- Return the UCI value of this object
|
||||||
function AbstractValue.cfgvalue(self, section)
|
function AbstractValue.cfgvalue(self, section)
|
||||||
local value = (self.error and self.error[section] == "invalid")
|
local value
|
||||||
and self:formvalue(section) or self.map:get(section, self.option)
|
if self.tag_error[section] then
|
||||||
|
value = self:formvalue(section)
|
||||||
|
else
|
||||||
|
value = self.map:get(section, self.option)
|
||||||
|
end
|
||||||
|
|
||||||
if not value then
|
if not value then
|
||||||
return nil
|
return nil
|
||||||
elseif not self.cast or self.cast == type(value) then
|
elseif not self.cast or self.cast == type(value) then
|
||||||
|
|
Loading…
Reference in a new issue