diff --git a/django/newforms/forms.py b/django/newforms/forms.py
index 0c5219dfe2..d492ebeb0f 100644
--- a/django/newforms/forms.py
+++ b/django/newforms/forms.py
@@ -173,7 +173,8 @@ class BoundField(StrAndUnicode):
def __init__(self, form, field, name):
self.form = form
self.field = field
- self.name = form.add_prefix(name)
+ self.name = name
+ self.html_name = form.add_prefix(name)
self.label = self.field.label or pretty_name(name)
def __unicode__(self):
@@ -201,7 +202,7 @@ class BoundField(StrAndUnicode):
auto_id = self.auto_id
if auto_id and not attrs.has_key('id') and not widget.attrs.has_key('id'):
attrs['id'] = auto_id
- return widget.render(self.name, self.data, attrs=attrs)
+ return widget.render(self.html_name, self.data, attrs=attrs)
def as_text(self, attrs=None):
"""
@@ -222,8 +223,8 @@ class BoundField(StrAndUnicode):
def _data(self):
"Returns the data for this BoundField, or None if it wasn't given."
if self.field.widget.requires_data_list and isinstance(self.form.data, MultiValueDict):
- return self.form.data.getlist(self.name)
- return self.form.data.get(self.name, None)
+ return self.form.data.getlist(self.html_name)
+ return self.form.data.get(self.html_name, None)
data = property(_data)
def label_tag(self, contents=None):
@@ -251,8 +252,8 @@ class BoundField(StrAndUnicode):
"""
auto_id = self.form.auto_id
if auto_id and '%s' in str(auto_id):
- return str(auto_id) % self.name
+ return str(auto_id) % self.html_name
elif auto_id:
- return self.name
+ return self.html_name
return ''
auto_id = property(_auto_id)
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''
>>> print p['birthday']
+>>> print p['nonexistentfield']
+Traceback (most recent call last):
+...
+KeyError: "Key 'nonexistentfield' not found in Form"
+
>>> for boundfield in p:
... print boundfield
@@ -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