Ticket #5524: ticket_5524__rev_6917.diff

File ticket_5524__rev_6917.diff, 5.1 KB (added by Ben Slavin, 17 years ago)
  • django/newforms/forms.py

     
    195195            self.cleaned_data = self.clean()
    196196        except ValidationError, e:
    197197            self._errors[NON_FIELD_ERRORS] = e.messages
    198         if self._errors:
    199             delattr(self, 'cleaned_data')
    200198
    201199    def clean(self):
    202200        """
     
    293291        """
    294292        return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
    295293    data = property(_data)
     294   
     295    def _cleaned_data(self):
     296        """
     297        Returns the cleaned_data for this BoundField, or raises KeyError if this field
     298        lacks an entry in cleaned_data.
     299       
     300        Hint: If self.errors evaluates to False, we know this won't throw a
     301        KeyError (otherwise, it will).
     302        """
     303        try:
     304            return self.form.cleaned_data[self.name]
     305        except KeyError:
     306            raise KeyError, "Field '%s' did not provide cleaned data." % self.name
     307    cleaned_data = property(_cleaned_data)
    296308
    297309    def label_tag(self, contents=None, attrs=None):
    298310        """
  • tests/modeltests/model_forms/models.py

     
    235235>>> f.errors
    236236{'name': [u'This field is required.'], 'slug': [u'This field is required.']}
    237237>>> f.cleaned_data
    238 Traceback (most recent call last):
    239 ...
    240 AttributeError: 'CategoryForm' object has no attribute 'cleaned_data'
     238{'url': u'foo'}
    241239>>> f.save()
    242240Traceback (most recent call last):
    243241...
  • tests/regressiontests/forms/extra.py

     
    396396>>> FormWithImage().is_multipart()
    397397True
    398398
     399###################################
     400# Test cleaned_data on BoundField #
     401###################################
     402
     403>>> class BandMemberForm(Form):
     404...     name = CharField(max_length=20)
     405...     plays_drums = BooleanField()
     406
     407>>> f = BandMemberForm({'name': 'Ringo', 'plays_drums': 'True'})
     408>>> f.is_valid()
     409True
     410>>> f['name'].data
     411'Ringo'
     412>>> f['name'].cleaned_data
     413u'Ringo'
     414>>> f['plays_drums'].data
     415'True'
     416>>> f['plays_drums'].cleaned_data
     417True
     418
     419>>> f = BandMemberForm({'plays_drums': 'True'})
     420>>> f.is_valid()
     421False
     422>>> f['name'].data
     423>>> f['name'].cleaned_data
     424Traceback (most recent call last):
     425...
     426KeyError: "Field 'name' did not provide cleaned data."
     427>>> f['plays_drums'].cleaned_data
     428True
    399429"""
  • tests/regressiontests/forms/forms.py

     
    7373>>> p.is_valid()
    7474False
    7575>>> p.cleaned_data
    76 Traceback (most recent call last):
    77 ...
    78 AttributeError: 'Person' object has no attribute 'cleaned_data'
     76{}
    7977>>> print p
    8078<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>
    8179<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>
     
    149147* birthday
    150148  * This field is required.
    151149>>> p.cleaned_data
    152 Traceback (most recent call last):
    153 ...
    154 AttributeError: 'Person' object has no attribute 'cleaned_data'
     150{'last_name': u'Lennon'}
    155151>>> p['first_name'].errors
    156152[u'This field is required.']
    157153>>> p['first_name'].errors.as_ul()
  • docs/newforms.txt

     
    247247always cleans the input into a Unicode string. We'll cover the encoding
    248248implications later in this document.
    249249
    250 If your data does *not* validate, your ``Form`` instance will not have a
    251 ``cleaned_data`` attribute::
     250If your data does *not* validate, your ``Form`` instance will still have a
     251``cleaned_data`` attribute.  It will contain cleaned data from fields that
     252did validate::
    252253
    253254    >>> data = {'subject': '',
    254255    ...         'message': 'Hi there',
     
    258259    >>> f.is_valid()
    259260    False
    260261    >>> f.cleaned_data
    261     Traceback (most recent call last):
    262     ...
    263     AttributeError: 'ContactForm' object has no attribute 'cleaned_data'
     262    {'cc_myself': True, 'message': u'Hi there'}
    264263
     264**New in Django development version:** The ``cleaned_data`` attribute was
     265previously absent if the data for a ``Form`` instance did not validate. This
     266has changed.
     267
    265268``cleaned_data`` will always *only* contain a key for fields defined in the
    266269``Form``, even if you pass extra data when you define the ``Form``. In this
    267270example, we pass a bunch of extra fields to the ``ContactForm`` constructor,
Back to Top