summaryrefslogtreecommitdiff
path: root/tests/regressiontests
diff options
context:
space:
mode:
authorJoseph Kocherhans <joseph@jkocherhans.com>2007-09-25 02:42:26 +0000
committerJoseph Kocherhans <joseph@jkocherhans.com>2007-09-25 02:42:26 +0000
commitb40f9b63bb6900689e6052052f620d6447ec63e5 (patch)
tree5536dc6276f5d5474a46f451145cf162a32a7105 /tests/regressiontests
parent9687447e3139d0f80619900c4f08d763ca8b0aa2 (diff)
newforms-admin: Fixed #5353. Added FormSet validation hook. Separated a few things out from the original patch and added more tests. Thanks, Honza Kral.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@6419 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests')
-rw-r--r--tests/regressiontests/forms/formsets.py65
1 files changed, 63 insertions, 2 deletions
diff --git a/tests/regressiontests/forms/formsets.py b/tests/regressiontests/forms/formsets.py
index 5129c1516c..a6da2fec36 100644
--- a/tests/regressiontests/forms/formsets.py
+++ b/tests/regressiontests/forms/formsets.py
@@ -5,8 +5,8 @@ formset_tests = """
FormSet allows us to use multiple instance of the same form on 1 page. For now,
the best way to create a FormSet is by using the formset_for_form function.
->>> from django.newforms import Form, CharField, IntegerField
->>> from django.newforms.formsets import formset_for_form
+>>> from django.newforms import Form, CharField, IntegerField, ValidationError
+>>> from django.newforms.formsets import formset_for_form, BaseFormSet
>>> class Choice(Form):
... choice = CharField()
@@ -420,4 +420,65 @@ True
[{'votes': 900, 'DELETE': True, 'ORDER': 2, 'choice': u'Fergie'}]
+# FormSet clean hook ##########################################################
+
+FormSets have a hook for doing extra validation that shouldn't be tied to any
+particular form. It follows the same pattern as the clean hook on Forms.
+
+Let's define a FormSet that takes a list of favorite drinks, but raises am
+error if there are any duplicates.
+
+>>> class FavoriteDrinkForm(Form):
+... name = CharField()
+...
+
+>>> class FavoriteDrinksFormSet(BaseFormSet):
+... form_class = FavoriteDrinkForm
+... num_extra = 2
+... orderable = False
+... deletable = False
+...
+... def clean(self):
+... seen_drinks = []
+... for drink in self.cleaned_data:
+... if drink['name'] in seen_drinks:
+... raise ValidationError('You may only specify a drink once.')
+... seen_drinks.append(drink['name'])
+... return self.cleaned_data
+...
+
+We start out with a some duplicate data.
+
+>>> data = {
+... 'drinks-COUNT': '2',
+... 'drinks-0-name': 'Gin and Tonic',
+... 'drinks-1-name': 'Gin and Tonic',
+... }
+
+>>> formset = FavoriteDrinksFormSet(data, prefix='drinks')
+>>> formset.is_valid()
+False
+
+Any errors raised by formset.clean() are available via the
+formset.non_form_errors() method.
+
+>>> for error in formset.non_form_errors():
+... print error
+You may only specify a drink once.
+
+
+Make sure we didn't break the valid case.
+
+>>> data = {
+... 'drinks-COUNT': '2',
+... 'drinks-0-name': 'Gin and Tonic',
+... 'drinks-1-name': 'Bloody Mary',
+... }
+
+>>> formset = FavoriteDrinksFormSet(data, prefix='drinks')
+>>> formset.is_valid()
+True
+>>> for error in formset.non_form_errors():
+... print error
+
"""