1
0
mirror of https://github.com/django/django.git synced 2025-10-24 22:26:08 +00:00

Fixed #3132 -- Added prefix support for newforms. Thanks, jkocherhans

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4194 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty
2006-12-12 05:55:39 +00:00
parent 5467c34b0b
commit faaec9c28f
2 changed files with 98 additions and 3 deletions

View File

@@ -36,10 +36,11 @@ class Form(StrAndUnicode):
"A collection of Fields, plus their associated data."
__metaclass__ = DeclarativeFieldsMetaclass
def __init__(self, data=None, auto_id='id_%s'): # TODO: prefix stuff
def __init__(self, data=None, auto_id='id_%s', prefix=None):
self.ignore_errors = data is None
self.data = data or {}
self.auto_id = auto_id
self.prefix = prefix
self.clean_data = None # Stores the data after clean() has been called.
self.__errors = None # Stores the errors after clean() has been called.
@@ -72,6 +73,15 @@ class Form(StrAndUnicode):
"""
return not self.ignore_errors and not bool(self.errors)
def add_prefix(self, field_name):
"""
Returns the field name with a prefix appended, if this Form has a
prefix set.
Subclasses may wish to override.
"""
return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name
def _html_output(self, normal_row, error_row, row_ender, errors_on_separate_row):
"Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
@@ -132,7 +142,7 @@ class Form(StrAndUnicode):
# value_from_datadict() gets the data from the dictionary.
# Each widget type knows how to retrieve its own data, because some
# widgets split data over several HTML fields.
value = field.widget.value_from_datadict(self.data, name)
value = field.widget.value_from_datadict(self.data, self.add_prefix(name))
try:
value = field.clean(value)
self.clean_data[name] = value
@@ -163,7 +173,7 @@ class BoundField(StrAndUnicode):
def __init__(self, form, field, name):
self.form = form
self.field = field
self.name = name
self.name = form.add_prefix(name)
self.label = self.field.label or pretty_name(name)
def __unicode__(self):