Skip to content

Instantly share code, notes, and snippets.

@tkaemming
Created December 21, 2009 01:24
Show Gist options
  • Save tkaemming/260702 to your computer and use it in GitHub Desktop.
Save tkaemming/260702 to your computer and use it in GitHub Desktop.
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">&mdash;</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