From a9e02830807b2aac576aa80ca0c912af7c9a66e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 14 Oct 2021 16:20:27 +0200
Subject: [PATCH] fields: fix live prefill for checkbox fields (#57860)

---
 tests/form_pages/test_live.py        | 62 ++++++++++++++++++++++++++++
 wcs/forms/common.py                  |  2 +
 wcs/qommon/static/js/qommon.forms.js |  6 ++-
 3 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/tests/form_pages/test_live.py b/tests/form_pages/test_live.py
index 774212ea..b946ea36 100644
--- a/tests/form_pages/test_live.py
+++ b/tests/form_pages/test_live.py
@@ -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()
diff --git a/wcs/forms/common.py b/wcs/forms/common.py
index 6a98af6c..c3907700 100644
--- a/wcs/forms/common.py
+++ b/wcs/forms/common.py
@@ -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')
diff --git a/wcs/qommon/static/js/qommon.forms.js b/wcs/qommon/static/js/qommon.forms.js
index e03bf9f0..459e955b 100644
--- a/wcs/qommon/static/js/qommon.forms.js
+++ b/wcs/qommon/static/js/qommon.forms.js
@@ -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);
                 }
               }
             });
-- 
2.33.0

