# HG changeset patch # User Thierry Florac # Date 1540833317 -3600 # Node ID 134b148fad1f77be67a54b4bb301b56ad392234f # Parent ea1abec0bb55f9d94b76142c8341ba427c1c4dd7 Update integer and float input widgets validation rules diff -r ea1abec0bb55 -r 134b148fad1f 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)