2 JP-specific Form helpers
5 from django
.forms
import ValidationError
6 from django
.utils
.translation
import ugettext_lazy
as _
7 from django
.forms
.fields
import RegexField
, Select
9 class JPPostalCodeField(RegexField
):
11 A form field that validates its input is a Japanese postcode.
13 Accepts 7 digits, with or without a hyphen.
15 default_error_messages
= {
16 'invalid': _('Enter a postal code in the format XXXXXXX or XXX-XXXX.'),
19 def __init__(self
, *args
, **kwargs
):
20 super(JPPostalCodeField
, self
).__init
__(r
'^\d{3}-\d{4}$|^\d{7}$',
21 max_length
=None, min_length
=None, *args
, **kwargs
)
23 def clean(self
, value
):
25 Validates the input and returns a string that contains only numbers.
26 Returns an empty string for empty values.
28 v
= super(JPPostalCodeField
, self
).clean(value
)
29 return v
.replace('-', '')
31 class JPPrefectureSelect(Select
):
33 A Select widget that uses a list of Japanese prefectures as its choices.
35 def __init__(self
, attrs
=None):
36 from jp_prefectures
import JP_PREFECTURES
37 super(JPPrefectureSelect
, self
).__init
__(attrs
, choices
=JP_PREFECTURES
)