# HG changeset patch # User Kim Alvefur # Date 1533314100 -7200 # Node ID 572b6858db03e0d7ee4911442114f9932ada4c54 # Parent 5d3639e415bd5c0da4dcf29965a381b47ed5e7a5 util.dataforms: Detach generation of options from values (fixes #1177) diff -r 5d3639e415bd -r 572b6858db03 util/dataforms.lua --- a/util/dataforms.lua Sat Jun 30 20:49:26 2018 +0200 +++ b/util/dataforms.lua Fri Aug 03 18:35:00 2018 +0200 @@ -49,6 +49,27 @@ 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 @@ 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