How to modify cbi variable in luci 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 --
 Value.parse(self, section, novld, ...)

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.

I don't understand what you're trying to do. Modify the values before they're written to the config? Modify the values shown in the widget?

modify the value before they are written to the config file

Override the .write() function then.

list.write = function(self, section, value)
    value = strip_spaces(value)
    return Value.write(self, section, value)

when you said "value = strip_spaces(value)" will that work on each element of the list or i need to do it like this

for key, element in pairs(value ) do
 value[key] = strip_speces(v)

I think you need to iterate using ipairs() and make sure that it is a table to begin with... something like the code below:

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

Thank you, it works
by the way, can you recommend any doc for Luci. i'm kinda struggling to find a useful documentation where i can find those functions and methods with a basic examples

For the Lua based CBI there is not much useful information unfortunately. I wrote a comprehensive reference for the JavaScript based reimplementation which functions very similar and uses the same logic flow and function names internally. Maybe you can deduce useful information from it:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.