Ticket #6675: newforms_better_error_message_if_unbound.diff

File newforms_better_error_message_if_unbound.diff, 3.0 KB (added by Thomas Güttler, 17 years ago)
  • django/newforms/forms.py

     
    9797            raise KeyError('Key %r not found in Form' % name)
    9898        return BoundField(self, field, name)
    9999
     100    def __getattr__(self, attr):
     101        if attr=='cleaned_data':
     102            raise AttributeError('%r: Unbound Fields have no cleaned_data attribute' % self)
     103        if attr=='errors':
     104            return self._get_errors()
     105        raise AttributeError('%r has no attribute %s' % (self.__class__, attr))
     106
    100107    def _get_errors(self):
    101108        "Returns an ErrorDict for the data provided for the form"
    102109        if self._errors is None:
  • tests/regressiontests/forms/forms.py

     
    7272{'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']}
    7373>>> p.is_valid()
    7474False
    75 >>> p.cleaned_data
    76 Traceback (most recent call last):
    77 ...
    78 AttributeError: 'Person' object has no attribute 'cleaned_data'
     75>>> try:
     76...     p.cleaned_data
     77... except AttributeError:
     78...     # This should happen.
     79...     pass
     80... else:
     81...     raise('accessing cleaned_data of unbound field should raise AttributeError')
     82
    7983>>> print p
    8084<tr><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr>
    8185<tr><th><label for="id_last_name">Last name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="last_name" id="id_last_name" /></td></tr>
     
    106110{}
    107111>>> p.is_valid()
    108112False
    109 >>> p.cleaned_data
    110 Traceback (most recent call last):
    111 ...
    112 AttributeError: 'Person' object has no attribute 'cleaned_data'
     113>>> try:
     114...     p.cleaned_data
     115... except AttributeError:
     116...     # This should happen.
     117...     pass
     118... else:
     119...     raise('accessing cleaned_data of unbound field should raise AttributeError')
    113120>>> print p
    114121<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
    115122<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>
     
    148155  * This field is required.
    149156* birthday
    150157  * This field is required.
    151 >>> p.cleaned_data
    152 Traceback (most recent call last):
    153 ...
    154 AttributeError: 'Person' object has no attribute 'cleaned_data'
     158>>> try:
     159...     p.cleaned_data
     160... except AttributeError:
     161...     # This should happen.
     162...     pass
     163... else:
     164...     raise('accessing cleaned_data of unbound field should raise AttributeError')
    155165>>> p['first_name'].errors
    156166[u'This field is required.']
    157167>>> p['first_name'].errors.as_ul()
Back to Top