Author: Honza_Kral
Date: 2009-06-02 21:36:33 -0500 (Tue, 02 Jun 2009)
New Revision: 10900
Modified:
django/branches/soc2009/model-validation/django/core/validators.py
django/branches/soc2009/model-validation/django/forms/fields.py
Log:
[soc2009/model-validation] Spit FormField.clean() to to_python() and validate()
Also moved EMPTY_VALUES to validators module and implemented the change
on first Field (CharField)
Modified: django/branches/soc2009/model-validation/django/core/validators.py
===================================================================
--- django/branches/soc2009/model-validation/django/core/validators.py
2009-06-03 02:36:16 UTC (rev 10899)
+++ django/branches/soc2009/model-validation/django/core/validators.py
2009-06-03 02:36:33 UTC (rev 10900)
@@ -1,5 +1,8 @@
from django.core.exceptions import ValidationError
+# These values, if given to to_python(), will trigger the self.required check.
+EMPTY_VALUES = (None, '')
+
def validate_integer(value, all_values={}, model_instance=None):
try:
int(value)
Modified: django/branches/soc2009/model-validation/django/forms/fields.py
===================================================================
--- django/branches/soc2009/model-validation/django/forms/fields.py
2009-06-03 02:36:16 UTC (rev 10899)
+++ django/branches/soc2009/model-validation/django/forms/fields.py
2009-06-03 02:36:33 UTC (rev 10900)
@@ -25,6 +25,7 @@
from django.core.exceptions import ValidationError
from django.core import validators
+from django.core.validators import EMPTY_VALUES
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_unicode, smart_str
@@ -46,10 +47,7 @@
'TypedChoiceField'
)
-# These values, if given to to_python(), will trigger the self.required check.
-EMPTY_VALUES = (None, '')
-
class Field(object):
widget = TextInput # Default widget to use when rendering this type of
Field.
hidden_widget = HiddenInput # Default widget to use when rendering this as
"hidden".
@@ -115,6 +113,14 @@
self.error_messages = messages
self.validators = self.default_validators + validators
+ def to_python(self, value):
+ return value
+
+ def validate(self, value):
+ if value in EMPTY_VALUES and self.required:
+ raise ValidationError(self.error_messages['required'])
+
+
def clean(self, value):
"""
Validates the given value and returns its "cleaned" value as an
@@ -122,8 +128,8 @@
Raises ValidationError for any errors.
"""
- if self.required and value in EMPTY_VALUES:
- raise ValidationError(self.error_messages['required'])
+ value = self.to_python(value)
+ self.validate(value)
return value
def widget_attrs(self, widget):
@@ -150,18 +156,23 @@
self.max_length, self.min_length = max_length, min_length
super(CharField, self).__init__(*args, **kwargs)
- def clean(self, value):
- "Validates max_length and min_length. Returns a Unicode object."
- super(CharField, self).clean(value)
+ def to_python(self, value):
+ "Returns a Unicode object."
if value in EMPTY_VALUES:
return u''
- value = smart_unicode(value)
+ return smart_unicode(value)
+
+ def validate(self, value):
+ "Validates max_length and min_length."
+ super(CharField, self).validate(value)
+ if value in EMPTY_VALUES:
+ # non-required field, no need for further validation
+ return
value_length = len(value)
if self.max_length is not None and value_length > self.max_length:
raise ValidationError(self.error_messages['max_length'] % {'max':
self.max_length, 'length': value_length})
if self.min_length is not None and value_length < self.min_length:
raise ValidationError(self.error_messages['min_length'] % {'min':
self.min_length, 'length': value_length})
- return value
def widget_attrs(self, widget):
if self.max_length is not None and isinstance(widget, (TextInput,
PasswordInput)):
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---