Commit e6631e4b authored by Tatuya Kamada's avatar Tatuya Kamada

Formulator: Add a FloatField validator to take account of the precision and the user input length.

parent eb5fdbe3
...@@ -103,6 +103,31 @@ ...@@ -103,6 +103,31 @@
<td>//button[@title=\'Save\']</td>\n <td>//button[@title=\'Save\']</td>\n
<td></td>\n <td></td>\n
</tr>\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 <tr>\n
<td>verifyPortalStatusMessage</td>\n <td>verifyPortalStatusMessage</td>\n
<td>Data updated.</td>\n <td>Data updated.</td>\n
...@@ -120,8 +145,7 @@ ...@@ -120,8 +145,7 @@
</tr>\n </tr>\n
</tbody></table>\n </tbody></table>\n
</body>\n </body>\n
</html>\n </html>
]]></unicode> </value> ]]></unicode> </value>
</item> </item>
......
713 714
\ No newline at end of file \ No newline at end of file
...@@ -231,6 +231,25 @@ class TestFloatField(ERP5TypeTestCase): ...@@ -231,6 +231,25 @@ class TestFloatField(ERP5TypeTestCase):
self.assertRaises(ValidationError, self.assertRaises(ValidationError,
self.validator.validate, self.field, 'field_test_field', self.portal.REQUEST) 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): def test_render_odt(self):
self.field.values['input_style'] = '-1 234.5' self.field.values['input_style'] = '-1 234.5'
self.field.values['default'] = 1000 self.field.values['default'] = 1000
......
...@@ -290,9 +290,27 @@ class IntegerValidator(StringBaseValidator): ...@@ -290,9 +290,27 @@ class IntegerValidator(StringBaseValidator):
IntegerValidatorInstance = IntegerValidator() IntegerValidatorInstance = IntegerValidator()
class FloatValidator(StringBaseValidator): 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." 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): def validate(self, field, key, REQUEST):
value = StringBaseValidator.validate(self, field, key, REQUEST) value = StringBaseValidator.validate(self, field, key, REQUEST)
...@@ -326,6 +344,7 @@ class FloatValidator(StringBaseValidator): ...@@ -326,6 +344,7 @@ class FloatValidator(StringBaseValidator):
value = value.replace(decimal_point, '.') value = value.replace(decimal_point, '.')
if value.find('%') >= 0: if value.find('%') >= 0:
value = value.replace('%', '') value = value.replace('%', '')
value = self._validatePrecision(field, value, decimal_point, input_style)
try: try:
value = float(value) value = float(value)
if input_style.find('%')>=0: if input_style.find('%')>=0:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment