# HG changeset patch # User Kim Alvefur # Date 1635254157 -7200 # Node ID 593b141ba01cd933d92c66d94d22bcd4644d93d8 # Parent 52a1b885044eae8acdb46fff04fe63b6f78af936 util.dataforms: Coerce number values for boolean fields Makes more sense than coercing to a string, which would always be truthy. diff -r 52a1b885044e -r 593b141ba01c spec/util_dataforms_spec.lua --- a/spec/util_dataforms_spec.lua Tue Oct 26 13:35:04 2021 +0200 +++ b/spec/util_dataforms_spec.lua Tue Oct 26 15:15:57 2021 +0200 @@ -417,6 +417,16 @@ 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 -r 52a1b885044e -r 593b141ba01c util/dataforms.lua --- a/util/dataforms.lua Tue Oct 26 13:35:04 2021 +0200 +++ b/util/dataforms.lua Tue Oct 26 15:15:57 2021 +0200 @@ -103,8 +103,11 @@ 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