From 43ad0ae1c1936923299d88e85b4e1981b78fa55c Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 23 Aug 2024 20:38:46 -0500 Subject: [PATCH] fix: improve handling of boolean form fields --- src/wuttaweb/forms/base.py | 11 ++++++++++- src/wuttaweb/forms/widgets.py | 3 ++- src/wuttaweb/templates/deform/checkbox.pt | 2 +- src/wuttaweb/templates/deform/readonly/checkbox.pt | 4 ++++ tests/forms/test_base.py | 10 ++++++++++ 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 src/wuttaweb/templates/deform/readonly/checkbox.pt diff --git a/src/wuttaweb/forms/base.py b/src/wuttaweb/forms/base.py index 4e415ca..ceaeeb7 100644 --- a/src/wuttaweb/forms/base.py +++ b/src/wuttaweb/forms/base.py @@ -995,7 +995,16 @@ class Form: model_data = {} def assign(field): - model_data[field.oid] = make_json_safe(field.cstruct) + value = field.cstruct + + # TODO: we need a proper true/false on the Vue side, + # but deform/colander want 'true' and 'false' ..so + # for now we explicitly translate here, ugh. also + # note this does not yet allow for null values.. :( + if isinstance(field.typ, colander.Boolean): + value = True if field.typ.true_val else False + + model_data[field.oid] = make_json_safe(value) for key in self.fields: diff --git a/src/wuttaweb/forms/widgets.py b/src/wuttaweb/forms/widgets.py index 6a7bc51..6c77fe0 100644 --- a/src/wuttaweb/forms/widgets.py +++ b/src/wuttaweb/forms/widgets.py @@ -33,6 +33,7 @@ in the namespace: * :class:`deform:deform.widget.TextAreaWidget` * :class:`deform:deform.widget.PasswordWidget` * :class:`deform:deform.widget.CheckedPasswordWidget` +* :class:`deform:deform.widget.CheckboxWidget` * :class:`deform:deform.widget.SelectWidget` * :class:`deform:deform.widget.CheckboxChoiceWidget` * :class:`deform:deform.widget.MoneyInputWidget` @@ -41,7 +42,7 @@ in the namespace: import colander from deform.widget import (Widget, TextInputWidget, TextAreaWidget, PasswordWidget, CheckedPasswordWidget, - SelectWidget, CheckboxChoiceWidget, + CheckboxWidget, SelectWidget, CheckboxChoiceWidget, MoneyInputWidget) from webhelpers2.html import HTML diff --git a/src/wuttaweb/templates/deform/checkbox.pt b/src/wuttaweb/templates/deform/checkbox.pt index 92c9f62..c4536e8 100644 --- a/src/wuttaweb/templates/deform/checkbox.pt +++ b/src/wuttaweb/templates/deform/checkbox.pt @@ -6,6 +6,6 @@ v-model="${vmodel}" native-value="true" tal:attributes="attributes|field.widget.attributes|{};"> - {{ ${vmodel} }} + {{ ${vmodel} ? "Yes" : "No" }} diff --git a/src/wuttaweb/templates/deform/readonly/checkbox.pt b/src/wuttaweb/templates/deform/readonly/checkbox.pt new file mode 100644 index 0000000..4988213 --- /dev/null +++ b/src/wuttaweb/templates/deform/readonly/checkbox.pt @@ -0,0 +1,4 @@ + + Yes + No + diff --git a/tests/forms/test_base.py b/tests/forms/test_base.py index 1fb02ec..73e9d85 100644 --- a/tests/forms/test_base.py +++ b/tests/forms/test_base.py @@ -520,6 +520,16 @@ class TestForm(TestCase): data = form.get_vue_model_data() self.assertEqual(len(data), 2) + # confirm bool values make it thru as-is + schema.add(colander.SchemaNode(colander.Bool(), name='baz')) + form = self.make_form(schema=schema, model_instance={ + 'foo': 'one', + 'bar': 'two', + 'baz': True, + }) + data = form.get_vue_model_data() + self.assertEqual(list(data.values()), ['one', 'two', True]) + def test_get_field_errors(self): schema = self.make_schema()