Ticket #5620: newforms_xmlfield.diff

File newforms_xmlfield.diff, 1.9 KB (added by teepark, 7 years ago)

recycles the oldforms XMLField validation for newforms

  • db/models/fields/__init__.py

     
    11571157        self.schema_path = schema_path
    11581158        Field.__init__(self, verbose_name, name, **kwargs)
    11591159
     1160    def formfield(self, **kwargs):
     1161        defaults = {'form_class': forms.XMLField}
     1162        defaults.update(kwargs)
     1163        return super(XMLField, self).formfield(**defaults)
     1164
    11601165    def get_manipulator_field_objs(self):
    11611166        return [curry(oldforms.XMLLargeTextField, schema_path=self.schema_path)]
    11621167
  • newforms/fields.py

     
    3232    'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField',
    3333    'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField',
    3434    'ComboField', 'MultiValueField', 'FloatField', 'DecimalField',
    35     'SplitDateTimeField', 'IPAddressField', 'FilePathField',
     35    'SplitDateTimeField', 'IPAddressField', 'FilePathField', 'XMLField'
    3636)
    3737
    3838# These values, if given to to_python(), will trigger the self.required check.
     
    782782
    783783    def __init__(self, *args, **kwargs):
    784784        super(IPAddressField, self).__init__(ipv4_re, *args, **kwargs)
     785
     786class XMLField(CharField):
     787    """
     788    A field that validates XML content against a RelaxNG schema
     789    """
     790    def __init__(self, schema_path):
     791        self.schema_path = schema_path
     792
     793    def clean(self, value):
     794        from django.core.validators import RelaxNGCompact, ValidationError as OldValidationError
     795        validator = RelaxNGCompact(self.schema_path)
     796        try:
     797            validator(value, "") # second arg isn't used but is required
     798        except OldValidationError, e:
     799            raise ValidationError(e.messages)
     800        return value
Back to Top