OpenWrt Forum Archive

Topic: SOLVED: Luci, CBI, how to determine if add section button clicked

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

Hi;

I am developing an app suite which requires custom luci pages for licensing and am having problems with section create which always creates a section from defaults. In other words, I cannot have zero sections.

Basically, if there are no section entries in the config file and the create function was not called by the add section button click,  I do not want to create the section.

here`s my template (/usr/lib/lua/luci/view/cbi-select-input-add.htm) for addremove section:

<div class="cbi-section-create">
        <select class="cbi-section-create-name" name="cbi.cts.<%=self.config%>.<%=self.sectiontype%>.select">
        <%- for k, v in luci.util.kspairs(self.add_select_options) do %>
                <option value="<%=k%>"><%=luci.util.pcdata(v)%></option>
        <% end -%>
        </select>
        <input class="cbi-button cbi-button-add" type="submit" value="<%:Add%>" title="<%:Add%>" />
</div>

The CBI maintains a state variable "cbi_state" for the section class which is "add-section" if a section was added or "del-section" if the section was deleted. This is managed by luci javascript.

I have tried to access cbi_state from within the section create function using the following two methods, both of which always return nil:

local cbi_state = luci.http.formvalue( "cbi_state" )
local cbi_state = luci.http.formvalue( luci.cbi.CREATE_PREFIX .. self.config .. "." .. self.sectiontype .. ".cbi_state" )

Questions:

1 - what is the best way (in section create function) to determine if the add and delete section buttons were clicked?
2 - If I am on the right track with cbi_state, how do I access this formvalue from lua?
3 - If cbi_state cannot be accessed, what can I do to the above template to add an on_click handler with a variable that I can access using luci.http.formvalue from within lua?

Thanks;
Bill Ross

(Last edited by rossb on 13 Aug 2016, 17:02)

Solved...

OK, displayed my luci / cbi ignorance with above question. What I was attempting to do was to have a config page where new sections were populated from a config file containing default values with a drop down box to select and add new sections. The configuration for values within each section are contained within another config file. One config file defines the section structure and defaults, another contains configuration for the section values. You will have to edit the controller file to map to your luci structure.

The ability to define luci page sections structure solely from a config file with the user entered section values in another config value is useful, so, I am sharing the code:

http://www.rossco.org/Files/luci-sections-config.tar.gz

The discussion might have continued from here.