Code

Opened 6 years ago

Last modified 2 years ago

#9153 new Bug

Forms (and formsets) don't always generate valid HTML for hidden fields

Reported by: mtredinnick Owned by: nobody
Component: Forms Version: 1.0
Severity: Normal Keywords:
Cc: asendecka@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Creating a form with only hidden fields and than calling as_p() on it generates invalid (X)HTML. The input elements must reside inside certain block-level elements and this doesn't always happen. In r9067 I made a change that made this work when displaying error messages for such fields, but the general case is trickier.

The problem is that we can't just apply the same change as r9067 because that would introduce, for example, an empty table row in the as_table() method. This normally wouldn't be an issue (why, exactly, are you calling as_p() on a form that doesn't display anything?), but it arises fairly naturally with formsets. The management form for formsets is a form that only contains hidden fields. Thus, any formset display is slightly invalid. Fixing this doesn't appear to be entirely trivial, so I didn't drop in any kind of patch around the time of r9067.

There are some tests in regressiontests/forms/forms.py that actually demonstrate the problem here: except they consider the invalid output to be valid. So any patch fixing this will cause a couple of tests for hidden fields to "fail" and the tests will need to be fixed.

Attachments (0)

Change History (5)

comment:1 Changed 5 years ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 3 years ago by lukeplant

  • Severity set to Normal
  • Type set to Bug

comment:3 follow-up: Changed 3 years ago by anonymous

  • Easy pickings unset
  • UI/UX unset

When using ajax to load a formset, this can cause improper rendering in the browser. In my case the formset content is sent using HttpResponse(formset.as_table(),mimetype='application/text'). The management form at the beginning of the formset is not in a <tr>. The result is that the tr tags are removed from the entire formset (by the browser when jQuery.html(data) is called).

comment:4 in reply to: ↑ 3 Changed 3 years ago by Nate

Replying to anonymous:

When using ajax to load a formset, this can cause improper rendering in the browser. In my case the formset content is sent using HttpResponse(formset.as_table(),mimetype='application/text'). The management form at the beginning of the formset is not in a <tr>. The result is that the tr tags are removed from the entire formset (by the browser when jQuery.html(data) is called).

The following jQuery patched this specific case:
function fixManagement(astable) {

var mngForm = astable.split('<tr>', 1);
var trIndex = astable.indexOf('<th>',1)+4;
var latter = astable.substr(trIndex);
return '<tr><th>'+ mngForm + latter;

}

comment:5 Changed 2 years ago by ethlinn

  • Cc asendecka@… added

I could not reproduce the bug, but I'm not sure, if I understand the ticket well. Does anybody have an example that generates invalid (X)HTML?

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.