fix: improve handling of boolean form fields
This commit is contained in:
parent
2503836ef5
commit
43ad0ae1c1
|
@ -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:
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -6,6 +6,6 @@
|
|||
v-model="${vmodel}"
|
||||
native-value="true"
|
||||
tal:attributes="attributes|field.widget.attributes|{};">
|
||||
{{ ${vmodel} }}
|
||||
{{ ${vmodel} ? "Yes" : "No" }}
|
||||
</b-checkbox>
|
||||
</div>
|
||||
|
|
4
src/wuttaweb/templates/deform/readonly/checkbox.pt
Normal file
4
src/wuttaweb/templates/deform/readonly/checkbox.pt
Normal 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>
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in a new issue