from django.core.validators import ValidationError, CriticalValidationError from django.newforms import ValidationError as newformsValidationError def run_oldforms_validator(field_name, form, validator): """ Run an oldforms validator against a newforms form. This function is nearly identical to oldforms.FormField.run_validator. """ # Use form.data here instead of form.clean_data because the oldforms # validators worked with the raw POST data. if form.fields[field_name].required or \ form.data.get(field_name, None) or \ hasattr(validator, 'always_test'): validator(form.data.get(field_name, ''), form.data) def run_oldforms_validators(field_name, form, validator_list): """ Run a list of oldforms validators against a newforms form. This function is nearly identical to oldforms.FormField.run_validators. """ errors = {} try: for validator in validator_list: try: run_oldforms_validator(field_name, form, validator) except ValidationError, e: errors.setdefault(field_name, []).extend(e.messages) # If a CriticalValidationError is raised, ignore any other ValidationErrors # for this particular field except CriticalValidationError, e: errors.setdefault(field_name, []).extend(e.messages) if errors: raise newformsValidationError(errors[field_name])