OpenWrt Forum Archive

Topic: Is there a way to set a UCI option from other values in same section

The content of this topic has been archived on 14 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi,

Let's say that we have the following code:

m = Map ("some-module", translate("Users"))
s = m:section(TypedSection, "user", ...)
s:option(Value, "username", translate("User Name"))
s:option(Value, "hostname", translate("Password"))

Upon hitting "Save&Apply", is there a way to create an additional option in "some-module" that is some combination of "username" and "password", say if the user enters "john" and "some.site.com", then I would like to cook the username and hostname with gsub to make "john_some_site_com" like this:

name=string.gsub(**username** .. "_" .. **hostname**, "%W", "_")

but I don't know how to get **username** and **hostname** at the time of hitting "Save&Apply", and I can't find any helpful documentation either! Remember, username and hostname have NOT been saved yet at that moment, so I need some access other than m.uci:get(blablabla).

Can some LuCI deity please help or tell me whether that's even possible?

Any takers on that one? smile

Alright, I also figured that one when I encountered (by chance) a new feature in LuCI 0.10 called "hooks".

Here is some sample code:

function m.on_after_commit(self)
   m.uci:foreach("some-module", "user", 
         function(s1)
            if s1.useername ~= nil and s1.hostname ~= nil then
               field_name=string.gsub(s1.username .. "_" .. s1.hostname, "%W", "_")
               m.uci:set("some-module", s1['.name'], "additional_option", field_name)
            end
         end)
   m.uci:commit("some-module")
end

I hope this helps somebody else! Also, if somebody happens to read this and sees a possibility of improvement, please do post suggestions.

The discussion might have continued from here.