How to update one field based on another field in luci

I need to update one field based on another field.
suppose one field is empty and I need to update it same value as another field.

Eg -

m = Map("abc",translate("SETTINGS"),translate(""))
s = m:section(NamedSection, section, "redirect", "")

o = s:option(Value, "a", translate("a name"),translate(""))
o = s:option(Value, "b", translate("b name"),translate(""))

In above example, my fist field is for "a" which is non-blank and second field for "b" which is blank. Both are not mandatory fields.

If "b" is blank value and "a" is non-blank then I need to update "b" same value as "a".

"b" is blank so non of function is getting called for that field, eg- write() and validate() functions.

Is there are any another way to do this work?
Any help would be appreciated.

There are probably more elegant ways of doing this, but facing the similar problem I've created the on_after_commit function for the map, where I checked the a and b values retrieved with uci and if any of them needed changing, updated them and then uci-save/uci-commit.

PS. You efforts to improve lua code may be short-lived, as there's a massive effort to remove any dependency on lua in the core code.

Thanks for response,
I have tried that several hooks - on_after_commit, on_before_commit, on_commit, on_apply, on_before_apply , But facing an issue there also,
actually its setting the value but somehow again its removing , when i run uci:change cammand then i got to know about that issue-

  "changes": [
                [
                        "set",
                        "cfg183837",
                        "b",
                        "1234"
                ],
                [
                        "remove",
                        "cfg183837",
                        "b"
                ]
        ]

Eg -

m.on_after_commit = function(self)
 	local a = m.uci:get("abc", arg[1], "a")
 	local b = m.uci:get("abc", arg[1], "b")
 	if a and b == nil then
		m.uci:set("abc", arg[1], "b", a)
		m.uci:save("abc")
		m.uci:commit('abc')
 		return true
	end
end

Same code, but within the function I get/save/commit variables from uci directly, without the use of the map and it works.

Okay, Thanks for help.

Okay, you're welcome.

1 Like

So, I have come up with the another solution by using formvalue() function

function o.formvalue(self, section)
	local a = m:formvalue("cbid."..section..".a")
	local b = m:formvalue("cbid."..section..".b")
	if a and b == nil then 
		return a
	else
		return b
	end
end

PS: I am just learner, just started to learn and explore the things.