formencode.national – Country specific validators

Country specific validators for use with FormEncode.

Module Contents

Note

To use CountryValidator and LanguageValidator, install either pycountry or TurboGears Version 1.x.

Country, State, and Postal Codes

class formencode.national.DelimitedDigitsPostalCode(partition_lengths, delimiter=None, strict=False, *args, **kw)

Abstraction of common postal code formats, such as 55555, 55-555 etc. With constant amount of digits. By providing a single digit as partition you can obtain a trivial ‘x digits’ postal code validator.

For flexibility, input may use additional delimiters or delimters in a bad position. Only the minimum (or if strict, exact) number of digits has to be provided.

>>> german = DelimitedDigitsPostalCode(5)
>>> german.to_python('55555')
'55555'
>>> german.to_python('55 55-5')
'55555'
>>> german.to_python('5555')
Traceback (most recent call last):
    ...
Invalid: Please enter a zip code (5 digits)
>>> polish = DelimitedDigitsPostalCode([2, 3], '-')
>>> polish.to_python('55555')
'55-555'
>>> polish.to_python('55-555')
'55-555'
>>> polish.to_python('555-55')
'55-555'
>>> polish.to_python('5555')
Traceback (most recent call last):
    ...
Invalid: Please enter a zip code (nn-nnn)
>>> nicaragua = DelimitedDigitsPostalCode([3, 3, 1], '-')
>>> nicaragua.to_python('5554443')
'555-444-3'
>>> nicaragua.to_python('555-4443')
'555-444-3'
>>> nicaragua.to_python('5555')
Traceback (most recent call last):
    ...
Invalid: Please enter a zip code (nnn-nnn-n)

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

invalid:

Please enter a zip code (%(format)s)

noneType:

The input must be a string (not None)

formencode.national.USPostalCode(*args, **kw)

US Postal codes (aka Zip Codes).

>>> uspc = USPostalCode()
>>> uspc.to_python('55555')
'55555'
>>> uspc.to_python('55555-5555')
'55555-5555'
>>> uspc.to_python('5555')
Traceback (most recent call last):
    ...
Invalid: Please enter a zip code (5 digits)
formencode.national.GermanPostalCode(*args, **kw)
formencode.national.FourDigitsPostalCode(*args, **kw)
formencode.national.PolishPostalCode(*args, **kw)
class formencode.national.ArgentinianPostalCode(*args, **kw)

Argentinian Postal codes.

>>> ArgentinianPostalCode.to_python('C1070AAM')
'C1070AAM'
>>> ArgentinianPostalCode.to_python('c 1070 aam')
'C1070AAM'
>>> ArgentinianPostalCode.to_python('5555')
Traceback (most recent call last):
    ...
Invalid: Please enter a zip code (LnnnnLLL)

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

invalid:

Please enter a zip code (%(format)s)

noneType:

The input must be a string (not None)

class formencode.national.CanadianPostalCode(*args, **kw)

Canadian Postal codes.

>>> CanadianPostalCode.to_python('V3H 1Z7')
'V3H 1Z7'
>>> CanadianPostalCode.to_python('v3h1z7')
'V3H 1Z7'
>>> CanadianPostalCode.to_python('5555')
Traceback (most recent call last):
    ...
Invalid: Please enter a zip code (LnL nLn)

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

invalid:

Please enter a zip code (%(format)s)

noneType:

The input must be a string (not None)

class formencode.national.UKPostalCode(*args, **kw)

UK Postal codes. Please see BS 7666.

>>> UKPostalCode.to_python('BFPO 3')
'BFPO 3'
>>> UKPostalCode.to_python('LE11 3GR')
'LE11 3GR'
>>> UKPostalCode.to_python('l1a 3gr')
'L1A 3GR'
>>> UKPostalCode.to_python('5555')
Traceback (most recent call last):
    ...
Invalid: Please enter a valid postal code (for format see BS 7666)

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

invalid:

Please enter a valid postal code (for format see BS 7666)

noneType:

The input must be a string (not None)

class formencode.national.CountryValidator(*args, **kw)

Will convert a country’s name into its ISO-3166 abbreviation for unified storage in databases etc. and return a localized country name in the reverse step.

@See http://www.iso.org/iso/country_codes/iso_3166_code_lists.htm

>>> CountryValidator.to_python('Germany')
'DE'
>>> CountryValidator.to_python('Finland')
'FI'
>>> CountryValidator.to_python('UNITED STATES')
'US'
>>> CountryValidator.to_python('Krakovia')
Traceback (most recent call last):
    ...
Invalid: That country is not listed in ISO 3166
>>> CountryValidator.from_python('DE')
'Germany'
>>> CountryValidator.from_python('FI')
'Finland'

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

noneType:

The input must be a string (not None)

valueNotFound:

That country is not listed in ISO 3166

class formencode.national.PostalCodeInCountryFormat(*args, **kw)

Makes sure the postal code is in the country’s format by chosing postal code validator by provided country code. Does convert it into the preferred format, too.

>>> fs = PostalCodeInCountryFormat('country', 'zip')
>>> sorted(fs.to_python(dict(country='DE', zip='30167')).items())
[('country', 'DE'), ('zip', '30167')]
>>> fs.to_python(dict(country='DE', zip='3008'))
Traceback (most recent call last):
    ...
