2
0
Fork 0

fix: improve handling of boolean form fields

This commit is contained in:
Lance Edgar 2024-08-23 20:38:46 -05:00
parent 2503836ef5
commit 43ad0ae1c1
5 changed files with 27 additions and 3 deletions

View file

@ -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:

View file

@ -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

View file

@ -6,6 +6,6 @@
v-model="${vmodel}"
native-value="true"
tal:attributes="attributes|field.widget.attributes|{};">
{{ ${vmodel} }}
{{ ${vmodel} ? "Yes" : "No" }}
</b-checkbox>
</div>

View file

@ -0,0 +1,4 @@
<tal:omit tal:define="true_val true_val|field.widget.true_val;">
<span tal:condition="cstruct == true_val">Yes</span>
<span tal:condition="cstruct != true_val">No</span>
</tal:omit>

View file

@ -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()