forms: add support for boolean fields in blocks (#45165)
This commit is contained in:
parent
62c0c20d24
commit
2a7fe34ed5
|
@ -8364,6 +8364,43 @@ def test_block_date(pub, blocks_feature):
|
|||
assert '>2020-06-16<' in resp
|
||||
|
||||
|
||||
def test_block_bool(pub, blocks_feature):
|
||||
create_user(pub)
|
||||
FormDef.wipe()
|
||||
BlockDef.wipe()
|
||||
|
||||
block = BlockDef()
|
||||
block.name = 'foobar'
|
||||
block.fields = [
|
||||
fields.StringField(id='123', required=True, label='Test', type='string'),
|
||||
fields.BoolField(id='234', required=False, label='Test2', type='bool'),
|
||||
]
|
||||
block.store()
|
||||
|
||||
formdef = FormDef()
|
||||
formdef.name = 'form title'
|
||||
formdef.fields = [
|
||||
fields.BlockField(id='1', label='test', type='block:foobar'),
|
||||
]
|
||||
formdef.store()
|
||||
|
||||
for value in (True, False):
|
||||
app = get_app(pub)
|
||||
resp = app.get(formdef.get_url())
|
||||
resp.form['f1$element0$f123'] = 'foo'
|
||||
resp.form['f1$element0$f234'].checked = value
|
||||
resp = resp.form.submit('submit') # -> validation page
|
||||
assert resp.form['f1$element0$f234disabled'].checked is value
|
||||
assert resp.form['f1$element0$f234'].value == str(value)
|
||||
assert 'Check values then click submit.' in resp
|
||||
resp = resp.form.submit('submit') # -> submit
|
||||
resp = resp.follow()
|
||||
if value:
|
||||
assert '<div class="value">Yes</div>' in resp
|
||||
else:
|
||||
assert '<div class="value">No</div>' in resp
|
||||
|
||||
|
||||
def test_block_autocomplete_list(pub, blocks_feature):
|
||||
create_user(pub)
|
||||
FormDef.wipe()
|
||||
|
|
|
@ -953,6 +953,8 @@ class BoolField(WidgetField):
|
|||
kwargs['disabled'] = 'disabled'
|
||||
value = get_request().get_field(self.field_key)
|
||||
form.add_hidden(self.field_key, value=str(value))
|
||||
widget = form.get_widget(self.field_key)
|
||||
widget.field = self
|
||||
if value and not value == 'False':
|
||||
self.field_key = 'f%sdisabled' % self.id
|
||||
get_request().form[self.field_key] = 'yes'
|
||||
|
|
|
@ -524,6 +524,9 @@ class CommentWidget(Widget):
|
|||
class CompositeWidget(quixote.form.CompositeWidget):
|
||||
content_extra_attributes = {'role': 'group'}
|
||||
|
||||
def add_hidden(self, name, value=None, **kwargs):
|
||||
self.add(HiddenWidget, name, value, **kwargs)
|
||||
|
||||
def transfer_form_value(self, request):
|
||||
for widget in self.get_widgets():
|
||||
widget.transfer_form_value(request)
|
||||
|
|
Loading…
Reference in New Issue