diff options
author | Kim Alvefur <zash@zash.se> | 2018-09-01 01:24:46 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2018-09-01 01:24:46 +0200 |
commit | 367e73a610e687ae01a6987023683d3ea9eac805 (patch) | |
tree | 044e31819102a37413083044bffde12de008df5c | |
parent | 55a6e42c6485ee2fdbdc6173c05d30b5ad8002ae (diff) | |
download | prosody-367e73a610e687ae01a6987023683d3ea9eac805.tar.gz prosody-367e73a610e687ae01a6987023683d3ea9eac805.zip |
util.dataforms: Allow field names to be different from the 'var' attribute
This should allow the usage of long prefixes and namespace-like names to
be contained to the XML representation of the form, so that the code can
use more convenient names.
-rw-r--r-- | spec/util_dataforms_spec.lua | 15 | ||||
-rw-r--r-- | util/dataforms.lua | 4 |
2 files changed, 17 insertions, 2 deletions
diff --git a/spec/util_dataforms_spec.lua b/spec/util_dataforms_spec.lua index 863a3290..123b9f8a 100644 --- a/spec/util_dataforms_spec.lua +++ b/spec/util_dataforms_spec.lua @@ -386,5 +386,20 @@ describe("util.dataforms", function () assert.same(expect, data, "got back the same data"); end); end); + + describe("field 'var' property", function () + it("works as expected", function () + local f = dataforms.new { + { + var = "someprefix#the-field", + name = "the_field", + type = "text-single", + } + }; + local x = f:form({the_field = "hello"}); + assert.equal("someprefix#the-field", x:find"field@var"); + assert.equal("hello", x:find"field/value#"); + end); + end); end); diff --git a/util/dataforms.lua b/util/dataforms.lua index a5733b83..3ac18697 100644 --- a/util/dataforms.lua +++ b/util/dataforms.lua @@ -42,7 +42,7 @@ function form_t.form(layout, data, formtype) for _, field in ipairs(layout) do local field_type = field.type or "text-single"; -- Add field tag - form:tag("field", { type = field_type, var = field.name, label = formtype ~= "submit" and field.label or nil }); + form:tag("field", { type = field_type, var = field.var or field.name, label = formtype ~= "submit" and field.label or nil }); if formtype ~= "submit" then if field.desc then @@ -150,7 +150,7 @@ function form_t.data(layout, stanza, current) for _, field in ipairs(layout) do local tag; for field_tag in stanza:childtags("field") do - if field.name == field_tag.attr.var then + if (field.var or field.name) == field_tag.attr.var then tag = field_tag; break; end |