aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2018-08-03 18:35:00 +0200
committerKim Alvefur <zash@zash.se>2018-08-03 18:35:00 +0200
commit8de8df075dee9065b278153427e5fb6cbb5b25d7 (patch)
tree40e54d5f9e6e706b35851653ce7c14c9341e3939 /util
parent211253c16c4c150dc2d5a56341d8a28029ac468b (diff)
downloadprosody-8de8df075dee9065b278153427e5fb6cbb5b25d7.tar.gz
prosody-8de8df075dee9065b278153427e5fb6cbb5b25d7.zip
util.dataforms: Detach generation of options from values (fixes #1177)
Diffstat (limited to 'util')
-rw-r--r--util/dataforms.lua51
1 files changed, 21 insertions, 30 deletions
diff --git a/util/dataforms.lua b/util/dataforms.lua
index 6152c9f3..a5826d91 100644
--- a/util/dataforms.lua
+++ b/util/dataforms.lua
@@ -49,6 +49,27 @@ function form_t.form(layout, data, formtype)
value = field.value;
end
+ if formtype ~= "result" and field.options then
+ local defaults = {};
+ for _, val in ipairs(field.options) do
+ if type(val) == "table" then
+ form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
+ if val.default then
+ defaults[#defaults+1] = val.value;
+ end
+ else
+ form:tag("option", { label= val }):tag("value"):text(val):up():up();
+ end
+ end
+ if not value then
+ if field_type == "list-single" then
+ value = defaults[1];
+ elseif field_type == "list-multi" then
+ value = defaults;
+ end
+ end
+ end
+
if value ~= nil then
-- Add value, depending on type
if field_type == "hidden" then
@@ -78,40 +99,10 @@ function form_t.form(layout, data, formtype)
form:tag("value"):text(line):up();
end
elseif field_type == "list-single" then
- if formtype ~= "result" then
- local has_default = false;
- for _, val in ipairs(field.options or value) do
- if type(val) == "table" then
- form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
- if value == val.value or val.default and (not has_default) then
- form:tag("value"):text(val.value):up();
- has_default = true;
- end
- else
- form:tag("option", { label= val }):tag("value"):text(val):up():up();
- end
- end
- end
- if (field.options or formtype == "result") and value then
form:tag("value"):text(value):up();
- end
elseif field_type == "list-multi" then
- if formtype ~= "result" then
- for _, val in ipairs(field.options or value) do
- if type(val) == "table" then
- form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
- if not field.options and val.default then
- form:tag("value"):text(val.value):up();
- end
- else
- form:tag("option", { label= val }):tag("value"):text(val):up():up();
- end
- end
- end
- if (field.options or formtype == "result") and value then
for _, val in ipairs(value) do
form:tag("value"):text(val):up();
- end
end
end
end