mirror of
https://github.com/django/django.git
synced 2025-11-20 19:47:55 +00:00
django.newforms: Implemented hook for validation not tied to a particular field. Renamed to_python() to clean() -- it's just...cleaner. Added Form.as_table(), Form.as_url(), Form.as_table_with_errors() and Form.as_ul_with_errors(). Added ComboField. Updated all unit tests.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3978 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
88029f7700
commit
46b0713315
4 changed files with 387 additions and 162 deletions
|
|
@ -14,6 +14,7 @@ __all__ = (
|
|||
'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField',
|
||||
'RegexField', 'EmailField', 'URLField', 'BooleanField',
|
||||
'ChoiceField', 'MultipleChoiceField',
|
||||
'ComboField',
|
||||
)
|
||||
|
||||
# These values, if given to to_python(), will trigger the self.required check.
|
||||
|
|
@ -34,9 +35,9 @@ class Field(object):
|
|||
widget = widget()
|
||||
self.widget = widget
|
||||
|
||||
def to_python(self, value):
|
||||
def clean(self, value):
|
||||
"""
|
||||
Validates the given value and returns its "normalized" value as an
|
||||
Validates the given value and returns its "cleaned" value as an
|
||||
appropriate Python object.
|
||||
|
||||
Raises ValidationError for any errors.
|
||||
|
|
@ -50,9 +51,9 @@ class CharField(Field):
|
|||
Field.__init__(self, required, widget)
|
||||
self.max_length, self.min_length = max_length, min_length
|
||||
|
||||
def to_python(self, value):
|
||||
def clean(self, value):
|
||||
"Validates max_length and min_length. Returns a Unicode object."
|
||||
Field.to_python(self, value)
|
||||
Field.clean(self, value)
|
||||
if value in EMPTY_VALUES: value = u''
|
||||
if not isinstance(value, basestring):
|
||||
value = unicode(str(value), DEFAULT_ENCODING)
|
||||
|
|
@ -65,12 +66,12 @@ class CharField(Field):
|
|||
return value
|
||||
|
||||
class IntegerField(Field):
|
||||
def to_python(self, value):
|
||||
def clean(self, value):
|
||||
"""
|
||||
Validates that int() can be called on the input. Returns the result
|
||||
of int().
|
||||
"""
|
||||
super(IntegerField, self).to_python(value)
|
||||
super(IntegerField, self).clean(value)
|
||||
try:
|
||||
return int(value)
|
||||
except (ValueError, TypeError):
|
||||
|
|
@ -89,12 +90,12 @@ class DateField(Field):
|
|||
Field.__init__(self, required, widget)
|
||||
self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS
|
||||
|
||||
def to_python(self, value):
|
||||
def clean(self, value):
|
||||
"""
|
||||
Validates that the input can be converted to a date. Returns a Python
|
||||
datetime.date object.
|
||||
"""
|
||||
Field.to_python(self, value)
|
||||
Field.clean(self, value)
|
||||
if value in EMPTY_VALUES:
|
||||
return None
|
||||
if isinstance(value, datetime.datetime):
|
||||
|
|
@ -125,12 +126,12 @@ class DateTimeField(Field):
|
|||
Field.__init__(self, required, widget)
|
||||
self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
|
||||
|
||||
def to_python(self, value):
|
||||
def clean(self, value):
|
||||
"""
|
||||
Validates that the input can be converted to a datetime. Returns a
|
||||
Python datetime.datetime object.
|
||||
"""
|
||||
Field.to_python(self, value)
|
||||
Field.clean(self, value)
|
||||
if value in EMPTY_VALUES:
|
||||
return None
|
||||
if isinstance(value, datetime.datetime):
|
||||
|
|
@ -157,12 +158,12 @@ class RegexField(Field):
|
|||
self.regex = regex
|
||||
self.error_message = error_message or u'Enter a valid value.'
|
||||
|
||||
def to_python(self, value):
|
||||
def clean(self, value):
|
||||
"""
|
||||
Validates that the input matches the regular expression. Returns a
|
||||
Unicode object.
|
||||
"""
|
||||
Field.to_python(self, value)
|
||||
Field.clean(self, value)
|
||||
if value in EMPTY_VALUES: value = u''
|
||||
if not isinstance(value, basestring):
|
||||
value = unicode(str(value), DEFAULT_ENCODING)
|
||||
|
|
@ -192,8 +193,8 @@ class URLField(RegexField):
|
|||
RegexField.__init__(self, url_re, u'Enter a valid URL.', required, widget)
|
||||
self.verify_exists = verify_exists
|
||||
|
||||
def to_python(self, value):
|
||||
value = RegexField.to_python(self, value)
|
||||
def clean(self, value):
|
||||
value = RegexField.clean(self, value)
|
||||
if self.verify_exists:
|
||||
import urllib2
|
||||
try:
|
||||
|
|
@ -207,9 +208,9 @@ class URLField(RegexField):
|
|||
class BooleanField(Field):
|
||||
widget = CheckboxInput
|
||||
|
||||
def to_python(self, value):
|
||||
def clean(self, value):
|
||||
"Returns a Python boolean object."
|
||||
Field.to_python(self, value)
|
||||
Field.clean(self, value)
|
||||
return bool(value)
|
||||
|
||||
class ChoiceField(Field):
|
||||
|
|
@ -219,11 +220,11 @@ class ChoiceField(Field):
|
|||
Field.__init__(self, required, widget)
|
||||
self.choices = choices
|
||||
|
||||
def to_python(self, value):
|
||||
def clean(self, value):
|
||||
"""
|
||||
Validates that the input is in self.choices.
|
||||
"""
|
||||
value = Field.to_python(self, value)
|
||||
value = Field.clean(self, value)
|
||||
if value in EMPTY_VALUES: value = u''
|
||||
if not isinstance(value, basestring):
|
||||
value = unicode(str(value), DEFAULT_ENCODING)
|
||||
|
|
@ -238,7 +239,7 @@ class MultipleChoiceField(ChoiceField):
|
|||
def __init__(self, choices=(), required=True, widget=SelectMultiple):
|
||||
ChoiceField.__init__(self, choices, required, widget)
|
||||
|
||||
def to_python(self, value):
|
||||
def clean(self, value):
|
||||
"""
|
||||
Validates that the input is a list or tuple.
|
||||
"""
|
||||
|
|
@ -259,3 +260,18 @@ class MultipleChoiceField(ChoiceField):
|
|||
if val not in valid_values:
|
||||
raise ValidationError(u'Select a valid choice. %s is not one of the available choices.' % val)
|
||||
return new_value
|
||||
|
||||
class ComboField(Field):
|
||||
def __init__(self, fields=(), required=True, widget=None):
|
||||
Field.__init__(self, required, widget)
|
||||
self.fields = fields
|
||||
|
||||
def clean(self, value):
|
||||
"""
|
||||
Validates the given value against all of self.fields, which is a
|
||||
list of Field instances.
|
||||
"""
|
||||
Field.clean(self, value)
|
||||
for field in self.fields:
|
||||
value = field.clean(value)
|
||||
return value
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue