diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2006-12-13 23:03:19 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2006-12-13 23:03:19 +0000 |
| commit | d8a21981b70770e536236acfbc4c87f48b587661 (patch) | |
| tree | 2e1f73d72ebfe05504e5afcd4da2b616207e830e /tests/regressiontests | |
| parent | 10edce12fc05fb31610f1ee4a372cd7c0b388533 (diff) | |
Fixed #3139 -- newforms BoundField no longer returns empty errors when using a prefix. Thanks, jkocherhans
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4199 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests')
| -rw-r--r-- | tests/regressiontests/forms/tests.py | 43 |
1 files changed, 37 insertions, 6 deletions
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index af5089c2e6..5fe95421db 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -1332,6 +1332,11 @@ u'' <input type="text" name="last_name" value="Lennon" id="id_last_name" /> >>> print p['birthday'] <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /> +>>> print p['nonexistentfield'] +Traceback (most recent call last): +... +KeyError: "Key 'nonexistentfield' not found in Form" + >>> for boundfield in p: ... print boundfield <input type="text" name="first_name" value="John" id="id_first_name" /> @@ -1974,9 +1979,39 @@ actual field name. {} >>> p.is_valid() True +>>> p.clean_data +{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} -This is pretty unremarkable in and of itself, but let's create some data that -contains info for two different people. +Let's try submitting some bad data to make sure form.errors and field.errors +work as expected. +>>> data = { +... 'person1-first_name': u'', +... 'person1-last_name': u'', +... 'person1-birthday': u'' +... } +>>> p = Person(data, prefix='person1') +>>> p.errors +{'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']} +>>> p['first_name'].errors +[u'This field is required.'] +>>> p['person1-first_name'].errors +Traceback (most recent call last): +... +KeyError: "Key 'person1-first_name' not found in Form" + +In this example, the data doesn't have a prefix, but the form requires it, so +the form doesn't "see" the fields. +>>> data = { +... 'first_name': u'John', +... 'last_name': u'Lennon', +... 'birthday': u'1940-10-9' +... } +>>> p = Person(data, prefix='person1') +>>> p.errors +{'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']} + +With prefixes, a single data dictionary can hold data for multiple instances +of the same form. >>> data = { ... 'person1-first_name': u'John', ... 'person1-last_name': u'Lennon', @@ -1985,15 +2020,11 @@ contains info for two different people. ... 'person2-last_name': u'Morrison', ... 'person2-birthday': u'1943-12-8' ... } - -If we use the correct prefix argument, we can create two different forms that -will only use and validate the data for fields with a matching prefix. >>> p1 = Person(data, prefix='person1') >>> p1.is_valid() True >>> p1.clean_data {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} - >>> p2 = Person(data, prefix='person2') >>> p2.is_valid() True |
