Django

Code

Changeset 9067

Show
Ignore:
Timestamp:
09/18/08 02:16:08 (2 months ago)
Author:
mtredinnick
Message:

Fixed #9125 -- When displaying errors for a form with only hidden fields, make sure the resulting XHTML is correct.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/forms/forms.py

    r8817 r9067  
    172172                # Chop off the trailing row_ender (e.g. '</td></tr>') and 
    173173                # insert the hidden fields. 
     174                if not last_row.endswith(row_ender): 
     175                    # This can happen in the as_p() case (and possibly others 
     176                    # that users write): if there are only top errors, we may 
     177                    # not be able to conscript the last row for our purposes, 
     178                    # so insert a new, empty row. 
     179                    last_row = normal_row % {'errors': '', 'label': '', 'field': '', 'help_text': ''} 
     180                    output.append(last_row) 
    174181                output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender 
    175182            else: 
  • django/trunk/tests/regressiontests/forms/regressions.py

    r8761 r9067  
    8989>>> f.cleaned_data 
    9090{'data': u'xyzzy'} 
     91 
     92A form with *only* hidden fields that has errors is going to be very unusual. 
     93But we can try to make sure it doesn't generate invalid XHTML. In this case, 
     94the as_p() method is the tricky one, since error lists cannot be nested 
     95(validly) inside p elements. 
     96 
     97>>> class HiddenForm(Form): 
     98...     data = IntegerField(widget=HiddenInput) 
     99>>> f = HiddenForm({}) 
     100>>> f.as_p() 
     101u'<ul class="errorlist"><li>(Hidden field data) This field is required.</li></ul>\n<p> <input type="hidden" name="data" id="id_data" /></p>' 
     102>>> f.as_table() 
     103u'<tr><td colspan="2"><ul class="errorlist"><li>(Hidden field data) This field is required.</li></ul><input type="hidden" name="data" id="id_data" /></td></tr>' 
     104 
    91105"""