Ticket #9082: forms.py

File forms.py, 1.1 KB (added by Tomáš Ehrlich, 16 years ago)
Line 
1"""
2Czech-specific form helpers
3"""
4
5from django.forms.fields import Select, RegexField
6from django.utils.translation import ugettext_lazy as _
7
8class CZRegionSelect(Select):
9 """
10 A select widget widget with list of Czech regions as choices.
11 """
12 def __init__(self, attrs=None):
13 from cs_regions import REGION_CHOICES
14 super(CZRegionSelect, self).__init__(attrs, choices=REGION_CHOICES)
15
16class CZPostalCodeField(RegexField):
17 """
18 A form field that validates its input as Czech postal code.
19 Valid form is XXXXX or XXX XX, where X represents integer.
20 """
21 default_error_messages = {
22 'invalid': _(u'Enter a postal code in the format XXXXX or XXX XX.'),
23 }
24
25 def __init__(self, *args, **kwargs):
26 super(CZPostalCodeField, self).__init__(r'^\d{5}$|^\d{3} \d{2}$',
27 max_length=None, min_length=None, *args, **kwargs)
28
29 def clean(self, value):
30 """
31 Validates the input and returns a string that contains only numbers.
32 Returns an empty string for empty values.
33 """
34 v = super(CZPostalCodeField, self).clean(value)
35 return v.replace(' ', '')
Back to Top