Created
March 18, 2013 13:47
-
-
Save chrisking/5187244 to your computer and use it in GitHub Desktop.
Validation in django forms
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
| class ContactForm(forms.Form): | |
| name = forms.CharField(label="Name*") | |
| company = forms.CharField(label="Company*") | |
| email = forms.EmailField(label="Email*") | |
| address = forms.CharField(required=False) | |
| city = forms.CharField(required=False) | |
| state = forms.CharField(required=False) | |
| zipcode = forms.CharField(required=False) | |
| country = forms.CharField(required=False) | |
| telephone = forms.CharField(required=False) | |
| fax = forms.CharField(required=False) | |
| def getEmailBody(self): | |
| body = 'Name: ' + self.cleaned_data['name'] | |
| body += '\n Company: ' + self.cleaned_data['company'] | |
| body += '\n Address: ' + self.cleaned_data['address'] | |
| body += '\n Email: ' + self.cleaned_data['email'] | |
| body += '\n City: ' + self.cleaned_data['city'] | |
| body += '\n State: ' + self.cleaned_data['state'] | |
| body += '\n Zipcode: ' + self.cleaned_data['zipcode'] | |
| body += '\n Country: ' + self.cleaned_data['country'] | |
| body += '\n Telephone: ' + self.cleaned_data['telephone'] | |
| body += '\n Fax: ' + self.cleaned_data['fax'] | |
| return body | |
| def clean_address(self): | |
| data = self.cleaned_data['address'] | |
| if "http://" in data: | |
| raise forms.ValidationError("SPAMBOT") | |
| # Always return the cleaned data, whether you have changed it or | |
| # not. | |
| return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment