summaryrefslogtreecommitdiff
path: root/django/newforms/fields.py
diff options
context:
space:
mode:
authorJoseph Kocherhans <joseph@jkocherhans.com>2006-11-25 22:37:50 +0000
committerJoseph Kocherhans <joseph@jkocherhans.com>2006-11-25 22:37:50 +0000
commit3afdd8850485c9119b50a5a736d3aa16b7b912fe (patch)
treeaa9663337dfd0c54d97f826bf912aa5a83af3468 /django/newforms/fields.py
parent889bf502818df7cbf8332a330703ff97dce01f2d (diff)
[generic-auth] Merged to trunk [4103].
git-svn-id: http://code.djangoproject.com/svn/django/branches/generic-auth@4104 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/newforms/fields.py')
-rw-r--r--django/newforms/fields.py31
1 files changed, 13 insertions, 18 deletions
diff --git a/django/newforms/fields.py b/django/newforms/fields.py
index b9e2ed35c7..40fc18bd3e 100644
--- a/django/newforms/fields.py
+++ b/django/newforms/fields.py
@@ -2,7 +2,7 @@
Field classes
"""
-from util import ValidationError, DEFAULT_ENCODING
+from util import ValidationError, DEFAULT_ENCODING, smart_unicode
from widgets import TextInput, CheckboxInput, Select, SelectMultiple
import datetime
import re
@@ -28,6 +28,9 @@ except NameError:
class Field(object):
widget = TextInput # Default widget to use when rendering this type of Field.
+ # Tracks each time a Field instance is created. Used to retain order.
+ creation_counter = 0
+
def __init__(self, required=True, widget=None):
self.required = required
widget = widget or self.widget
@@ -35,6 +38,10 @@ class Field(object):
widget = widget()
self.widget = widget
+ # Increase the creation counter, and save our local copy.
+ self.creation_counter = Field.creation_counter
+ Field.creation_counter += 1
+
def clean(self, value):
"""
Validates the given value and returns its "cleaned" value as an
@@ -55,10 +62,7 @@ class CharField(Field):
"Validates max_length and min_length. Returns a Unicode object."
Field.clean(self, value)
if value in EMPTY_VALUES: value = u''
- if not isinstance(value, basestring):
- value = unicode(str(value), DEFAULT_ENCODING)
- elif not isinstance(value, unicode):
- value = unicode(value, DEFAULT_ENCODING)
+ value = smart_unicode(value)
if self.max_length is not None and len(value) > self.max_length:
raise ValidationError(u'Ensure this value has at most %d characters.' % self.max_length)
if self.min_length is not None and len(value) < self.min_length:
@@ -165,10 +169,7 @@ class RegexField(Field):
"""
Field.clean(self, value)
if value in EMPTY_VALUES: value = u''
- if not isinstance(value, basestring):
- value = unicode(str(value), DEFAULT_ENCODING)
- elif not isinstance(value, unicode):
- value = unicode(value, DEFAULT_ENCODING)
+ value = smart_unicode(value)
if not self.regex.search(value):
raise ValidationError(self.error_message)
return value
@@ -244,10 +245,7 @@ class ChoiceField(Field):
"""
value = Field.clean(self, value)
if value in EMPTY_VALUES: value = u''
- if not isinstance(value, basestring):
- value = unicode(str(value), DEFAULT_ENCODING)
- elif not isinstance(value, unicode):
- value = unicode(value, DEFAULT_ENCODING)
+ value = smart_unicode(value)
valid_values = set([str(k) for k, v in self.choices])
if value not in valid_values:
raise ValidationError(u'Select a valid choice. %s is not one of the available choices.' % value)
@@ -267,11 +265,8 @@ class MultipleChoiceField(ChoiceField):
raise ValidationError(u'This field is required.')
new_value = []
for val in value:
- if not isinstance(val, basestring):
- value = unicode(str(val), DEFAULT_ENCODING)
- elif not isinstance(val, unicode):
- value = unicode(val, DEFAULT_ENCODING)
- new_value.append(value)
+ val = smart_unicode(val)
+ new_value.append(val)
# Validate that each value in the value list is in self.choices.
valid_values = set([k for k, v in self.choices])
for val in new_value: