Search code examples
luaopenwrtluci

how to update cbi variable in luci web interface using lua code before commit?


I'm trying to modify each element in the dynamic list and if those modification went ok and the validation passed, i want to commit the updated variable and not the original. example: i pass "A B C" and "1 2 3 ABC" and i want the function to save them as "ABC" and "123ABC" i'm not having a problem with the validation and deleting spaces (doing the modification) the issue is i don't know how to replace the old strings with the new strings.

i can't post the code but this is a general idea of what i'm doing

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
   -- change the value here and delete spaces--
   -- validate the new value --
 end
 Value.parse(self, section, novld, ...)
end

this is the general idea, and i tried to use Value.write(self, section, list) where the list is the same old list but each time i modify a value i update it in the list like this, list[key] = value (after modofication) the thing is, the write function has no effect if the function reached the Value.parse.


Solution

  • this was the solution if anyone may need it.

    list.write = function(self, section, value)
    local list
    if type(value) == "table" then
        list = value
    elseif value ~= nil then
        list = { value }
    else
        return -- should not happen, .remove() should have been called then
    end
    for _, item in ipairs(list) do
        list[_] = strip_spaces(item)
    end
    return Value.write(self, section, list)
    end
    

    for more details see this post: link