aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2018-08-05 15:50:06 +0200
committerKim Alvefur <zash@zash.se>2018-08-05 15:50:06 +0200
commit876b96659c284c4ab0cb3bbbfe0f8abb7b634562 (patch)
treeab6500efb03d437a820995c696ab9ef95f9d8ca0
parent553b67ccd09ff5706194a1a7e7d75172948da6bf (diff)
downloadprosody-876b96659c284c4ab0cb3bbbfe0f8abb7b634562.tar.gz
prosody-876b96659c284c4ab0cb3bbbfe0f8abb7b634562.zip
util.dataforms: Allow passing the current values to be used in stead of omitted fields
-rw-r--r--spec/util_dataforms_spec.lua39
-rw-r--r--util/dataforms.lua6
2 files changed, 43 insertions, 2 deletions
diff --git a/spec/util_dataforms_spec.lua b/spec/util_dataforms_spec.lua
index 66236e96..863a3290 100644
--- a/spec/util_dataforms_spec.lua
+++ b/spec/util_dataforms_spec.lua
@@ -347,5 +347,44 @@ describe("util.dataforms", function ()
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);
diff --git a/util/dataforms.lua b/util/dataforms.lua
index a75e8db0..a5733b83 100644
--- a/util/dataforms.lua
+++ b/util/dataforms.lua
@@ -142,7 +142,7 @@ end
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 @@ function form_t.data(layout, stanza)
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