Commits

James Crasta committed e59337e

Fix issue with AnyOf/NoneOf on checking non-string values. Fixes #100.

Comments (0)

Files changed (2)

tests/validators.py

         self.assertEqual(email()(self.form, DummyField('foo@bar456.info')), None)
         self.assertRaises(ValidationError, email(), self.form, DummyField(None))
         self.assertRaises(ValidationError, email(), self.form, DummyField(''))
+        self.assertRaises(ValidationError, email(), self.form, DummyField('  '))
         self.assertRaises(ValidationError, email(), self.form, DummyField('foo'))
         self.assertRaises(ValidationError, email(), self.form, DummyField('bar.dk'))
         self.assertRaises(ValidationError, email(), self.form, DummyField('foo@'))
         self.assertEqual(AnyOf(['a', 'b', 'c'])(self.form, DummyField('b')), None)
         self.assertRaises(ValueError, AnyOf(['a', 'b', 'c']), self.form, DummyField(None))
 
+        # Anyof in 1.0.1 failed on numbers for formatting the error with a TypeError
+        check_num = AnyOf([1,2,3])
+        self.assertEqual(check_num(self.form, DummyField(2)), None)
+        self.assertRaises(ValueError, check_num, self.form, DummyField(4))
+
+        # Test values_formatter
+        formatter = lambda values: '::'.join(unicode(x) for x in reversed(values))
+        checker = AnyOf([7,8,9], message='test %(values)s', values_formatter=formatter)
+        self.assertEqual(grab_error_message(checker, self.form, DummyField(4)), u'test 9::8::7')
+
     def test_none_of(self):
         self.assertEqual(NoneOf(['a', 'b', 'c'])(self.form, DummyField('d')), None)
         self.assertRaises(ValueError, NoneOf(['a', 'b', 'c']), self.form, DummyField('a'))

wtforms/validators.py

 
 import re
 
-from wtforms.compat import string_types
+from wtforms.compat import string_types, text_type
 
 __all__ = (
     'DataRequired', 'data_required', 'Email', 'email', 'EqualTo', 'equal_to',
         self.values = values
         self.message = message
         if values_formatter is None:
-            values_formatter = lambda v: ', '.join(v)
+            values_formatter = lambda v: ', '.join(text_type(x) for x in v)
         self.values_formatter = values_formatter
 
     def __call__(self, form, field):
             if self.message is None:
                 self.message = field.gettext('Invalid value, must be one of: %(values)s.')
 
-            raise ValueError(self.message % dict(values=self.values_formatter(self.values)))
+            raise ValidationError(self.message % dict(values=self.values_formatter(self.values)))
 
 
 class NoneOf(object):
         self.values = values
         self.message = message
         if values_formatter is None:
-            values_formatter = lambda v: ', '.join(v)
+            values_formatter = lambda v: ', '.join(text_type(x) for x in v)
         self.values_formatter = values_formatter
 
     def __call__(self, form, field):
             if self.message is None:
                 self.message = field.gettext('Invalid value, can\'t be any of: %(values)s.')
 
-            raise ValueError(self.message % dict(values=self.values_formatter(self.values)))
+            raise ValidationError(self.message % dict(values=self.values_formatter(self.values)))
 
 
 email = Email