aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--spec/util_dataforms_spec.lua10
-rw-r--r--util/dataforms.lua7
2 files changed, 15 insertions, 2 deletions
diff --git a/spec/util_dataforms_spec.lua b/spec/util_dataforms_spec.lua
index f796b30d..a76a9565 100644
--- a/spec/util_dataforms_spec.lua
+++ b/spec/util_dataforms_spec.lua
@@ -417,6 +417,16 @@ describe("util.dataforms", function ()
end);
end);
+ describe("number handling", function()
+ it("handles numbers as booleans", function()
+ local f = dataforms.new { { name = "boolean"; type = "boolean" } };
+ local x = f:form({ boolean = 0 });
+ assert.equal("0", x:find "field/value#");
+ x = f:form({ boolean = 1 });
+ assert.equal("1", x:find "field/value#");
+ end);
+ end)
+
describe("datatype validation", function ()
local f = dataforms.new {
{
diff --git a/util/dataforms.lua b/util/dataforms.lua
index 79559b22..7bb3cff9 100644
--- a/util/dataforms.lua
+++ b/util/dataforms.lua
@@ -103,8 +103,11 @@ function form_t.form(layout, data, formtype)
if value ~= nil then
if type(value) == "number" then
- -- TODO validate that this is ok somehow, eg check field.datatype
- value = ("%g"):format(value);
+ if field_type == "boolean" then
+ value = value ~= 0;
+ else
+ value = ("%g"):format(value);
+ end
end
-- Add value, depending on type
if field_type == "hidden" then