Invalid: Given postal code does not match the country's format.
>>> sorted(fs.to_python(dict(country='PL', zip='34343')).items())
[('country', 'PL'), ('zip', '34-343')]
>>> fs = PostalCodeInCountryFormat('staat', 'plz')
>>> sorted(fs.to_python(dict(staat='GB', plz='l1a 3gr')).items())
[('plz', 'L1A 3GR'), ('staat', 'GB')]

Messages

badFormat:

Given postal code does not match the country’s format.

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

noneType:

The input must be a string (not None)

class formencode.national.USStateProvince(*args, **kw)

Valid state or province code (two-letter).

Well, for now I don’t know the province codes, but it does state codes. Give your own states list to validate other state-like codes; give extra_states to add values without losing the current state values.

>>> s = USStateProvince('XX')
>>> s.to_python('IL')
'IL'
>>> s.to_python('XX')
'XX'
>>> s.to_python('xx')
'XX'
>>> s.to_python('YY')
Traceback (most recent call last):
    ...
Invalid: That is not a valid state code

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a state code

invalid:

That is not a valid state code

noneType:

The input must be a string (not None)

wrongLength:

Please enter a state code with TWO letters

Phones and Addresses

class formencode.national.USPhoneNumber(*args, **kw)

Validates, and converts to ###-###-####, optionally with extension (as ext.##…). Only support US phone numbers. See InternationalPhoneNumber for support for that kind of phone number.

>>> p = USPhoneNumber()
>>> p.to_python('333-3333')
Traceback (most recent call last):
    ...
Invalid: Please enter a number, with area code, in the form ###-###-####, optionally with "ext.####"
>>> p.to_python('555-555-5555')
'555-555-5555'
>>> p.to_python('1-393-555-3939')
'1-393-555-3939'
>>> p.to_python('321.555.4949')
'321.555.4949'
>>> p.to_python('3335550000')
'3335550000'

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

noneType:

The input must be a string (not None)

phoneFormat:

Please enter a number, with area code, in the form ###-###-####, optionally with “ext.####”

class formencode.national.InternationalPhoneNumber(*args, **kw)

Validates, and converts phone numbers to +##-###-#######. Adapted from RFC 3966

@param default_cc country code for prepending if none is provided

can be a paramerless callable

>>> c = InternationalPhoneNumber(default_cc=lambda: 49)
>>> c.to_python('0555/8114100')
'+49-555-8114100'
>>> p = InternationalPhoneNumber(default_cc=49)
>>> p.to_python('333-3333')
Traceback (most recent call last):
    ...
Invalid: Please enter a number, with area code, in the form +##-###-#######.
>>> p.to_python('0555/4860-300')
'+49-555-4860-300'
>>> p.to_python('0555-49924-51')
'+49-555-49924-51'
>>> p.to_python('0555 / 8114100')
'+49-555-8114100'
>>> p.to_python('0555/8114100')
'+49-555-8114100'
>>> p.to_python('0555 8114100')
'+49-555-8114100'
>>> p.to_python(' +49 (0)555 350 60 0')
'+49-555-35060-0'
>>> p.to_python('+49 555 350600')
'+49-555-350600'
>>> p.to_python('0049/ 555/ 871 82 96')
'+49-555-87182-96'
>>> p.to_python('0555-2 50-30')
'+49-555-250-30'
>>> p.to_python('0555 43-1200')
'+49-555-43-1200'
>>> p.to_python('(05 55)4 94 33 47')
'+49-555-49433-47'
>>> p.to_python('(00 48-555)2 31 72 41')
'+48-555-23172-41'
>>> p.to_python('+973-555431')
'+973-555431'
>>> p.to_python('1-393-555-3939')
'+1-393-555-3939'
>>> p.to_python('+43 (1) 55528/0')
'+43-1-55528-0'
>>> p.to_python('+43 5555 429 62-0')
'+43-5555-42962-0'
>>> p.to_python('00 218 55 33 50 317 321')
'+218-55-3350317-321'
>>> p.to_python('+218 (0)55-3636639/38')
'+218-55-3636639-38'
>>> p.to_python('032 555555 367')
'+49-32-555555-367'
>>> p.to_python('(+86) 555 3876693')
'+86-555-3876693'

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

noneType:

The input must be a string (not None)

phoneFormat:

Please enter a number, with area code, in the form +##-###-#######.

Language Codes and Names

class formencode.national.LanguageValidator(*args, **kw)

Converts a given language into its ISO 639 alpha 2 code, if there is any. Returns the language’s full name in the reverse.

Warning: ISO 639 neither differentiates between languages such as Cantonese and Mandarin nor does it contain all spoken languages. E.g., Lechitic languages are missing. Warning: ISO 639 is a smaller subset of ISO 639-2

@param key_ok accept the language’s code instead of its name for input

defaults to True

>>> l = LanguageValidator()
>>> l.to_python('German')
'de'
>>> l.to_python('Chinese')
'zh'
>>> l.to_python('Klingonian')
Traceback (most recent call last):
    ...
Invalid: That language is not listed in ISO 639
>>> l.from_python('de')
'German'
>>> l.from_python('zh')
'Chinese'

Messages

badType:

The input must be a string (not a %(type)s: %(value)r)

empty:

Please enter a value

noneType:

The input must be a string (not None)

valueNotFound:

That language is not listed in ISO 639