I am using FormEncode in Python http://formencode.readthedocs.org/en/latest/modules/national.html#module-formencode.national.
I am trying to add a custom postal code validator for Brazil. I've read the documentation, but none of it seems to be helping. Does anyone have any idea on how to approach this?
In case anyone wants to know how to solve this using FormEncode, this is the solution I came up with
class BrazilZipValidator(validators.Regex):
messages = {
'invalid': _('Please enter a valid code (nnnnn-nnn or nnnnnnnn)')
}
regex = re.compile(r'^([0-9]{8})$|^([0-9]{5}-[0-9]{3})$')
strip = True
def _to_python(self, value, state):
self.assert_string(value, state)
match = self.regex.search(value)
if not match:
raise formencode.Invalid(self.message('invalid', state), value, \
state)
return match.group()
Then attach this class to the validation object
national.PostalCodeInCountryFormat._vd.update({'BR': BrazilZipValidator})