2007-03-26 05:26:44 +08:00
|
|
|
"""
|
|
|
|
JP-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.core import validators
|
|
|
|
from django.newforms import ValidationError
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext
|
2007-03-26 05:26:44 +08:00
|
|
|
from django.newforms.fields import RegexField, Select
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
'invalid': ugettext('Enter a postal code in the format XXXXXXX or XXX-XXXX.'),
|
|
|
|
}
|
|
|
|
|
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)
|