如何使用Lua代码在Luci Web界面中更新CBI变量并在提交之前进行更新?

我正在尝试修改动态列表中的每个元素,如果这些修改顺利进行并且验证通过,我希望提交更新的变量而不是原始变量。 示例:我传递"A B C"和"1 2 3 ABC",我希望函数将它们保存为"ABC"和"123ABC" 我没有问题验证和删除空格(进行修改),问题是我不知道如何将旧字符串替换为新字符串。

我不能发布代码,但这是我正在做的一般想法

list = s:option(DynamicList, "text", "text")
list.parse = function(self, section, novld, ...)
 local listString = luci.http.formvalue(path to the list)
 for key, value in pairs(listString) do
   -- 在此处更改值并删除空格--
   -- 验证新值--
 end
 Value.parse(self, section, novld, ...)
end

这是一般的想法,我尝试使用Value.write(self, section, list),其中列表是相同的旧列表,但每次修改值时,我都会在列表中更新它,就像这样,list \ [key ] = value(经过修改后)的事情是,如果函数达到Value.parse,则写入函数将没有任何效果。

点赞
用户4882880
用户4882880

以下是解决方案,如果有人需要。

list.write = function(self, section, value)
local list
if type(value) == "table" then
    list = value
elseif value ~= nil then
    list = { value }
else
    return -- 应该不会发生,否则应该调用 .remove()
end
for _, item in ipairs(list) do
    list[_] = strip_spaces(item)
end
return Value.write(self, section, list)
end

更多详情请查看此帖子: 链接

2020-07-13 08:51:02