# HG changeset patch # User Matthew Wild # Date 1238855552 -3600 # Node ID 4b9207949735c4e2f84956c0fdd57ea7f832c692 # Parent a00b4269de70acf382dffb839b741b0d3117a444 util.dataforms: Fixed to actually work, mostly diff -r a00b4269de70 -r 4b9207949735 util/dataforms.lua --- a/util/dataforms.lua Fri Apr 03 01:29:59 2009 +0100 +++ b/util/dataforms.lua Sat Apr 04 15:32:32 2009 +0100 @@ -1,3 +1,6 @@ +local setmetatable = setmetatable; +local pairs, ipairs = pairs, ipairs; +local st = require "util.stanza"; module "dataforms" @@ -13,11 +16,17 @@ local form_x_attr = { xmlns = xmlns_forms }; function form_t.form(layout, data) - local form = st.tag("x", form_x_attr); + local form = st.stanza("x", form_x_attr); + if layout.title then + form:tag("title"):text(layout.title):up(); + end + if layout.instructions then + form:tag("instructions"):text(layout.instructions):up(); + end for n, field in ipairs(layout) do - local field_type = field.type; + local field_type = field.type or "text-single"; -- Add field tag - form:tag("field", { type = field_type, var = field.name }); + form:tag("field", { type = field_type, var = field.name, label = field.label }); local value = data[field.name]; @@ -30,7 +39,7 @@ form:text(tostring(value)); end elseif field_type == "boolean" then - form:text((value and "1") or "0"); + form:tag("value"):text((value and "1") or "0"); elseif field_type == "fixed" then elseif field_type == "jid-multi" then @@ -39,7 +48,13 @@ end elseif field_type == "jid-single" then form:tag("value"):text(value):up(); - + elseif field_type == "text-single" or field_type == "text-private" then + form:tag("value"):text(value):up(); + elseif field_type == "text-multi" then + -- Split into multiple tags, one for each line + for line in value:gmatch("([^\r\n]+)\r?\n*") do + form:tag("value"):text(line):up(); + end end -- Jump back up to list of fields @@ -52,9 +67,10 @@ end +return _M; ---[[ +--[=[ Layout: { @@ -67,4 +83,4 @@ } ---]] +--]=]