Update integer and float input widgets validation rules
authorThierry Florac <thierry.florac@onf.fr>
Mon, 29 Oct 2018 18:15:17 +0100
changeset 164 134b148fad1f
parent 163 ea1abec0bb55
child 165 80cec44f47ad
Update integer and float input widgets validation rules
src/pyams_form/widget/__init__.py
--- a/src/pyams_form/widget/__init__.py	Fri Oct 26 14:28:50 2018 +0200
+++ b/src/pyams_form/widget/__init__.py	Mon Oct 29 18:15:17 2018 +0100
@@ -306,6 +306,10 @@
 # Integer field widget
 #
 
+SIGN_BEFORE_VALUE = (1, 3)
+SIGN_AFTER_VALUE = (2, 4)
+
+
 @adapter_config(context=(IInt, IIntegerWidget), provides=IDataConverter)
 class IntegerFieldDataConverter(BaseDataConverter):
     """Integer field data converter"""
@@ -333,11 +337,17 @@
 
     @property
     def validate_rules(self):
-        rules = {'pattern': '\-?\d+'}
-        if self.field.min is not None:
-            rules['min'] = self.field.min
-        if self.field.max is not None:
-            rules['max'] = self.field.max
+        conv = locale.localeconv()
+        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 '')}
         return json.dumps(rules)
 
 
@@ -380,9 +390,17 @@
     @property
     def validate_rules(self):
         conv = locale.localeconv()
-        rules = {'pattern': '\{}?[\d{}]+{}?\d*'.format(conv.get('negative_sign', '-'),
-                                                       conv.get('mon_thousands_sep', ''),
-                                                       conv.get('decimal_point', '\.'))}
+        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 '')}
         return json.dumps(rules)