Changeset

955:98ca5a753fee

util.dataforms: Support for retriving the field value data from forms
author Matthew Wild <mwild1@gmail.com>
date Sat, 04 Apr 2009 18:08:35 +0100
parents 954:72e4639c9310
children 956:4c3f3d60a2f4
files util/dataforms.lua
diffstat 1 files changed, 55 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/util/dataforms.lua	Sat Apr 04 18:07:48 2009 +0100
+++ b/util/dataforms.lua	Sat Apr 04 18:08:35 2009 +0100
@@ -72,10 +72,65 @@
 	return form;
 end
 
+local field_readers = {};
+
 function form_t.data(layout, stanza)
+	local data = {};
 	
+	for field_tag in stanza:childtags() do
+		local field_type = field_tag.attr.type;
+		
+		local reader = field_readers[field_type];
+		if reader then
+			data[field_tag.attr.var] = reader(field_tag);
+		end
+		
+	end
+	return data;
 end
 
+field_readers["text-single"] = 
+	function (field_tag)
+		local value = field_tag:child_with_name("value");
+		if value then
+			return value[1];
+		end
+	end
+
+field_readers["text-private"] = 
+	field_readers["text-single"];
+
+field_readers["text-multi"] = 
+	function (field_tag)
+		local result = {};
+		for value_tag in field_tag:childtags() do
+			if value_tag.name == "value" then
+				result[#result+1] = value_tag[1];
+			end
+		end
+		return t_concat(result, "\n");
+	end
+
+field_readers["boolean"] = 
+	function (field_tag)
+		local value = field_tag:child_with_name("value");
+		if value then
+			if value[1] == "1" or value[1] == "true" then
+				return true;
+			else
+				return false;
+			end
+		end		
+	end
+
+field_readers["hidden"] = 
+	function (field_tag)
+		local value = field_tag:child_with_name("value");
+		if value then
+			return value[1];
+		end
+	end
+	
 return _M;