1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.cbi", package.seeall)
6 require("luci.template")
7 local util = require("luci.util")
11 --local event = require "luci.sys.event"
12 local fs = require("nixio.fs")
13 local uci = require("luci.model.uci")
14 local datatypes = require("luci.cbi.datatypes")
15 local dispatcher = require("luci.dispatcher")
16 local class = util.class
17 local instanceof = util.instanceof
29 CREATE_PREFIX = "cbi.cts."
30 REMOVE_PREFIX = "cbi.rts."
31 RESORT_PREFIX = "cbi.sts."
32 FEXIST_PREFIX = "cbi.cbe."
34 -- Loads a CBI map from given file, creating an environment and returns it
35 function load(cbimap, ...)
36 local fs = require "nixio.fs"
37 local i18n = require "luci.i18n"
38 require("luci.config")
41 local upldir = "/lib/uci/upload/"
42 local cbidir = luci.util.libpath() .. "/model/cbi/"
45 if fs.access(cbidir..cbimap..".lua") then
46 func, err = loadfile(cbidir..cbimap..".lua")
47 elseif fs.access(cbimap) then
48 func, err = loadfile(cbimap)
50 func, err = nil, "Model '" .. cbimap .. "' not found!"
56 translate=i18n.translate,
57 translatef=i18n.translatef,
61 setfenv(func, setmetatable(env, {__index =
63 return rawget(tbl, key) or _M[key] or _G[key]
66 local maps = { func() }
68 local has_upload = false
70 for i, map in ipairs(maps) do
71 if not instanceof(map, Node) then
72 error("CBI map returns no valid map object!")
76 if map.upload_fields then
78 for _, field in ipairs(map.upload_fields) do
80 field.config .. '.' ..
81 (field.section.sectiontype or '1') .. '.' ..
90 local uci = luci.model.uci.cursor()
91 local prm = luci.http.context.request.message.params
94 luci.http.setfilehandler(
95 function( field, chunk, eof )
96 if not field then return end
97 if field.name and not cbid then
98 local c, s, o = field.name:gmatch(
99 "cbid%.([^%.]+)%.([^%.]+)%.([^%.]+)"
102 if c and s and o then
103 local t = uci:get( c, s ) or s
104 if uploads[c.."."..t.."."..o] then
105 local path = upldir .. field.name
106 fd = io.open(path, "w")
115 if field.name == cbid and fd then
132 -- Compile a datatype specification into a parse tree for evaluation later on
134 local cdt_cache = { }
136 function compile_datatype(code)
143 for i = 1, #code+1 do
144 local byte = code:byte(i) or 44
147 elseif byte == 92 then
149 elseif byte == 40 or byte == 44 then
152 local label = code:sub(pos, i-1)
157 if #label > 0 and tonumber(label) then
158 stack[#stack+1] = tonumber(label)
159 elseif label:match("^'.*'$") or label:match('^".*"$') then
160 stack[#stack+1] = label:gsub("[\"'](.*)[\"']", "%1")
161 elseif type(datatypes[label]) == "function" then
162 stack[#stack+1] = datatypes[label]
163 stack[#stack+1] = { }
165 error("Datatype error, bad token %q" % label)
170 depth = depth + (byte == 40 and 1 or 0)
171 elseif byte == 41 then
174 if type(stack[#stack-1]) ~= "function" then
175 error("Datatype error, argument list follows non-function")
177 stack[#stack] = compile_datatype(code:sub(pos, i-1))
186 function verify_datatype(dt, value)
187 if dt and #dt > 0 then
188 if not cdt_cache[dt] then
189 local c = compile_datatype(dt)
190 if c and type(c[1]) == "function" then
193 error("Datatype error, not a function expression")
196 if cdt_cache[dt] then
197 return cdt_cache[dt][1](value, unpack(cdt_cache[dt][2]))
204 -- Node pseudo abstract class
207 function Node.__init__(self, title, description)
209 self.title = title or ""
210 self.description = description or ""
211 self.template = "cbi/node"
215 function Node._run_hook(self, hook)
216 if type(self[hook]) == "function" then
217 return self[hook](self)
221 function Node._run_hooks(self, ...)
224 for _, f in ipairs(arg) do
225 if type(self[f]) == "function" then
234 function Node.prepare(self, ...)
235 for k, child in ipairs(self.children) do
240 -- Append child nodes
241 function Node.append(self, obj)
242 table.insert(self.children, obj)
245 -- Parse this node and its children
246 function Node.parse(self, ...)
247 for k, child in ipairs(self.children) do
253 function Node.render(self, scope)
257 luci.template.render(self.template, scope)
260 -- Render the children
261 function Node.render_children(self, ...)
263 for k, node in ipairs(self.children) do
264 node.last_child = (k == #self.children)
271 A simple template element
273 Template = class(Node)
275 function Template.__init__(self, template)
277 self.template = template
280 function Template.render(self)
281 luci.template.render(self.template, {self=self})
284 function Template.parse(self, readinput)
285 self.readinput = (readinput ~= false)
286 return Map.formvalue(self, "cbi.submit") and FORM_DONE or FORM_NODATA
291 Map - A map describing a configuration file
295 function Map.__init__(self, config, ...)
296 Node.__init__(self, ...)
299 self.parsechain = {self.config}
300 self.template = "cbi/map"
301 self.apply_on_parse = nil
302 self.readinput = true
306 self.uci = uci.cursor()
311 local path = "%s/%s" %{ self.uci:get_confdir(), self.config }
312 if fs.stat(path, "type") ~= "reg" then
313 fs.writefile(path, "")
316 local ok, err = self.uci:load(self.config)
318 local url = dispatcher.build_url(unpack(dispatcher.context.request))
319 local source = self:formvalue("cbi.source")
320 if type(source) == "string" then
321 fs.writefile(path, source:gsub("\r\n", "\n"))
322 ok, err = self.uci:load(self.config)
324 luci.http.redirect(url)
330 self.template = "cbi/error"
332 self.source = fs.readfile(path) or ""
333 self.pageaction = false
337 function Map.formvalue(self, key)
338 return self.readinput and luci.http.formvalue(key)
341 function Map.formvaluetable(self, key)
342 return self.readinput and luci.http.formvaluetable(key) or {}
345 function Map.get_scheme(self, sectiontype, option)
347 return self.scheme and self.scheme.sections[sectiontype]
349 return self.scheme and self.scheme.variables[sectiontype]
350 and self.scheme.variables[sectiontype][option]
354 function Map.submitstate(self)
355 return self:formvalue("cbi.submit")
358 -- Chain foreign config
359 function Map.chain(self, config)
360 table.insert(self.parsechain, config)
363 function Map.state_handler(self, state)
367 -- Use optimized UCI writing
368 function Map.parse(self, readinput, ...)
369 self.readinput = (readinput ~= false)
370 self:_run_hooks("on_parse")
372 if self:formvalue("cbi.skip") then
373 self.state = FORM_SKIP
374 return self:state_handler(self.state)
377 Node.parse(self, ...)
380 self:_run_hooks("on_save", "on_before_save")
381 for i, config in ipairs(self.parsechain) do
382 self.uci:save(config)
384 self:_run_hooks("on_after_save")
385 if self:submitstate() and ((not self.proceed and self.flow.autoapply) or luci.http.formvalue("cbi.apply")) then
386 self:_run_hooks("on_before_commit")
387 for i, config in ipairs(self.parsechain) do
388 self.uci:commit(config)
390 -- Refresh data because commit changes section names
391 self.uci:load(config)
393 self:_run_hooks("on_commit", "on_after_commit", "on_before_apply")
394 if self.apply_on_parse then
395 self.uci:apply(self.parsechain)
396 self:_run_hooks("on_apply", "on_after_apply")
398 -- This is evaluated by the dispatcher and delegated to the
399 -- template which in turn fires XHR to perform the actual
401 self.apply_needed = true
405 Node.parse(self, true)
408 for i, config in ipairs(self.parsechain) do
409 self.uci:unload(config)
411 if type(self.commit_handler) == "function" then
412 self:commit_handler(self:submitstate())
416 if self:submitstate() then
417 if not self.save then
418 self.state = FORM_INVALID
419 elseif self.proceed then
420 self.state = FORM_PROCEED
422 self.state = self.changed and FORM_CHANGED or FORM_VALID
425 self.state = FORM_NODATA
428 return self:state_handler(self.state)
431 function Map.render(self, ...)
432 self:_run_hooks("on_init")
433 Node.render(self, ...)
436 -- Creates a child section
437 function Map.section(self, class, ...)
438 if instanceof(class, AbstractSection) then
439 local obj = class(self, ...)
443 error("class must be a descendent of AbstractSection")
448 function Map.add(self, sectiontype)
449 return self.uci:add(self.config, sectiontype)
453 function Map.set(self, section, option, value)
454 if type(value) ~= "table" or #value > 0 then
456 return self.uci:set(self.config, section, option, value)
458 return self.uci:set(self.config, section, value)
461 return Map.del(self, section, option)
466 function Map.del(self, section, option)
468 return self.uci:delete(self.config, section, option)
470 return self.uci:delete(self.config, section)
475 function Map.get(self, section, option)
477 return self.uci:get_all(self.config)
479 return self.uci:get(self.config, section, option)
481 return self.uci:get_all(self.config, section)
488 Compound = class(Node)
490 function Compound.__init__(self, ...)
492 self.template = "cbi/compound"
493 self.children = {...}
496 function Compound.populate_delegator(self, delegator)
497 for _, v in ipairs(self.children) do
498 v.delegator = delegator
502 function Compound.parse(self, ...)
503 local cstate, state = 0
505 for k, child in ipairs(self.children) do
506 cstate = child:parse(...)
507 state = (not state or cstate < state) and cstate or state
515 Delegator - Node controller
517 Delegator = class(Node)
518 function Delegator.__init__(self, ...)
519 Node.__init__(self, ...)
521 self.defaultpath = {}
522 self.pageaction = false
523 self.readinput = true
524 self.allow_reset = false
525 self.allow_cancel = false
526 self.allow_back = false
527 self.allow_finish = false
528 self.template = "cbi/delegator"
531 function Delegator.set(self, name, node)
532 assert(not self.nodes[name], "Duplicate entry")
534 self.nodes[name] = node
537 function Delegator.add(self, name, node)
538 node = self:set(name, node)
539 self.defaultpath[#self.defaultpath+1] = name
542 function Delegator.insert_after(self, name, after)
543 local n = #self.chain + 1
544 for k, v in ipairs(self.chain) do
550 table.insert(self.chain, n, name)
553 function Delegator.set_route(self, ...)
554 local n, chain, route = 0, self.chain, {...}
556 if chain[i] == self.current then
565 for i = n + 1, #chain do
570 function Delegator.get(self, name)
571 local node = self.nodes[name]
573 if type(node) == "string" then
574 node = load(node, name)
577 if type(node) == "table" and getmetatable(node) == nil then
578 node = Compound(unpack(node))
584 function Delegator.parse(self, ...)
585 if self.allow_cancel and Map.formvalue(self, "cbi.cancel") then
586 if self:_run_hooks("on_cancel") then
591 if not Map.formvalue(self, "cbi.delg.current") then
592 self:_run_hooks("on_init")
596 self.chain = self.chain or self:get_chain()
597 self.current = self.current or self:get_active()
598 self.active = self.active or self:get(self.current)
599 assert(self.active, "Invalid state")
601 local stat = FORM_DONE
602 if type(self.active) ~= "function" then
603 self.active:populate_delegator(self)
604 stat = self.active:parse()
609 if stat > FORM_PROCEED then
610 if Map.formvalue(self, "cbi.delg.back") then
611 newcurrent = self:get_prev(self.current)
613 newcurrent = self:get_next(self.current)
615 elseif stat < FORM_PROCEED then
620 if not Map.formvalue(self, "cbi.submit") then
622 elseif stat > FORM_PROCEED
623 and (not newcurrent or not self:get(newcurrent)) then
624 return self:_run_hook("on_done") or FORM_DONE
626 self.current = newcurrent or self.current
627 self.active = self:get(self.current)
628 if type(self.active) ~= "function" then
629 self.active:populate_delegator(self)
630 local stat = self.active:parse(false)
631 if stat == FORM_SKIP then
632 return self:parse(...)
637 return self:parse(...)
642 function Delegator.get_next(self, state)
643 for k, v in ipairs(self.chain) do
645 return self.chain[k+1]
650 function Delegator.get_prev(self, state)
651 for k, v in ipairs(self.chain) do
653 return self.chain[k-1]
658 function Delegator.get_chain(self)
659 local x = Map.formvalue(self, "cbi.delg.path") or self.defaultpath
660 return type(x) == "table" and x or {x}
663 function Delegator.get_active(self)
664 return Map.formvalue(self, "cbi.delg.current") or self.chain[1]
672 Page.__init__ = Node.__init__
673 Page.parse = function() end
677 SimpleForm - A Simple non-UCI form
679 SimpleForm = class(Node)
681 function SimpleForm.__init__(self, config, title, description, data)
682 Node.__init__(self, title, description)
684 self.data = data or {}
685 self.template = "cbi/simpleform"
687 self.pageaction = false
688 self.readinput = true
691 SimpleForm.formvalue = Map.formvalue
692 SimpleForm.formvaluetable = Map.formvaluetable
694 function SimpleForm.parse(self, readinput, ...)
695 self.readinput = (readinput ~= false)
697 if self:formvalue("cbi.skip") then
701 if self:formvalue("cbi.cancel") and self:_run_hooks("on_cancel") then
705 if self:submitstate() then
706 Node.parse(self, 1, ...)
710 for k, j in ipairs(self.children) do
711 for i, v in ipairs(j.children) do
713 and (not v.tag_missing or not v.tag_missing[1])
714 and (not v.tag_invalid or not v.tag_invalid[1])
720 not self:submitstate() and FORM_NODATA
721 or valid and FORM_VALID
724 self.dorender = not self.handle
726 local nrender, nstate = self:handle(state, self.data)
727 self.dorender = self.dorender or (nrender ~= false)
728 state = nstate or state
733 function SimpleForm.render(self, ...)
734 if self.dorender then
735 Node.render(self, ...)
739 function SimpleForm.submitstate(self)
740 return self:formvalue("cbi.submit")
743 function SimpleForm.section(self, class, ...)
744 if instanceof(class, AbstractSection) then
745 local obj = class(self, ...)
749 error("class must be a descendent of AbstractSection")
753 -- Creates a child field
754 function SimpleForm.field(self, class, ...)
756 for k, v in ipairs(self.children) do
757 if instanceof(v, SimpleSection) then
763 section = self:section(SimpleSection)
766 if instanceof(class, AbstractValue) then
767 local obj = class(self, section, ...)
768 obj.track_missing = true
772 error("class must be a descendent of AbstractValue")
776 function SimpleForm.set(self, section, option, value)
777 self.data[option] = value
781 function SimpleForm.del(self, section, option)
782 self.data[option] = nil
786 function SimpleForm.get(self, section, option)
787 return self.data[option]
791 function SimpleForm.get_scheme()
796 Form = class(SimpleForm)
798 function Form.__init__(self, ...)
799 SimpleForm.__init__(self, ...)
807 AbstractSection = class(Node)
809 function AbstractSection.__init__(self, map, sectiontype, ...)
810 Node.__init__(self, ...)
811 self.sectiontype = sectiontype
813 self.config = map.config
818 self.tag_invalid = {}
819 self.tag_deperror = {}
823 self.addremove = false
827 -- Define a tab for the section
828 function AbstractSection.tab(self, tab, title, desc)
829 self.tabs = self.tabs or { }
830 self.tab_names = self.tab_names or { }
832 self.tab_names[#self.tab_names+1] = tab
840 -- Check whether the section has tabs
841 function AbstractSection.has_tabs(self)
842 return (self.tabs ~= nil) and (next(self.tabs) ~= nil)
845 -- Appends a new option
846 function AbstractSection.option(self, class, option, ...)
847 if instanceof(class, AbstractValue) then
848 local obj = class(self.map, self, option, ...)
850 self.fields[option] = obj
852 elseif class == true then
853 error("No valid class was given and autodetection failed.")
855 error("class must be a descendant of AbstractValue")
859 -- Appends a new tabbed option
860 function AbstractSection.taboption(self, tab, ...)
862 assert(tab and self.tabs and self.tabs[tab],
863 "Cannot assign option to not existing tab %q" % tostring(tab))
865 local l = self.tabs[tab].childs
866 local o = AbstractSection.option(self, ...)
868 if o then l[#l+1] = o end
873 -- Render a single tab
874 function AbstractSection.render_tab(self, tab, ...)
876 assert(tab and self.tabs and self.tabs[tab],
877 "Cannot render not existing tab %q" % tostring(tab))
880 for k, node in ipairs(self.tabs[tab].childs) do
881 node.last_child = (k == #self.tabs[tab].childs)
886 -- Parse optional options
887 function AbstractSection.parse_optionals(self, section)
888 if not self.optional then
892 self.optionals[section] = {}
894 local field = self.map:formvalue("cbi.opt."..self.config.."."..section)
895 for k,v in ipairs(self.children) do
896 if v.optional and not v:cfgvalue(section) and not self:has_tabs() then
897 if field == v.option then
899 self.map.proceed = true
901 table.insert(self.optionals[section], v)
906 if field and #field > 0 and self.dynamic then
907 self:add_dynamic(field)
911 -- Add a dynamic option
912 function AbstractSection.add_dynamic(self, field, optional)
913 local o = self:option(Value, field, field)
914 o.optional = optional
917 -- Parse all dynamic options
918 function AbstractSection.parse_dynamic(self, section)
919 if not self.dynamic then
923 local arr = luci.util.clone(self:cfgvalue(section))
924 local form = self.map:formvaluetable("cbid."..self.config.."."..section)
925 for k, v in pairs(form) do
929 for key,val in pairs(arr) do
932 for i,c in ipairs(self.children) do
933 if c.option == key then
938 if create and key:sub(1, 1) ~= "." then
939 self.map.proceed = true
940 self:add_dynamic(key, true)
945 -- Returns the section's UCI table
946 function AbstractSection.cfgvalue(self, section)
947 return self.map:get(section)
951 function AbstractSection.push_events(self)
952 --luci.util.append(self.map.events, self.events)
953 self.map.changed = true
956 -- Removes the section
957 function AbstractSection.remove(self, section)
958 self.map.proceed = true
959 return self.map:del(section)
962 -- Creates the section
963 function AbstractSection.create(self, section)
967 stat = section:match("^[%w_]+$") and self.map:set(section, nil, self.sectiontype)
969 section = self.map:add(self.sectiontype)
974 for k,v in pairs(self.children) do
976 self.map:set(section, v.option, v.default)
980 for k,v in pairs(self.defaults) do
981 self.map:set(section, k, v)
985 self.map.proceed = true
991 SimpleSection = class(AbstractSection)
993 function SimpleSection.__init__(self, form, ...)
994 AbstractSection.__init__(self, form, nil, ...)
995 self.template = "cbi/nullsection"
999 Table = class(AbstractSection)
1001 function Table.__init__(self, form, data, ...)
1002 local datasource = {}
1004 datasource.config = "table"
1005 self.data = data or {}
1007 datasource.formvalue = Map.formvalue
1008 datasource.formvaluetable = Map.formvaluetable
1009 datasource.readinput = true
1011 function datasource.get(self, section, option)
1012 return tself.data[section] and tself.data[section][option]
1015 function datasource.submitstate(self)
1016 return Map.formvalue(self, "cbi.submit")
1019 function datasource.del(...)
1023 function datasource.get_scheme()
1027 AbstractSection.__init__(self, datasource, "table", ...)
1028 self.template = "cbi/tblsection"
1029 self.rowcolors = true
1030 self.anonymous = true
1033 function Table.parse(self, readinput)
1034 self.map.readinput = (readinput ~= false)
1035 for i, k in ipairs(self:cfgsections()) do
1036 if self.map:submitstate() then
1042 function Table.cfgsections(self)
1045 for i, v in luci.util.kspairs(self.data) do
1046 table.insert(sections, i)
1052 function Table.update(self, data)
1059 NamedSection - A fixed configuration section defined by its name
1061 NamedSection = class(AbstractSection)
1063 function NamedSection.__init__(self, map, section, stype, ...)
1064 AbstractSection.__init__(self, map, stype, ...)
1067 self.addremove = false
1068 self.template = "cbi/nsection"
1069 self.section = section
1072 function NamedSection.parse(self, novld)
1073 local s = self.section
1074 local active = self:cfgvalue(s)
1076 if self.addremove then
1077 local path = self.config.."."..s
1078 if active then -- Remove the section
1079 if self.map:formvalue("cbi.rns."..path) and self:remove(s) then
1083 else -- Create and apply default values
1084 if self.map:formvalue("cbi.cns."..path) then
1092 AbstractSection.parse_dynamic(self, s)
1093 if self.map:submitstate() then
1096 AbstractSection.parse_optionals(self, s)
1098 if self.changed then
1106 TypedSection - A (set of) configuration section(s) defined by the type
1107 addremove: Defines whether the user can add/remove sections of this type
1108 anonymous: Allow creating anonymous sections
1109 validate: a validation function returning nil if the section is invalid
1111 TypedSection = class(AbstractSection)
1113 function TypedSection.__init__(self, map, type, ...)
1114 AbstractSection.__init__(self, map, type, ...)
1116 self.template = "cbi/tsection"
1118 self.anonymous = false
1121 -- Return all matching UCI sections for this TypedSection
1122 function TypedSection.cfgsections(self)
1124 self.map.uci:foreach(self.map.config, self.sectiontype,
1126 if self:checkscope(section[".name"]) then
1127 table.insert(sections, section[".name"])
1134 -- Limits scope to sections that have certain option => value pairs
1135 function TypedSection.depends(self, option, value)
1136 table.insert(self.deps, {option=option, value=value})
1139 function TypedSection.parse(self, novld)
1140 if self.addremove then
1142 local crval = REMOVE_PREFIX .. self.config
1143 local name = self.map:formvaluetable(crval)
1144 for k,v in pairs(name) do
1145 if k:sub(-2) == ".x" then
1146 k = k:sub(1, #k - 2)
1148 if self:cfgvalue(k) and self:checkscope(k) then
1155 for i, k in ipairs(self:cfgsections()) do
1156 AbstractSection.parse_dynamic(self, k)
1157 if self.map:submitstate() then
1158 Node.parse(self, k, novld)
1160 AbstractSection.parse_optionals(self, k)
1163 if self.addremove then
1166 local crval = CREATE_PREFIX .. self.config .. "." .. self.sectiontype
1167 local origin, name = next(self.map:formvaluetable(crval))
1168 if self.anonymous then
1170 created = self:create(nil, origin)
1174 -- Ignore if it already exists
1175 if self:cfgvalue(name) then
1179 name = self:checkscope(name)
1182 self.err_invalid = true
1185 if name and #name > 0 then
1186 created = self:create(name, origin) and name
1188 self.invalid_cts = true
1195 AbstractSection.parse_optionals(self, created)
1199 if self.sortable then
1200 local stval = RESORT_PREFIX .. self.config .. "." .. self.sectiontype
1201 local order = self.map:formvalue(stval)
1202 if order and #order > 0 then
1205 for sid in util.imatch(order) do
1206 self.map.uci:reorder(self.config, sid, num)
1209 self.changed = (num > 0)
1213 if created or self.changed then
1218 -- Verifies scope of sections
1219 function TypedSection.checkscope(self, section)
1220 -- Check if we are not excluded
1221 if self.filter and not self:filter(section) then
1225 -- Check if at least one dependency is met
1226 if #self.deps > 0 and self:cfgvalue(section) then
1229 for k, v in ipairs(self.deps) do
1230 if self:cfgvalue(section)[v.option] == v.value then
1240 return self:validate(section)
1244 -- Dummy validate function
1245 function TypedSection.validate(self, section)
1251 AbstractValue - An abstract Value Type
1252 null: Value can be empty
1253 valid: A function returning the value if it is valid otherwise nil
1254 depends: A table of option => value pairs of which one must be true
1255 default: The default value
1256 size: The size of the input fields
1257 rmempty: Unset value if empty
1258 optional: This value is optional (see AbstractSection.optionals)
1260 AbstractValue = class(Node)
1262 function AbstractValue.__init__(self, map, section, option, ...)
1263 Node.__init__(self, ...)
1264 self.section = section
1265 self.option = option
1267 self.config = map.config
1268 self.tag_invalid = {}
1269 self.tag_missing = {}
1270 self.tag_reqerror = {}
1274 --self.cast = "string"
1276 self.track_missing = false
1280 self.optional = false
1283 function AbstractValue.prepare(self)
1284 self.cast = self.cast or "string"
1287 -- Add a dependencie to another section field
1288 function AbstractValue.depends(self, field, value)
1290 if type(field) == "string" then
1297 table.insert(self.deps, {deps=deps, add=""})
1300 -- Generates the unique CBID
1301 function AbstractValue.cbid(self, section)
1302 return "cbid."..self.map.config.."."..section.."."..self.option
1305 -- Return whether this object should be created
1306 function AbstractValue.formcreated(self, section)
1307 local key = "cbi.opt."..self.config.."."..section
1308 return (self.map:formvalue(key) == self.option)
1311 -- Returns the formvalue for this object
1312 function AbstractValue.formvalue(self, section)
1313 return self.map:formvalue(self:cbid(section))
1316 function AbstractValue.additional(self, value)
1317 self.optional = value
1320 function AbstractValue.mandatory(self, value)
1321 self.rmempty = not value
1324 function AbstractValue.add_error(self, section, type, msg)
1325 self.error = self.error or { }
1326 self.error[section] = msg or type
1328 self.section.error = self.section.error or { }
1329 self.section.error[section] = self.section.error[section] or { }
1330 table.insert(self.section.error[section], msg or type)
1332 if type == "invalid" then
1333 self.tag_invalid[section] = true
1334 elseif type == "missing" then
1335 self.tag_missing[section] = true
1338 self.tag_error[section] = true
1339 self.map.save = false
1342 function AbstractValue.parse(self, section, novld)
1343 local fvalue = self:formvalue(section)
1344 local cvalue = self:cfgvalue(section)
1346 -- If favlue and cvalue are both tables and have the same content
1347 -- make them identical
1348 if type(fvalue) == "table" and type(cvalue) == "table" then
1349 local equal = #fvalue == #cvalue
1352 if cvalue[i] ~= fvalue[i] then
1362 if fvalue and #fvalue > 0 then -- If we have a form value, write it to UCI
1364 fvalue, val_err = self:validate(fvalue, section)
1365 fvalue = self:transform(fvalue)
1367 if not fvalue and not novld then
1368 self:add_error(section, "invalid", val_err)
1371 if fvalue and (self.forcewrite or not (fvalue == cvalue)) then
1372 if self:write(section, fvalue) then
1374 self.section.changed = true
1375 --luci.util.append(self.map.events, self.events)
1378 else -- Unset the UCI or error
1379 if self.rmempty or self.optional then
1380 if self:remove(section) then
1382 self.section.changed = true
1383 --luci.util.append(self.map.events, self.events)
1385 elseif cvalue ~= fvalue and not novld then
1386 -- trigger validator with nil value to get custom user error msg.
1387 local _, val_err = self:validate(nil, section)
1388 self:add_error(section, "missing", val_err)
1393 -- Render if this value exists or if it is mandatory
1394 function AbstractValue.render(self, s, scope)
1395 if not self.optional or self.section:has_tabs() or self:cfgvalue(s) or self:formcreated(s) then
1398 scope.cbid = self:cbid(s)
1399 Node.render(self, scope)
1403 -- Return the UCI value of this object
1404 function AbstractValue.cfgvalue(self, section)
1406 if self.tag_error[section] then
1407 value = self:formvalue(section)
1409 value = self.map:get(section, self.option)
1414 elseif not self.cast or self.cast == type(value) then
1416 elseif self.cast == "string" then
1417 if type(value) == "table" then
1420 elseif self.cast == "table" then
1425 -- Validate the form value
1426 function AbstractValue.validate(self, value)
1427 if self.datatype and value then
1428 if type(value) == "table" then
1430 for _, v in ipairs(value) do
1431 if v and #v > 0 and not verify_datatype(self.datatype, v) then
1436 if not verify_datatype(self.datatype, value) then
1445 AbstractValue.transform = AbstractValue.validate
1449 function AbstractValue.write(self, section, value)
1450 return self.map:set(section, self.option, value)
1454 function AbstractValue.remove(self, section)
1455 return self.map:del(section, self.option)
1462 Value - A one-line value
1463 maxlength: The maximum length
1465 Value = class(AbstractValue)
1467 function Value.__init__(self, ...)
1468 AbstractValue.__init__(self, ...)
1469 self.template = "cbi/value"
1474 function Value.reset_values(self)
1479 function Value.value(self, key, val)
1481 table.insert(self.keylist, tostring(key))
1482 table.insert(self.vallist, tostring(val))
1486 -- DummyValue - This does nothing except being there
1487 DummyValue = class(AbstractValue)
1489 function DummyValue.__init__(self, ...)
1490 AbstractValue.__init__(self, ...)
1491 self.template = "cbi/dvalue"
1495 function DummyValue.cfgvalue(self, section)
1498 if type(self.value) == "function" then
1499 value = self:value(section)
1504 value = AbstractValue.cfgvalue(self, section)
1509 function DummyValue.parse(self)
1515 Flag - A flag being enabled or disabled
1517 Flag = class(AbstractValue)
1519 function Flag.__init__(self, ...)
1520 AbstractValue.__init__(self, ...)
1521 self.template = "cbi/fvalue"
1525 self.default = self.disabled
1528 -- A flag can only have two states: set or unset
1529 function Flag.parse(self, section)
1530 local fexists = self.map:formvalue(
1531 FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
1534 local fvalue = self:formvalue(section) and self.enabled or self.disabled
1535 if fvalue ~= self.default or (not self.optional and not self.rmempty) then
1536 self:write(section, fvalue)
1538 self:remove(section)
1541 self:remove(section)
1545 function Flag.cfgvalue(self, section)
1546 return AbstractValue.cfgvalue(self, section) or self.default
1551 ListValue - A one-line value predefined in a list
1552 widget: The widget that will be used (select, radio)
1554 ListValue = class(AbstractValue)
1556 function ListValue.__init__(self, ...)
1557 AbstractValue.__init__(self, ...)
1558 self.template = "cbi/lvalue"
1563 self.widget = "select"
1566 function ListValue.reset_values(self)
1571 function ListValue.value(self, key, val, ...)
1572 if luci.util.contains(self.keylist, key) then
1577 table.insert(self.keylist, tostring(key))
1578 table.insert(self.vallist, tostring(val))
1580 for i, deps in ipairs({...}) do
1581 self.subdeps[#self.subdeps + 1] = {add = "-"..key, deps=deps}
1585 function ListValue.validate(self, val)
1586 if luci.util.contains(self.keylist, val) then
1596 MultiValue - Multiple delimited values
1597 widget: The widget that will be used (select, checkbox)
1598 delimiter: The delimiter that will separate the values (default: " ")
1600 MultiValue = class(AbstractValue)
1602 function MultiValue.__init__(self, ...)
1603 AbstractValue.__init__(self, ...)
1604 self.template = "cbi/mvalue"
1609 self.widget = "checkbox"
1610 self.delimiter = " "
1613 function MultiValue.render(self, ...)
1614 if self.widget == "select" and not self.size then
1615 self.size = #self.vallist
1618 AbstractValue.render(self, ...)
1621 function MultiValue.reset_values(self)
1626 function MultiValue.value(self, key, val)
1627 if luci.util.contains(self.keylist, key) then
1632 table.insert(self.keylist, tostring(key))
1633 table.insert(self.vallist, tostring(val))
1636 function MultiValue.valuelist(self, section)
1637 local val = self:cfgvalue(section)
1639 if not(type(val) == "string") then
1643 return luci.util.split(val, self.delimiter)
1646 function MultiValue.validate(self, val)
1647 val = (type(val) == "table") and val or {val}
1651 for i, value in ipairs(val) do
1652 if luci.util.contains(self.keylist, value) then
1653 result = result and (result .. self.delimiter .. value) or value
1661 StaticList = class(MultiValue)
1663 function StaticList.__init__(self, ...)
1664 MultiValue.__init__(self, ...)
1666 self.valuelist = self.cfgvalue
1668 if not self.override_scheme
1669 and self.map:get_scheme(self.section.sectiontype, self.option) then
1670 local vs = self.map:get_scheme(self.section.sectiontype, self.option)
1671 if self.value and vs.values and not self.override_values then
1672 for k, v in pairs(vs.values) do
1679 function StaticList.validate(self, value)
1680 value = (type(value) == "table") and value or {value}
1683 for i, v in ipairs(value) do
1684 if luci.util.contains(self.keylist, v) then
1685 table.insert(valid, v)
1692 DynamicList = class(AbstractValue)
1694 function DynamicList.__init__(self, ...)
1695 AbstractValue.__init__(self, ...)
1696 self.template = "cbi/dynlist"
1702 function DynamicList.reset_values(self)
1707 function DynamicList.value(self, key, val)
1709 table.insert(self.keylist, tostring(key))
1710 table.insert(self.vallist, tostring(val))
1713 function DynamicList.write(self, section, value)
1716 if type(value) == "table" then
1718 for _, x in ipairs(value) do
1719 if x and #x > 0 then
1727 if self.cast == "string" then
1728 value = table.concat(t, " ")
1733 return AbstractValue.write(self, section, value)
1736 function DynamicList.cfgvalue(self, section)
1737 local value = AbstractValue.cfgvalue(self, section)
1739 if type(value) == "string" then
1742 for x in value:gmatch("%S+") do
1753 function DynamicList.formvalue(self, section)
1754 local value = AbstractValue.formvalue(self, section)
1756 if type(value) == "string" then
1757 if self.cast == "string" then
1760 for x in value:gmatch("%S+") do
1774 TextValue - A multi-line value
1777 TextValue = class(AbstractValue)
1779 function TextValue.__init__(self, ...)
1780 AbstractValue.__init__(self, ...)
1781 self.template = "cbi/tvalue"
1787 Button = class(AbstractValue)
1789 function Button.__init__(self, ...)
1790 AbstractValue.__init__(self, ...)
1791 self.template = "cbi/button"
1792 self.inputstyle = nil
1797 FileUpload = class(AbstractValue)
1799 function FileUpload.__init__(self, ...)
1800 AbstractValue.__init__(self, ...)
1801 self.template = "cbi/upload"
1802 if not self.map.upload_fields then
1803 self.map.upload_fields = { self }
1805 self.map.upload_fields[#self.map.upload_fields+1] = self
1809 function FileUpload.formcreated(self, section)
1810 return AbstractValue.formcreated(self, section) or
1811 self.map:formvalue("cbi.rlf."..section.."."..self.option) or
1812 self.map:formvalue("cbi.rlf."..section.."."..self.option..".x")
1815 function FileUpload.cfgvalue(self, section)
1816 local val = AbstractValue.cfgvalue(self, section)
1817 if val and fs.access(val) then
1823 function FileUpload.formvalue(self, section)
1824 local val = AbstractValue.formvalue(self, section)
1826 if not self.map:formvalue("cbi.rlf."..section.."."..self.option) and
1827 not self.map:formvalue("cbi.rlf."..section.."."..self.option..".x")
1837 function FileUpload.remove(self, section)
1838 local val = AbstractValue.formvalue(self, section)
1839 if val and fs.access(val) then fs.unlink(val) end
1840 return AbstractValue.remove(self, section)
1844 FileBrowser = class(AbstractValue)
1846 function FileBrowser.__init__(self, ...)
1847 AbstractValue.__init__(self, ...)
1848 self.template = "cbi/browser"