Skip to content

Instantly share code, notes, and snippets.

@mjtamlyn
Created May 15, 2015 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjtamlyn/00a34acb07481d8ae28d to your computer and use it in GitHub Desktop.
Save mjtamlyn/00a34acb07481d8ae28d to your computer and use it in GitHub Desktop.
class MyForm(forms.Form):
# ... Many fields
def clean(self):
foo = self.cleaned_data.get('foo')
bar = self.cleaned_data.get('bar')
if foo and bar and foo > 0 and bar < 0:
self.add_error('bar', ValidationError('Some message', code='foo_bar_mismatch'))
# ... Other validation rules here
class TestMyForm(TestCase):
def test_foo_bar_mismatch(self):
data = {
# .. other fields
'foo': 1,
'bar': -1,
}
form = MyForm(data=data)
self.assertFalse(form.is_valid())
self.assertTrue(form.has_error('bar', code='foo_bar_mismatch'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment