diff --git a/bt5/erp5_ui_test/PathTemplateItem/portal_tests/float_field_zuite/testFloatField.xml b/bt5/erp5_ui_test/PathTemplateItem/portal_tests/float_field_zuite/testFloatField.xml index c06048b7ad833b95a9c1e30357fa02ad1808927f..18034698fcd5a85a132af6fda48a1664f5ee8482 100644 --- a/bt5/erp5_ui_test/PathTemplateItem/portal_tests/float_field_zuite/testFloatField.xml +++ b/bt5/erp5_ui_test/PathTemplateItem/portal_tests/float_field_zuite/testFloatField.xml @@ -103,6 +103,31 @@ <td>//button[@title=\'Save\']</td>\n <td></td>\n </tr>\n +<tr>\n + <td>verifyText</td>\n + <td>//div[@id="information_area"]</td>\n + <td>Input data has errors. Please look at the error messages below.</td>\n +</tr>\n +<tr>\n + <td>verifyText</td>\n + <td>//span[@class="error"]</td>\n + <td>The number you input has too large precision.</td>\n +</tr>\n +<tr>\n + <td>type</td>\n + <td>field_my_quantity</td>\n + <td>1000000000000.0</td>\n +</tr>\n +<tr>\n + <td>clickAndWait</td>\n + <td>//button[@title=\'Save\']</td>\n + <td></td>\n +</tr>\n +<tr>\n + <td>verifyPortalStatusMessage</td>\n + <td>Data updated.</td>\n + <td></td>\n +</tr>\n <tr>\n <td>verifyPortalStatusMessage</td>\n <td>Data updated.</td>\n @@ -120,8 +145,7 @@ </tr>\n </tbody></table>\n </body>\n -</html>\n - +</html> ]]></unicode> </value> </item> diff --git a/bt5/erp5_ui_test/bt/revision b/bt5/erp5_ui_test/bt/revision index 7bd7739249784375437eaeb74301105a8ad9f459..50ae88076af348a8f3cb426174f40415dc680115 100644 --- a/bt5/erp5_ui_test/bt/revision +++ b/bt5/erp5_ui_test/bt/revision @@ -1 +1 @@ -713 \ No newline at end of file +714 \ No newline at end of file diff --git a/product/ERP5Form/tests/testFields.py b/product/ERP5Form/tests/testFields.py index 1db2dfbff52137808c5bb419bd58b786e8c5d2b7..c523a61f1461777c50ee992a6d256cd8508fa85f 100644 --- a/product/ERP5Form/tests/testFields.py +++ b/product/ERP5Form/tests/testFields.py @@ -231,6 +231,25 @@ class TestFloatField(ERP5TypeTestCase): self.assertRaises(ValidationError, self.validator.validate, self.field, 'field_test_field', self.portal.REQUEST) + def test_validate_precision0(self): + # Check the consistency among the precision and user inputs + self.field.values['input_style'] = '-1,234.5' + self.field.values['precision'] = 0 + self.portal.REQUEST.set('field_test_field', '1.00') + self.assertRaises(ValidationError, + self.validator.validate, self.field, 'field_test_field', + self.portal.REQUEST) + + def test_validate_precision0_with_percent(self): + # Check the precision and user inputs when the style is '%' + self.field.values['input_style'] = '-12.5%' + self.field.values['precision'] = 1 + self.assertEqual('12.5%', self.widget.format_value(self.field, 0.125)) + self.portal.REQUEST.set('field_test_field', '0.1255') + self.assertRaises(ValidationError, + self.validator.validate, self.field, 'field_test_field', + self.portal.REQUEST) + def test_render_odt(self): self.field.values['input_style'] = '-1 234.5' self.field.values['default'] = 1000 diff --git a/product/Formulator/Validator.py b/product/Formulator/Validator.py index e884ff5b0a77307d971e985b7db5c5359865a08a..5b84cbd11e144c89e0f3424d465ba810844caccf 100644 --- a/product/Formulator/Validator.py +++ b/product/Formulator/Validator.py @@ -290,9 +290,27 @@ class IntegerValidator(StringBaseValidator): IntegerValidatorInstance = IntegerValidator() class FloatValidator(StringBaseValidator): - message_names = StringBaseValidator.message_names + ['not_float'] + message_names = StringBaseValidator.message_names + ['not_float', + 'too_large_precision'] not_float = "You did not enter a floating point number." + too_large_precision = "The number you input has too large precision." + + def _validatePrecision(self, field, value, decimal_point, input_style): + """ Validate the consistency among the precision and the user inputs """ + if not field.has_value('precision'): + return value + precision = field.get_value('precision') + if precision == '' or precision is None: + # need to validate when the precision is 0 + return value + index = value.find(decimal_point) + if index < 0: + return value + input_precision_length = len(value[index+1:]) + if input_precision_length > int(precision): + self.raise_error('too_large_precision', field) + return value def validate(self, field, key, REQUEST): value = StringBaseValidator.validate(self, field, key, REQUEST) @@ -326,6 +344,7 @@ class FloatValidator(StringBaseValidator): value = value.replace(decimal_point, '.') if value.find('%') >= 0: value = value.replace('%', '') + value = self._validatePrecision(field, value, decimal_point, input_style) try: value = float(value) if input_style.find('%')>=0: