fields: fix live prefill for checkbox fields (#57860)
gitea-wip/wcs/pipeline/head There was a failure building this commit Details

This commit is contained in:
Lauréline Guérin 2021-10-14 16:20:27 +02:00
parent 713a6ccc82
commit 3e2476ea1c
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 68 additions and 2 deletions

View File

@ -799,6 +799,68 @@ def test_field_live_string_prefill(pub, http_requests):
live_resp = app.post('/foo/live?modified_field_id=user', params=resp.form.submit_fields(), status=403)
def test_field_live_bool_prefill(pub, http_requests):
CardDef.wipe()
carddef = CardDef()
carddef.name = 'foo'
carddef.digest_templates = {'default': '{{ form_var_foo }}'}
carddef.fields = [
fields.StringField(id='1', type='string', label='string', varname='foo'),
fields.BoolField(
type='bool',
id='2',
varname='bool',
),
]
carddef.store()
carddef.data_class().wipe()
carddata1 = carddef.data_class()()
carddata1.data = {
'1': 'bar',
'2': True,
}
carddata1.just_created()
carddata1.store()
carddata2 = carddef.data_class()()
carddata2.data = {
'1': 'baz',
'2': False,
}
carddata2.just_created()
carddata2.store()
FormDef.wipe()
formdef = FormDef()
formdef.name = 'Foo'
formdef.fields = [
fields.ItemField(
type='item', id='1', label='foo', varname='foo', data_source={'type': 'carddef:foo'}
),
fields.BoolField(
type='bool',
id='2',
label='bool',
varname='bool',
prefill={'type': 'string', 'value': '{{ form_var_foo_live_var_bool }}'},
),
]
formdef.store()
formdef.data_class().wipe()
app = get_app(pub)
resp = app.get('/foo/')
assert resp.html.find('div', {'data-field-id': '1'}).attrs['data-live-source'] == 'true'
assert resp.pyquery('#var_bool.widget-prefilled') # second field is marked as prefilled
assert resp.form['f2'].value is None
resp.form['f1'] = str(carddata1.id)
live_resp = app.post('/foo/live?modified_field_id=1&prefilled_2=on', params=resp.form.submit_fields())
assert live_resp.json['result']['2'] == {'visible': True, 'content': True}
resp.form['f2'] = False # manually changed -> widget-prefilled class will be removed
resp.form['f1'] = str(carddata2.id)
live_resp = app.post('/foo/live?modified_field_id=1', params=resp.form.submit_fields())
assert live_resp.json['result']['2'] == {'visible': True}
def test_field_live_block_string_prefill(pub, http_requests):
FormDef.wipe()
BlockDef.wipe()

View File

@ -798,6 +798,8 @@ class FormStatusPage(Directory, FormTemplateMixin):
update_prefill = bool('prefilled_%s' % field.id in get_request().form)
if update_prefill:
value = field.get_prefill_value()[0]
if field.key == 'bool':
value = field.convert_value_from_str(value)
entry['content'] = value
elif field.prefill and field.prefill.get('type') == 'user':
update_prefill = bool(get_request().form.get('modified_field_id') == 'user')

View File

@ -471,15 +471,17 @@ $(function() {
}
$select.trigger('wcs:options-change', {items: value.items});
}
if (value.content) {
if (typeof value.content !== 'undefined') {
$widget.each(function(idx, widget) {
if ($widget.hasClass('comment-field')) {
// replace comment content
$widget.html(value.content);
} else {
// replace text input value
if ($(widget).is('.widget-prefilled') || $(widget).is('.widget-readonly') || data.modified_field == 'user') {
// replace text input value
$(widget).find('input[type=text], input[type=tel], input[type=numeric], input[type=email], textarea').val(value.content);
// replace checkbox input value
$(widget).find('input[type=checkbox]').prop('checked', value.content);
}
}
});