Comparison

util/dataforms.lua @ 2061:e34fdca432a9

util.dataforms: Only add value to rendered form if supplied in the data
author Florian Zeitz
date Fri, 30 Oct 2009 01:18:56 +0000
parent 1958:e2b0026143c4
child 2070:25dc4b9d06b1
comparison
equal deleted inserted replaced
2060:b23295b5428a 2061:e34fdca432a9
36 -- Add field tag 36 -- Add field tag
37 form:tag("field", { type = field_type, var = field.name, label = field.label }); 37 form:tag("field", { type = field_type, var = field.name, label = field.label });
38 38
39 local value = (data and data[field.name]) or field.value; 39 local value = (data and data[field.name]) or field.value;
40 40
41 -- Add value, depending on type 41 if value then
42 if field_type == "hidden" then 42 -- Add value, depending on type
43 if type(value) == "table" then 43 if field_type == "hidden" then
44 -- Assume an XML snippet 44 if type(value) == "table" then
45 form:tag("value") 45 -- Assume an XML snippet
46 :add_child(value) 46 form:tag("value")
47 :up(); 47 :add_child(value)
48 elseif value then 48 :up();
49 form:tag("value"):text(tostring(value)):up();
50 end
51 elseif field_type == "boolean" then
52 form:tag("value"):text((value and "1") or "0"):up();
53 elseif field_type == "fixed" then
54
55 elseif field_type == "jid-multi" then
56 for _, jid in ipairs(value) do
57 form:tag("value"):text(jid):up();
58 end
59 elseif field_type == "jid-single" then
60 form:tag("value"):text(value):up();
61 elseif field_type == "text-single" or field_type == "text-private" then
62 form:tag("value"):text(value):up();
63 elseif field_type == "text-multi" then
64 -- Split into multiple <value> tags, one for each line
65 for line in value:gmatch("([^\r\n]+)\r?\n*") do
66 form:tag("value"):text(line):up();
67 end
68 elseif field_type == "list-single" then
69 for _, val in ipairs(value) do
70 if type(val) == "table" then
71 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
72 else 49 else
73 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up(); 50 form:tag("value"):text(tostring(value)):up();
51 end
52 elseif field_type == "boolean" then
53 form:tag("value"):text((value and "1") or "0"):up();
54 elseif field_type == "fixed" then
55
56 elseif field_type == "jid-multi" then
57 for _, jid in ipairs(value) do
58 form:tag("value"):text(jid):up();
59 end
60 elseif field_type == "jid-single" then
61 form:tag("value"):text(value):up();
62 elseif field_type == "text-single" or field_type == "text-private" then
63 form:tag("value"):text(value):up();
64 elseif field_type == "text-multi" then
65 -- Split into multiple <value> tags, one for each line
66 for line in value:gmatch("([^\r\n]+)\r?\n*") do
67 form:tag("value"):text(line):up();
68 end
69 elseif field_type == "list-single" then
70 for _, val in ipairs(value) do
71 if type(val) == "table" then
72 form:tag("option", { label = val.label }):tag("value"):text(val.value):up():up();
73 else
74 form:tag("option", { label= val }):tag("value"):text(tostring(val)):up():up();
75 end
74 end 76 end
75 end 77 end
76 end 78 end
77 79
78 if field.required then 80 if field.required then