Created
February 10, 2018 01:25
-
-
Save guyjacks/3f72111ec692467cfd1ec5d67c89ec17 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from django import forms | |
from localflavor.us.forms import USZipCodeField | |
from .models import Organization | |
class OrganizationModelForm(forms.ModelForm): | |
class Meta: | |
model = Organization | |
fields = [ | |
'name', | |
'description', | |
'short_description', | |
'phone', | |
'url', | |
'ein', | |
] | |
error_messages = { | |
'ein_mismatch': "Your EINs didn't match.", | |
} | |
ein_confirm = forms.CharField( | |
max_length=10, | |
empty_value=None, | |
label='Confirm EIN', | |
required=False, | |
) | |
def clean(self): | |
super().clean() | |
self.clean_ein() | |
def clean_ein(self): | |
ein = self.cleaned_data.get('ein') | |
ein_confirm = self.cleaned_data.get('ein_confirm') | |
if ein and ein_confirm: | |
if ein != ein_confirm: | |
raise forms.ValidationError( | |
self.error_messages['ein_mismatch'], | |
code='ein_mismatch', | |
) | |
return ein | |
class OrganizationAddForm(OrganizationModelForm): | |
class Meta: | |
model = Organization | |
fields = OrganizationAddForm.Meta.fields | |
fields.append('type') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment