Updated form's boolean fields management
authorThierry Florac <tflorac@ulthar.net>
Tue, 08 Oct 2019 17:14:54 +0200
changeset 481 b523fbdb0081
parent 480 b55212e53cdb
child 482 f15a620e5a5d
Updated form's boolean fields management
src/pyams_default_theme/shared/form/__init__.py
--- a/src/pyams_default_theme/shared/form/__init__.py	Tue Oct 08 17:14:12 2019 +0200
+++ b/src/pyams_default_theme/shared/form/__init__.py	Tue Oct 08 17:14:54 2019 +0200
@@ -93,8 +93,9 @@
                 yield captcha
             yield from IFormFieldContainer(self.context).get_fields()
             if self.context.rgpd_consent:
-                consent = Bool(title=II18n(self.context).query_attribute('rgpd_warning',
-                                                                         request=self.request),
+                consent = Bool(title=' ',
+                               description=II18n(self.context).query_attribute('rgpd_warning',
+                                                                               request=self.request),
                                required=True,
                                default=False)
                 consent.__name__ = RGPD_CONSENT_FIELD_NAME
@@ -113,24 +114,26 @@
 
     def updateWidgets(self, prefix=None):
         super(FormFieldContainerInputForm, self).updateWidgets(prefix)
+        request = self.request
         for widget in self.widgets.values():
             if widget.field.__name__ == CSRF_FIELD_NAME:
                 widget.name = CSRF_FIELD_NAME
                 widget.mode = HIDDEN_MODE
-                widget.value = get_csrf_token(self.request)
+                widget.value = get_csrf_token(request)
             elif widget.field.__name__ == RECAPTCHA_FIELD_NAME:
                 widget.name = RECAPTCHA_FIELD_NAME
                 widget.mode = HIDDEN_MODE
             elif widget.field.__name__ == RGPD_CONSENT_FIELD_NAME:
                 widget.name = RGPD_CONSENT_FIELD_NAME
+                widget.description = ' '
                 widget.required = False
                 user_rights = II18n(self.context).query_attribute('rgpd_user_rights',
-                                                                  request=self.request)
+                                                                  request=request)
                 widget.after_widget_notice = '<div><br />{0}</div>'.format(
                     text_to_html(user_rights, 'oid_to_href'))
                 widget.object_data = {
                     'ams-validate-messages': {
-                        'required': self.request.localizer.translate(
+                        'required': request.localizer.translate(
                             _("You can't submit this form without accepting data usage rules."))
                     }
                 }
@@ -138,8 +141,11 @@
             else:
                 field = IFormFieldContainer(self.context).get(widget.field.__name__)
                 if field is not None:
+                    # switch label and description for boolean fields
                     if field.field_type == 'bool':
-                        widget.label = ''
+                        widget.description = ' '
+                        widget.label = II18n(field).query_attribute('label',
+                                                                    request=request)
                     widget.placeholder = field.placeholder
 
     @button.buttonAndHandler('title', name='submit')
@@ -180,6 +186,8 @@
                     if converter is not None:
                         user_data[form_field.__name__] = converter.convert(
                             data.get(form_field.__name__))
+                    if not user_data[form_field.__name__]:
+                        user_data[form_field.__name__] = '--'
                 output = handler.handle(form, data, user_data)
                 if output:
                     request.annotations['form.output'] = output