Changeset

9121:e5eb36ee07a2

util.dataforms: Allow passing the current values to be used in stead of omitted fields
author Kim Alvefur <zash@zash.se>
date Sun, 05 Aug 2018 15:50:06 +0200
parents 9120:01fc8ba0a7fa
children 9122:a69dc9c729ec
files spec/util_dataforms_spec.lua util/dataforms.lua
diffstat 2 files changed, 43 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_dataforms_spec.lua	Sun Aug 05 19:12:49 2018 +0200
+++ b/spec/util_dataforms_spec.lua	Sun Aug 05 15:50:06 2018 +0200
@@ -347,5 +347,44 @@
 			assert.truthy(f:find("field/option"));
 		end);
 	end);
+
+	describe("using current values in place of missing fields", function ()
+		it("gets back the previous values when given an empty form", function ()
+			local current = {
+				["list-multi-field"] = {
+					"list-multi-option-value#2";
+				};
+				["list-single-field"] = "list-single-value#2";
+				["hidden-field"] = "hidden-value";
+				["boolean-field"] = false;
+				["text-multi-field"] = "words\ngo\nhere";
+				["jid-single-field"] = "alice@example.com";
+				["text-private-field"] = "hunter2";
+				["text-single-field"] = "text-single-value";
+				["jid-multi-field"] = {
+					"bob@example.net";
+				};
+			};
+			local expect = {
+				-- FORM_TYPE = "xmpp:prosody.im/spec/util.dataforms#1"; -- does this need to be included?
+				["list-multi-field"] = {
+					"list-multi-option-value#2";
+				};
+				["list-single-field"] = "list-single-value#2";
+				["hidden-field"] = "hidden-value";
+				["boolean-field"] = false;
+				["text-multi-field"] = "words\ngo\nhere";
+				["jid-single-field"] = "alice@example.com";
+				["text-private-field"] = "hunter2";
+				["text-single-field"] = "text-single-value";
+				["jid-multi-field"] = {
+					"bob@example.net";
+				};
+			};
+			local data, err = some_form:data(st.stanza("x", {xmlns="jabber:x:data"}), current);
+			assert.is.table(data, err);
+			assert.same(expect, data, "got back the same data");
+		end);
+	end);
 end);
 
--- a/util/dataforms.lua	Sun Aug 05 19:12:49 2018 +0200
+++ b/util/dataforms.lua	Sun Aug 05 15:50:06 2018 +0200
@@ -142,7 +142,7 @@
 
 local field_readers = {};
 
-function form_t.data(layout, stanza)
+function form_t.data(layout, stanza, current)
 	local data = {};
 	local errors = {};
 	local present = {};
@@ -157,7 +157,9 @@
 		end
 
 		if not tag then
-			if field.required then
+			if current and current[field.name] ~= nil then
+				data[field.name] = current[field.name];
+			elseif field.required then
 				errors[field.name] = "Required value missing";
 			end
 		elseif field.name then