Ticket #4752: div-form.2.diff

File div-form.2.diff, 3.4 KB (added by michal@…, 17 years ago)

Patch that adds a parameter to BaseForm.init constructor to allow using arbitrary ErrorList class implementation

  • django/newforms/forms.py

     
    5757    # class is different than Form. See the comments by the Form class for more
    5858    # information. Any improvements to the form API should be made to *this*
    5959    # class, not to the Form class.
    60     def __init__(self, data=None, auto_id='id_%s', prefix=None, initial=None):
     60    def __init__(self, data=None, auto_id='id_%s', prefix=None, initial=None, error_list_class = ErrorList):
    6161        self.is_bound = data is not None
    6262        self.data = data or {}
    6363        self.auto_id = auto_id
    6464        self.prefix = prefix
    6565        self.initial = initial or {}
     66        self.error_list_class = error_list_class
    6667        self._errors = None # Stores the errors after clean() has been called.
    6768
    6869        # The base_fields class attribute is the *class-wide* definition of
     
    7778
    7879    def __iter__(self):
    7980        for name, field in self.fields.items():
    80             yield BoundField(self, field, name)
     81            yield BoundField(self, field, name, self.error_list_class)
    8182
    8283    def __getitem__(self, name):
    8384        "Returns a BoundField with the given name."
     
    8586            field = self.fields[name]
    8687        except KeyError:
    8788            raise KeyError('Key %r not found in Form' % name)
    88         return BoundField(self, field, name)
     89        return BoundField(self, field, name, self.error_list_class)
    8990
    9091    def _get_errors(self):
    9192        "Returns an ErrorDict for self.data"
     
    115116        top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
    116117        output, hidden_fields = [], []
    117118        for name, field in self.fields.items():
    118             bf = BoundField(self, field, name)
    119             bf_errors = ErrorList([escape(error) for error in bf.errors]) # Escape and cache in local variable.
     119            bf = BoundField(self, field, name, self.error_list_class)
     120            bf_errors = self.error_list_class([escape(error) for error in bf.errors]) # Escape and cache in local variable.
    120121            if bf.is_hidden:
    121122                if bf_errors:
    122123                    top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
     
    167168        field -- i.e., from Form.clean(). Returns an empty ErrorList if there
    168169        are none.
    169170        """
    170         return self.errors.get(NON_FIELD_ERRORS, ErrorList())
     171        return self.errors.get(NON_FIELD_ERRORS, self.error_list_class())
    171172
    172173    def full_clean(self):
    173174        """
     
    220221
    221222class BoundField(StrAndUnicode):
    222223    "A Field plus data"
    223     def __init__(self, form, field, name):
     224    def __init__(self, form, field, name, error_list_class = ErrorList):
    224225        self.form = form
    225226        self.field = field
    226227        self.name = name
    227228        self.html_name = form.add_prefix(name)
     229        self.error_list_class = error_list_class
    228230        if self.field.label is None:
    229231            self.label = pretty_name(name)
    230232        else:
     
    248250        Returns an ErrorList for this field. Returns an empty ErrorList
    249251        if there are none.
    250252        """
    251         return self.form.errors.get(self.name, ErrorList())
     253        return self.form.errors.get(self.name, self.error_list_class())
    252254    errors = property(_errors)
    253255
    254256    def as_widget(self, widget, attrs=None):
Back to Top