2007-03-26 05:26:44 +08:00
|
|
|
"""
|
|
|
|
JP-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.core import validators
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms import ValidationError
|
2008-06-18 21:10:05 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms.fields import RegexField, Select
|
2007-03-26 05:26:44 +08:00
|
|
|
|
|
|
|
class JPPostalCodeField(RegexField):
|
|
|
|
"""
|
|
|
|
A form field that validates its input is a Japanese postcode.
|
|
|
|
|
|
|
|
Accepts 7 digits, with or without a hyphen.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _('Enter a postal code in the format XXXXXXX or XXX-XXXX.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2007-03-26 05:26:44 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(JPPostalCodeField, self).__init__(r'^\d{3}-\d{4}$|^\d{7}$',
|
2007-12-17 16:05:27 +08:00
|
|
|
max_length=None, min_length=None, *args, **kwargs)
|
2007-03-26 05:26:44 +08:00
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
"""
|
|
|
|
Validates the input and returns a string that contains only numbers.
|
|
|
|
Returns an empty string for empty values.
|
|
|
|
"""
|
|
|
|
v = super(JPPostalCodeField, self).clean(value)
|
|
|
|
return v.replace('-', '')
|
|
|
|
|
|
|
|
class JPPrefectureSelect(Select):
|
|
|
|
"""
|
|
|
|
A Select widget that uses a list of Japanese prefectures as its choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
from jp_prefectures import JP_PREFECTURES
|
|
|
|
super(JPPrefectureSelect, self).__init__(attrs, choices=JP_PREFECTURES)
|