Ticket #5524: ticket_5524__rev_6822.diff

File ticket_5524__rev_6822.diff, 11.4 KB (added by Ben Slavin, 17 years ago)

Now with docs and tests.

  • 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        """
     
    290288    def _data(self):
    291289        """
    292290        Returns the data for this BoundField, or None if it wasn't given.
     291        If the data has been cleaned, that value is used; otherwise, the value
     292        is provided by value_from_datadict.
    293293        """
    294         return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
     294        if hasattr(self.form, 'cleaned_data') and self.name in self.form.cleaned_data:
     295            return self.form.cleaned_data[self.name]
     296        else:
     297            return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
    295298    data = property(_data)
    296299
    297300    def label_tag(self, contents=None, attrs=None):
  • tests/modeltests/model_forms/models.py

     
    163163>>> f.errors
    164164{'name': [u'This field is required.'], 'slug': [u'This field is required.']}
    165165>>> f.cleaned_data
    166 Traceback (most recent call last):
    167 ...
    168 AttributeError: 'CategoryForm' object has no attribute 'cleaned_data'
     166{'url': u'foo'}
    169167>>> f.save()
    170168Traceback (most recent call last):
    171169...
  • tests/regressiontests/forms/extra.py

     
    159159rendering as well.
    160160
    161161>>> print a['mydate'].as_hidden()
    162 <input type="hidden" name="mydate" value="2008-4-1" id="id_mydate" />
     162<input type="hidden" name="mydate" value="2008-04-01" id="id_mydate" />
    163163>>> b=GetDate({'mydate':'2008-4-1'})
    164164>>> print b.is_valid()
    165165True
  • tests/regressiontests/forms/forms.py

     
    4343>>> print p['last_name']
    4444<input type="text" name="last_name" value="Lennon" id="id_last_name" />
    4545>>> print p['birthday']
    46 <input type="text" name="birthday" value="1940-10-9" id="id_birthday" />
     46<input type="text" name="birthday" value="1940-10-09" id="id_birthday" />
    4747>>> print p['nonexistentfield']
    4848Traceback (most recent call last):
    4949...
     
    5353...     print boundfield
    5454<input type="text" name="first_name" value="John" id="id_first_name" />
    5555<input type="text" name="last_name" value="Lennon" id="id_last_name" />
    56 <input type="text" name="birthday" value="1940-10-9" id="id_birthday" />
     56<input type="text" name="birthday" value="1940-10-09" id="id_birthday" />
    5757>>> for boundfield in p:
    5858...     print boundfield.label, boundfield.data
    5959First name John
    6060Last name Lennon
    61 Birthday 1940-10-9
     61Birthday 1940-10-09
    6262>>> print p
    6363<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr>
    6464<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="Lennon" id="id_last_name" /></td></tr>
    65 <tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></td></tr>
     65<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-09" id="id_birthday" /></td></tr>
    6666
    6767Empty dictionaries are valid, too.
    6868>>> p = Person({})
     
    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>
     
    130128Unicode values are handled properly.
    131129>>> p = Person({'first_name': u'John', 'last_name': u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111', 'birthday': '1940-10-9'})
    132130>>> p.as_table()
    133 u'<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr>\n<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></td></tr>\n<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></td></tr>'
     131u'<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" value="John" id="id_first_name" /></td></tr>\n<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></td></tr>\n<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" value="1940-10-09" id="id_birthday" /></td></tr>'
    134132>>> p.as_ul()
    135 u'<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></li>\n<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></li>\n<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></li>'
     133u'<li><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></li>\n<li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></li>\n<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-09" id="id_birthday" /></li>'
    136134>>> p.as_p()
    137 u'<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></p>\n<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></p>\n<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-9" id="id_birthday" /></p>'
     135u'<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" value="John" id="id_first_name" /></p>\n<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" /></p>\n<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" value="1940-10-09" id="id_birthday" /></p>'
    138136
    139137>>> p = Person({'last_name': u'Lennon'})
    140138>>> p.errors
     
    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()
     
    802798<tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr>
    803799<tr><th>First name:</th><td><input type="text" name="first_name" value="John" /></td></tr>
    804800<tr><th>Last name:</th><td><input type="text" name="last_name" value="Lennon" /></td></tr>
    805 <tr><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></td></tr>
     801<tr><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10-09" /><input type="hidden" name="hidden_text" /></td></tr>
    806802>>> print p.as_ul()
    807803<li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
    808804<li>First name: <input type="text" name="first_name" value="John" /></li>
    809805<li>Last name: <input type="text" name="last_name" value="Lennon" /></li>
    810 <li>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></li>
     806<li>Birthday: <input type="text" name="birthday" value="1940-10-09" /><input type="hidden" name="hidden_text" /></li>
    811807>>> print p.as_p()
    812808<ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul>
    813809<p>First name: <input type="text" name="first_name" value="John" /></p>
    814810<p>Last name: <input type="text" name="last_name" value="Lennon" /></p>
    815 <p>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></p>
     811<p>Birthday: <input type="text" name="birthday" value="1940-10-09" /><input type="hidden" name="hidden_text" /></p>
    816812
    817813A corner case: It's possible for a form to have only HiddenInputs.
    818814>>> class TestForm(Form):
     
    12471243>>> print p.as_ul()
    12481244<li><label for="id_person1-first_name">First name:</label> <input type="text" name="person1-first_name" value="John" id="id_person1-first_name" /></li>
    12491245<li><label for="id_person1-last_name">Last name:</label> <input type="text" name="person1-last_name" value="Lennon" id="id_person1-last_name" /></li>
    1250 <li><label for="id_person1-birthday">Birthday:</label> <input type="text" name="person1-birthday" value="1940-10-9" id="id_person1-birthday" /></li>
     1246<li><label for="id_person1-birthday">Birthday:</label> <input type="text" name="person1-birthday" value="1940-10-09" id="id_person1-birthday" /></li>
    12511247>>> print p['first_name']
    12521248<input type="text" name="person1-first_name" value="John" id="id_person1-first_name" />
    12531249>>> print p['last_name']
    12541250<input type="text" name="person1-last_name" value="Lennon" id="id_person1-last_name" />
    12551251>>> print p['birthday']
    1256 <input type="text" name="person1-birthday" value="1940-10-9" id="id_person1-birthday" />
     1252<input type="text" name="person1-birthday" value="1940-10-09" id="id_person1-birthday" />
    12571253>>> p.errors
    12581254{}
    12591255>>> p.is_valid()
  • 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, but it will only contain 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
    265264``cleaned_data`` will always *only* contain a key for fields defined in the
    266265``Form``, even if you pass extra data when you define the ``Form``. In this
Back to Top