From c43edf6d768f0ad001a6fea73084c04428f9721e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Mon, 8 Jun 2009 00:55:27 +0000 Subject: [PATCH] [soc2009/model-validation] Moved execution of validators to separate method This method (run_validators) is being run after the validate() method finishes and doesn't produce any errors. Errors from individual validators are aggregated and then raised together in one ValidationError. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10947 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/fields.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/django/forms/fields.py b/django/forms/fields.py index d65edd7047..6adc6dba2a 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -119,12 +119,19 @@ class Field(object): def validate(self, value): if value in EMPTY_VALUES and self.required: raise ValidationError(self.error_messages['required']) + + def run_validators(self, value): + errors = [] for v in self.validators: # don't run complex validators since they need all_values # and must therefore be run on the form level if not isinstance(v, validators.ComplexValidator): - v(value) - + try: + v(value) + except ValidationError, e: + errors.extend(e.messages) + if errors: + raise ValidationError(errors) def clean(self, value): """ @@ -135,6 +142,7 @@ class Field(object): """ value = self.to_python(value) self.validate(value) + self.run_validators(value) return value def widget_attrs(self, widget):