forms: always add attributes to prefilled fields (#8223)

This commit is contained in:
Frédéric Péters 2015-09-09 13:40:52 +02:00
parent cca1cf925b
commit f1645fc610
2 changed files with 45 additions and 0 deletions

View File

@ -1085,3 +1085,44 @@ def test_formdata_form_file_download(pub):
# go back to the status page, this will exercise the substitution variables
# codepath.
resp = resp.follow()
def test_form_map_field_back_and_submit(pub):
formdef = create_formdef()
formdef.fields = [
fields.MapField(id='0', label='map'),
fields.StringField(id='1', label='street', required=False,
prefill={'type': 'geolocation', 'value': 'road'}),
]
formdef.store()
resp = get_app(pub).get('/test/')
formdef.data_class().wipe()
assert 'qommon.map.js' in resp.body
assert 'qommon.geolocation.js' in resp.body
# with a real user interaction this would get set by javascript
resp.forms[0]['f0$latlng'] = '1.234;-1.234'
assert 'data-geolocation="road"' in resp.body
# check summary page
resp = resp.forms[0].submit('submit')
assert 'Check values then click submit.' in resp.body
assert 'data-init-lng="-1.234"' in resp.body
assert 'data-init-lat="1.234"' in resp.body
# get back to the map field
resp = resp.forms[0].submit('previous')
# check the field is still marked as holding the road
assert 'data-geolocation="road"' in resp.body
assert resp.forms[0]['f0$latlng'].value == '1.234;-1.234'
# back to summary page
resp = resp.forms[0].submit('submit')
# and submitting the form
resp = resp.forms[0].submit('submit')
assert resp.status_int == 302
resp = resp.follow()
assert 'The form has been recorded' in resp.body
assert formdef.data_class().count() == 1
data_id = formdef.data_class().select()[0].id
data = formdef.data_class().get(data_id)
assert data.data == {'1': None, '0': '1.234;-1.234'}

View File

@ -388,6 +388,10 @@ class FormPage(Directory):
form.get_widget('f%s' % k).set_message(
_('Value has been automatically prefilled.'))
form.get_widget('f%s' % k).prefilled = True
if field.prefill:
# always set additional attributes as they will be used for
# "live prefill", regardless of existing data.
form.get_widget('f%s' % k).prefill_attributes = field.get_prefill_attributes()
if not prefilled and form.get_widget('f%s' % k):