def render(self, name, value, attrs=None): if value is None: value = '' code, number = parse_phone(value) final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) final_attrs_code = final_attrs.copy() final_attrs_code['name'] += '_code' final_attrs_code['id'] += '_code' final_attrs_code['maxlength'] = '6' if 'class' not in final_attrs_code: final_attrs_code['class'] = 'PhoneCode' else: final_attrs_code['class'] += ' PhoneCode' code_input = super(PhoneInput, self).render(None, force_unicode(code), final_attrs_code) number_input = super(PhoneInput, self).render(None, force_unicode(number), final_attrs) return mark_safe(u'<span class="country_code">+7</span><span class="phone_left_parenthesis">(</span>' \ '%s' \ '<span class="phone_right_parenthesis">)</span>' \ '\n%s' % (code_input, number_input))
def clean(self, value): # First check if empty if value in fields.EMPTY_VALUES: if self.required: raise util.ValidationError(self.error_messages['required']) else: return super(PhoneField, self).clean(value) code, number = parse_phone(value) code_len = len(code) if code_len < 3 or code_len > 6: raise util.ValidationError(self.error_messages['code_length']) if not code.isdigit(): raise util.ValidationError(self.error_messages['code_digits']) if len(number.strip()) == 0: raise util.ValidationError(self.error_messages['number_length']) return smart_unicode('(%s) %s' % (code, number))