+ to_python=(lambda x: Currency(float(x) * 100)),
+ from_python=Currency.__str_no_dollar__),
+ validators.Regex(r'^\-?[0-9]*(\.[0-9]{2})?$'))
+
+# This is virtually copied from formencode.validator.FieldsMatch, but
+# I wanted my own version for fields that shouldn't match
+class FieldsDontMatch(validators.FormValidator):
+ """
+ Tests that the given fields do not match.
+ """
+
+ show_match = False
+ field_names = None
+ valid_partial_form = True
+ __unpackargs__ = ('*', 'field_names')
+
+ messages = {
+ 'invalid': "Fields match"
+ }
+
+ def validate_partial(self, field_dict, state):
+ for name in self.field_names:
+ if not field_dict.has_key(name):
+ return
+ self.validate_python(field_dict, state)
+
+ def validate_python(self, field_dict, state):
+ errors = {}
+ for ref_index, ref in enumerate(self.field_names):
+ for name in self.field_names[ref_index+1:]:
+ if field_dict.get(name, '') == field_dict.get(ref, ''):
+ errors[name] = self.message('invalid', state)
+ if errors:
+ error_list = errors.items()
+ error_list.sort()
+ error_message = '<br>\n'.join(
+ ['%s: %s' % (name, value) for name, value in error_list])
+ raise validators.Invalid(error_message,
+ field_dict, state,
+ error_dict=errors)