Index: __init__.py
===================================================================
--- __init__.py (revision 3874)
+++ __init__.py (working copy)
@@ -280,6 +280,8 @@
this field must pass in order to be added or changed.
is_required
A Boolean. Is it a required field?
+ attribute_dict
+ A dictionary of property/value attributes for the form field.
Subclasses should also implement a render(data) method, which is responsible
for rending the form field in XHTML.
"""
@@ -360,6 +362,12 @@
def get_id(self):
"Returns the HTML 'id' attribute for this form field."
return FORM_FIELD_ID_PREFIX + self.field_name
+
+ def get_attribute_string(self):
+ attributes = ''
+ if self.attribute_dict:
+ attributes = " ".join(["%s=\"%s\"" % (p, v) for p, v in self.attribute_dict.items()])
+ return attributes
####################
# GENERIC WIDGETS #
@@ -367,12 +375,14 @@
class TextField(FormField):
input_type = "text"
- def __init__(self, field_name, length=30, maxlength=None, is_required=False, validator_list=None, member_name=None):
+ def __init__(self, field_name, length=30, maxlength=None, is_required=False, validator_list=None, member_name=None, attribute_dict=None):
if validator_list is None: validator_list = []
+ if attribute_dict is None: attribute_dict = {}
self.field_name = field_name
self.length, self.maxlength = length, maxlength
self.is_required = is_required
self.validator_list = [self.isValidLength, self.hasNoNewlines] + validator_list
+ self.attribute_dict = attribute_dict
if member_name != None:
self.member_name = member_name
@@ -393,9 +403,9 @@
maxlength = 'maxlength="%s" ' % self.maxlength
if isinstance(data, unicode):
data = data.encode(settings.DEFAULT_CHARSET)
- return '' % \
+ return '' % \
(self.input_type, self.get_id(), self.__class__.__name__, self.is_required and ' required' or '',
- self.field_name, self.length, escape(data), maxlength)
+ self.field_name, self.length, escape(data), maxlength, self.get_attribute_string())
def html2python(data):
return data
@@ -405,11 +415,13 @@
input_type = "password"
class LargeTextField(TextField):
- def __init__(self, field_name, rows=10, cols=40, is_required=False, validator_list=None, maxlength=None):
+ def __init__(self, field_name, rows=10, cols=40, is_required=False, validator_list=None, maxlength=None, attribute_dict=None):
if validator_list is None: validator_list = []
+ if attribute_dict is None: attribute_dict = {}
self.field_name = field_name
self.rows, self.cols, self.is_required = rows, cols, is_required
self.validator_list = validator_list[:]
+ self.attribute_dict = attribute_dict
if maxlength:
self.validator_list.append(self.isValidLength)
self.maxlength = maxlength
@@ -419,35 +431,39 @@
data = ''
if isinstance(data, unicode):
data = data.encode(settings.DEFAULT_CHARSET)
- return '' % \
+ return '' % \
(self.get_id(), self.__class__.__name__, self.is_required and ' required' or '',
- self.field_name, self.rows, self.cols, escape(data))
+ self.field_name, self.rows, self.cols, self.get_attribute_string(), escape(data))
class HiddenField(FormField):
- def __init__(self, field_name, is_required=False, validator_list=None):
+ def __init__(self, field_name, is_required=False, validator_list=None, attribute_dict=None):
if validator_list is None: validator_list = []
+ if attribute_dict is None: attribute_dict = {}
self.field_name, self.is_required = field_name, is_required
self.validator_list = validator_list[:]
+ self.attribute_dict = attribute_dict
def render(self, data):
- return '' % \
- (self.get_id(), self.field_name, escape(data))
+ return '' % \
+ (self.get_id(), self.field_name, escape(data), self.get_attribute_string())
class CheckboxField(FormField):
- def __init__(self, field_name, checked_by_default=False, validator_list=None):
+ def __init__(self, field_name, checked_by_default=False, validator_list=None, attribute_dict=None):
if validator_list is None: validator_list = []
+ if attribute_dict is None: attribute_dict = {}
self.field_name = field_name
self.checked_by_default = checked_by_default
self.is_required = False # because the validator looks for these
self.validator_list = validator_list[:]
+ self.attribute_dict = attribute_dict
def render(self, data):
checked_html = ''
if data or (data is '' and self.checked_by_default):
checked_html = ' checked="checked"'
- return '' % \
+ return '' % \
(self.get_id(), self.__class__.__name__,
- self.field_name, checked_html)
+ self.field_name, checked_html, self.get_attribute_string())
def html2python(data):
"Convert value from browser ('on' or '') to a Python boolean"
@@ -457,20 +473,22 @@
html2python = staticmethod(html2python)
class SelectField(FormField):
- def __init__(self, field_name, choices=None, size=1, is_required=False, validator_list=None, member_name=None):
+ def __init__(self, field_name, choices=None, size=1, is_required=False, validator_list=None, member_name=None, attribute_dict=None):
if validator_list is None: validator_list = []
+ if attribute_dict is None: attribute_dict = {}
if choices is None: choices = []
self.field_name = field_name
# choices is a list of (value, human-readable key) tuples because order matters
self.choices, self.size, self.is_required = choices, size, is_required
self.validator_list = [self.isValidChoice] + validator_list
+ self.attribute_dict = attribute_dict
if member_name != None:
self.member_name = member_name
def render(self, data):
- output = ['