Added widget adapter for decimal fields
authorThierry Florac <tflorac@ulthar.net>
Fri, 04 Oct 2019 13:37:08 +0200
changeset 196 893cf2088818
parent 195 e4b68da720e2
child 197 3d3dc44d9535
Added widget adapter for decimal fields
src/pyams_form/widget/__init__.py
--- a/src/pyams_form/widget/__init__.py	Fri Oct 04 12:54:42 2019 +0200
+++ b/src/pyams_form/widget/__init__.py	Fri Oct 04 13:37:08 2019 +0200
@@ -36,7 +36,7 @@
 from zope.pagetemplate.interfaces import IPageTemplate
 from zope.schema.fieldproperty import FieldProperty
 from zope.schema.interfaces import IBytes, IChoice, IDate, IDatetime, IFloat, IInt, IList, ISet, \
-    ITime, ITuple
+    ITime, ITuple, IDecimal
 
 from pyams_form.interfaces.form import IActionWidget, ICloseWidget, IColorWidget, IDateWidget, \
     IDatetimeWidget, IFloatWidget, IFormLayer, IHTMLWidget, IIntegerWidget, IJSONDictWidget, \
@@ -348,7 +348,7 @@
         try:
             return locale.atoi(value)
         except ValueError:
-            raise FormatterValidationError(self.errorMessage, value)
+            raise FormatterValidationError(self.error_message, value)
 
 
 @widgettemplate_config(mode=INPUT_MODE, template='templates/integer-input.pt', layer=IFormLayer)
@@ -362,13 +362,12 @@
         allow_negative = True
         if (self.field.min is not None) and (self.field.min >= 0):
             allow_negative = False
-        rules = {'pattern': '{}[\d{}]+{}'.format('\{}?'.format(conv.get('negative_sign', '-'))
-                                                     if (allow_negative and
-                                                         (conv.get('n_sign_posn') in SIGN_BEFORE_VALUE)) else '',
-                                                 conv.get('thousands_sep', ''),
-                                                 '\{}?'.format(conv.get('negative_sign', '-'))
-                                                     if (allow_negative and
-                                                         (conv.get('n_sign_posn') in SIGN_AFTER_VALUE)) else '')}
+        rules = {'pattern': '{}[\d{}]+{}'.format(
+            '\{}?'.format(conv.get('negative_sign', '-'))
+                if (allow_negative and (conv.get('n_sign_posn') in SIGN_BEFORE_VALUE)) else '',
+            conv.get('thousands_sep', ''),
+            '\{}?'.format(conv.get('negative_sign', '-'))
+                if (allow_negative and (conv.get('n_sign_posn') in SIGN_AFTER_VALUE)) else '')}
         return json.dumps(rules)
 
 
@@ -414,18 +413,18 @@
         allow_negative = True
         if (self.field.min is not None) and (self.field.min >= 0):
             allow_negative = False
-        rules = {'pattern': '{}[\d{}]+{}?\d*{}'.format('\{}?'.format(conv.get('negative_sign', '-'))
-                                                           if (allow_negative and
-                                                               (conv.get('n_sign_posn') in SIGN_BEFORE_VALUE)) else '',
-                                                       conv.get('thousands_sep', ''),
-                                                       conv.get('decimal_point', '\.'),
-                                                       '\{}?'.format(conv.get('negative_sign', '-'))
-                                                           if (allow_negative and
-                                                               (conv.get('n_sign_posn') in SIGN_AFTER_VALUE)) else '')}
+        rules = {'pattern': '{}[\d{}]+{}?\d*{}'.format(
+            '\{}?'.format(conv.get('negative_sign', '-'))
+                if (allow_negative and (conv.get('n_sign_posn') in SIGN_BEFORE_VALUE)) else '',
+            conv.get('thousands_sep', ''),
+            conv.get('decimal_point', '\.'),
+            '\{}?'.format(conv.get('negative_sign', '-'))
+                if (allow_negative and (conv.get('n_sign_posn') in SIGN_AFTER_VALUE)) else '')}
         return json.dumps(rules)
 
 
 @adapter_config(context=(IFloat, IFormLayer), provides=IFieldWidget)
+@adapter_config(context=(IDecimal, IFormLayer), provides=IFieldWidget)
 def FloatFieldWidget(field, request):
     return FieldWidget(field, FloatWidget(request))