Created
December 21, 2009 01:24
-
-
Save tkaemming/260702 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PhoneNumberMultiWidget(forms.MultiWidget): | |
def __init__(self, attrs=None): | |
self.widgets = ( | |
forms.TextInput(attrs={'size': 3, 'maxlength': 3, 'class': 'areacode'}), | |
forms.TextInput(attrs={'size': 3, 'maxlength': 3, 'class': 'exchange'}), | |
forms.TextInput(attrs={'size': 4, 'maxlength': 4, 'class': 'lastfour'}) | |
) | |
super(PhoneNumberMultiWidget, self).__init__(self.widgets, attrs) | |
def format_output(self, rendered_widgets): | |
return u'<div class="phone-number">%s</div>' % u'<span class="separator">—</span>'.join(rendered_widgets) | |
def decompress(self, data_list): | |
return [None, None, None] | |
class PhoneNumberHiddenMultiWidget(PhoneNumberMultiWidget): | |
def __init__(self, attrs=None): | |
self.widgets = (forms.HiddenInput(), forms.HiddenInput(), | |
forms.HiddenInput()) | |
super(PhoneNumberMultiWidget, self).__init__(self.widgets, attrs) | |
def format_output(self, rendered_widgets): | |
return ''.join(rendered_widgets) | |
class PhoneNumberMultiField(forms.MultiValueField): | |
widget = PhoneNumberMultiWidget | |
hidden_widget = PhoneNumberHiddenMultiWidget | |
def __init__(self, *args, **kwargs): | |
self.fields = ( | |
forms.RegexField(r'^(\d+)$', required=True, max_length=3, min_length=3), | |
forms.RegexField(r'^(\d+)$', required=True, max_length=3, min_length=3), | |
forms.RegexField(r'^(\d+)$', required=True, max_length=4, min_length=4), | |
) | |
super(PhoneNumberMultiField, self).__init__(self.fields, *args, **kwargs) | |
def compress(self, data_list): | |
return u'-'.join(data_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment