def clean(self, value): value = super(BrPlacaVeicularField, self).clean(value) if value in EMPTY_VALUES: return u'' if len(value) == 7: # formato "AAA1111" value = mask_placa(value) pass if len(value) != 8: # formato "AAA-1111" raise forms.ValidationError(self.error_messages['invalid']) grupos = value.split('-') if not grupos[0].isalpha(): raise forms.ValidationError(self.error_messages['invalid']) else: letras = grupos[0].upper() if not grupos[1].isdigit(): raise forms.ValidationError(self.error_messages['invalid']) return letras + '-' + grupos[1]
def render(self, name, value, attrs=None): if value and len(value) == 7: value = mask_placa(value) return super(self.__class__, self).render(name, value, attrs=attrs)