2023-08-10 10:10:01 +00:00
# Writing LuCI CBI models
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
See [online wiki ](https://github.com/openwrt/luci/wiki/CBI ) for latest version.
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
CBI models are Lua files describing the structure of an UCI config file and the resulting HTML form to be evaluated by the CBI parser.< br / >
All CBI model files must return an object of type `luci.cbi.Map` .< br />
For a commented example of a CBI model, see the [Writing Modules tutorial ](./ModulesHowTo.md ).
The scope of a CBI model file is automatically extended by the contents of the module `luci.cbi` and the `translate` function from `luci.i18n` .
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
This Reference covers **the basics** of the CBI system.
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
## class Map (config, title, description)
2015-06-16 08:11:03 +00:00
This is the root object of the model.
2023-08-10 10:10:01 +00:00
* `config:` configuration filename to be mapped, see [UCI documentation ](https://openwrt.org/docs/guide-user/base-system/uci ) and the files in `/etc/config`
* `title:` title shown in the UI
* `description:` description shown in the UI
2015-11-01 16:57:08 +00:00
2023-08-10 10:10:01 +00:00
#### function :section (sectionclass, ...)
2015-06-16 08:11:03 +00:00
Creates a new section
2023-08-10 10:10:01 +00:00
* `sectionclass` : a class object of the section
2015-06-16 08:11:03 +00:00
* _additional parameters passed to the constructor of the section class_
----
2023-08-10 10:10:01 +00:00
## class NamedSection (name, type, title, description)
2015-11-01 16:57:08 +00:00
An object describing an UCI section selected by the name.< br / >
To instantiate use: `Map:section(NamedSection, "name", "type", "title", "description")`
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
* `name:` UCI section name
* `type:` UCI section type
* `title:` The title shown in the UI
* `description:` description shown in the UI
2015-11-01 16:57:08 +00:00
2023-08-10 10:10:01 +00:00
#### function :option(optionclass, ...)
2015-11-01 16:57:08 +00:00
Creates a new option
2023-08-10 10:10:01 +00:00
* `optionclass:` a class object of the section
2015-11-01 16:57:08 +00:00
* _additional parameters passed to the constructor of the option class_
#### property .addremove = false
Allows the user to remove and recreate the configuration section.
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .dynamic = false
2023-08-10 10:10:01 +00:00
Marks this section as dynamic.
Dynamic sections can contain an undefinded number of completely userdefined options.
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .optional = true
2015-06-16 08:11:03 +00:00
Parse optional options
2015-11-01 16:57:08 +00:00
----
2023-08-10 10:10:01 +00:00
## class TypedSection (type, title, description)
2015-11-01 16:57:08 +00:00
An object describing a group of UCI sections selected by their type.< br / >
To instantiate use: `Map:section(TypedSection, "type", "title", "description")`
2023-08-10 10:10:01 +00:00
* `type:` UCI section type
* `title:` The title shown in the UI
* `description:` description shown in the UI
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
#### function :option(optionclass, ...)
2015-06-16 08:11:03 +00:00
Creates a new option
2023-08-10 10:10:01 +00:00
* `optionclass:` a class object of the section
* _additional parameters passed to the constructor of the option class_
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
#### function :depends(key, value)
Only show this option field if another option `key` is set to `value` in the same section.< br />
2015-11-01 16:57:08 +00:00
If you call this function several times the dependencies will be linked with ** "or"**
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
#### function .filter(self, section) -abstract-
2015-11-01 16:57:08 +00:00
You can override this function to filter certain sections that will not be parsed.
2023-08-10 10:10:01 +00:00
The filter function will be called for every section that should be parsed and returns `nil` for sections that should be filtered.
For all other sections it should return the section name as given in the second parameter.
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .addremove = false
2015-06-16 08:11:03 +00:00
Allows the user to remove and recreate the configuration section
2015-11-01 16:57:08 +00:00
#### property .dynamic = false
2023-08-10 10:10:01 +00:00
Marks this section as dynamic.
Dynamic sections can contain an undefinded number of completely userdefined options.
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .optional = true
2015-06-16 08:11:03 +00:00
Parse optional options
2015-11-01 16:57:08 +00:00
#### property .anonymous = false
Do not show UCI section names
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
----
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
## class Value (option, title, description)
2015-11-01 16:57:08 +00:00
An object describing an option in a section of a UCI File. Creates a standard text field in the formular.< br / >
To instantiate use: `NamedSection:option(Value, "option", "title", "description")` < br />
or `TypedSection:option(Value, "option", "title", "description")`
2023-08-10 10:10:01 +00:00
* `option:` UCI option name
* `title:` The title shown in the UI
* `description:` description shown in the UI
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### function :depends(key, value)
2023-08-10 10:10:01 +00:00
Only show this option field if another option `key` is set to `value` in the same section.< br />
2015-11-01 16:57:08 +00:00
If you call this function several times the dependencies will be linked with ** "or"**
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### function :value(key, value)
Convert this text field into a combobox if possible and add a selection option.
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .default = nil
2015-06-16 08:11:03 +00:00
The default value
2015-11-01 16:57:08 +00:00
#### property .maxlength = nil
2023-08-10 10:10:01 +00:00
The maximum input length (of chars) of the value
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .optional = false
2023-08-10 10:10:01 +00:00
Marks this option as optional, implies `.rmempty = true`
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .rmempty = true
2015-06-16 08:11:03 +00:00
Removes this option from the configuration file when the user enters an empty value
2015-11-01 16:57:08 +00:00
#### property .size = nil
The maximum number of chars displayed by form field
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
----
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
## class ListValue (option, title, description)
2015-11-01 16:57:08 +00:00
An object describing an option in a section of a UCI File.< br / >
Creates a list box or list of radio (for selecting one of many choices) in the formular.< br / >
To instantiate use: `NamedSection:option(ListValue, "option", "title", "description")` < br />
or `TypedSection:option(ListValue, "option", "title", "description")`
2023-08-10 10:10:01 +00:00
* `option:` UCI option name
* `title:` The title shown in the UI
* `description:` description shown in the UI
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### function :depends(key, value)
2023-08-10 10:10:01 +00:00
Only show this option field if another option `key` is set to `value` in the same section.< br />
2015-11-01 16:57:08 +00:00
If you call this function several times the dependencies will be linked with ** "or"**
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
#### function :value(key, value)
2015-11-01 16:57:08 +00:00
Adds an entry to the selection list
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .widget = "select"
2023-08-10 10:10:01 +00:00
`select` shows a selection list, `radio` shows a list of radio buttons inside form
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .default = nil
2015-06-16 08:11:03 +00:00
The default value
2015-11-01 16:57:08 +00:00
#### property .optional = false
2023-08-10 10:10:01 +00:00
Marks this option as optional, implies `.rmempty = true`
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .rmempty = true
2015-06-16 08:11:03 +00:00
Removes this option from the configuration file when the user enters an empty value
2015-11-01 16:57:08 +00:00
#### property .size = nil
2015-06-16 08:11:03 +00:00
The size of the form field
----
2023-08-10 10:10:01 +00:00
## class Flag (option, title, description)
2015-11-01 16:57:08 +00:00
An object describing an option with two possible values in a section of a UCI File.< br / >
Creates a checkbox field in the formular.< br / >
2023-08-10 10:10:01 +00:00
To instantiate use: `NamedSection:option(Flag, "option", "title", "description")` < br />
2015-11-01 16:57:08 +00:00
or `TypedSection:option(Flag, "option", "title", "description")`
2023-08-10 10:10:01 +00:00
* `option:` UCI option name
* `title:` The title shown in the UI
* `description:` description shown in the UI
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
#### function :depends (key, value)
Only show this option field if another option `key` is set to `value` in the same section.< br />
2015-11-01 16:57:08 +00:00
If you call this function several times the dependencies will be linked with ** "or"**
#### property .default = nil
2015-06-16 08:11:03 +00:00
The default value
2015-11-01 16:57:08 +00:00
#### property .disabled = 0
2016-03-26 09:11:33 +00:00
the value that should be set if the checkbox is unchecked
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .enabled = 1
2015-06-16 08:11:03 +00:00
the value that should be set if the checkbox is checked
2015-11-01 16:57:08 +00:00
#### property .optional = false
2023-08-10 10:10:01 +00:00
Marks this option as optional, implies `.rmempty = true`
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .rmempty = true
2015-06-16 08:11:03 +00:00
Removes this option from the configuration file when the user enters an empty value
2015-11-01 16:57:08 +00:00
----
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
## class MultiValue (option, title, description)
2015-11-01 16:57:08 +00:00
An object describing an option in a section of a UCI File.< br / >
Creates a list of checkboxed or a multiselectable list as form fields.< br / >
2023-08-10 10:10:01 +00:00
To instantiate use: `NamedSection:option(MultiValue, "option", "title", "description")` < br />
2015-11-01 16:57:08 +00:00
or `TypedSection:option(MultiValue, "option", "title", "description")`
2023-08-10 10:10:01 +00:00
* `option:` UCI option name
* `title:` The title shown in the UI
* `description:` description shown in the UI
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
#### function :depends (key, value)
Only show this option field if another option `key` is set to `value` in the same section.< br />
2015-11-01 16:57:08 +00:00
If you call this function several times the dependencies will be linked with ** "or"**
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
#### function :value(key, value)
2015-11-01 16:57:08 +00:00
Adds an entry to the list
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .widget = "checkbox"
2023-08-10 10:10:01 +00:00
`select` shows a selection list, `checkbox` shows a list of checkboxes inside form
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .delimiter = " "
The string which will be used to delimit the values inside stored option
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .default = nil
2015-06-16 08:11:03 +00:00
The default value
2015-11-01 16:57:08 +00:00
#### property .optional = false
2023-08-10 10:10:01 +00:00
Marks this option as optional, implies `.rmempty = true`
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
#### property .rmempty = true
2015-06-16 08:11:03 +00:00
Removes this option from the configuration file when the user enters an empty value
2015-11-01 16:57:08 +00:00
#### property .size = nil
2023-08-10 10:10:01 +00:00
The size of the form field (only used if property `.widget = "select"` )
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
----
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
## class StaticList (option, title, description)
Similar to the `MultiValue` , but stores selected Values into a UCI list instead of a character-separated option.
2015-06-16 08:11:03 +00:00
----
2023-08-10 10:10:01 +00:00
## class DynamicList (option, title, description)
2015-11-01 16:57:08 +00:00
A extensible list of user-defined values. Stores Values into a UCI list
2015-06-16 08:11:03 +00:00
2015-11-01 16:57:08 +00:00
----
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
## class DummyValue (option, title, description)
2015-11-01 16:57:08 +00:00
Creates a readonly text in the form. !It writes no data to UCI!< br / >
2023-08-10 10:10:01 +00:00
To instantiate use: `NamedSection:option(DummyValue, "option", "title", "description")` < br />
2015-11-01 16:57:08 +00:00
or `TypedSection:option(DummyValue, "option", "title", "description")`
2023-08-10 10:10:01 +00:00
* `option:` UCI option name
* `title:` The title shown in the UI
* `description:` description shown in the UI
2015-06-16 08:11:03 +00:00
2023-08-10 10:10:01 +00:00
#### function :depends (key, value)
Only show this option field if another option `key` is set to `value` in the same section.< br />
2015-11-01 16:57:08 +00:00
If you call this function several times the dependencies will be linked with ** "or"**
2015-06-16 08:11:03 +00:00
----
2023-08-10 10:10:01 +00:00
## class TextValue (option, title, description)
2015-06-16 08:11:03 +00:00
An object describing a multi-line textbox in a section in a non-UCI form.
----
2023-08-10 10:10:01 +00:00
## class Button (option, title, description)
2015-06-16 08:11:03 +00:00
An object describing a Button in a section in a non-UCI form.