#11632 closed (fixed)
show_hidden_initial=True produces invalid HTML
| Reported by: | Owned by: | Karen Tracey | |
|---|---|---|---|
| Component: | Forms | Version: | 1.1 |
| Severity: | Keywords: | show_hidden_initial | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When you have a model like this:
class MyModel(models.Model):
datetime = models.TimestampField(_('Affected Time Start'), default=datetime.datetime.now)
And a Form like this:
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('datetime',)
The generated HTML (in my case with prefix=2) looks like that:
<input type="text" name="2-datetime" value="2009-08-04 11:13:40" id="id_2-datetime" /><input type="hidden" name="initial-2-datetime" value="2009-08-04 11:13:40.694547" id="id_2-datetime" />
Obviously, the HTML ID is the same in the hidden field, which causes some JS-trouble.
Hope you'll fix it soon.
Cheers.
Attachments (1)
Change History (18)
comment:1 by , 16 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:2 by , 16 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
| Triage Stage: | Unreviewed → Accepted |
comment:3 by , 16 years ago
| Resolution: | fixed |
|---|---|
| Status: | closed → reopened |
comment:4 by , 16 years ago
| Owner: | removed |
|---|---|
| Status: | reopened → new |
comment:5 by , 16 years ago
| Keywords: | show_hidden_initial added |
|---|---|
| milestone: | → 1.2 |
| Summary: | default=datetime.datetime.now in ModelField creates dublicate HTML-IDs in ModelForm → show_hidden_initial=True produces invalid HTML |
| Version: | 1.1 → SVN |
comment:6 by , 16 years ago
| Version: | SVN → 1.1 |
|---|
Ok, i think better render id differently in as_widget() method if only_initial is True. Does we really need id for the hidden initial field?
follow-up: 8 comment:7 by , 16 years ago
I've just encountered this issue myself. I'm conflicted on what the solution should be. Since the <name> field gets 'initial-' prefixed to it, it seems like it would be consistent ti prefix the 'initial-' on the id. However, in practice that has issues. I have jquery code that finds dom objects by searching for them like this:
row.find('[id$=foo]')
ie, this finds a dom element whose name ends in 'foo'. Once I started using show_hidden_initial, my jquery code broke since it now finds two items whose name ends in 'foo'. I suspect other folks might do similar types of selectors. Adding 'initial-' to the front of the hidden initial input wouldn't help in this situation. Of course I could do a more compex jquery selector and ignore stuff that starts with initial-.
Getting rid of the id altogether on the hidden initial element fixes my issue, but leaves one in the unpleasant situation of never being able to find that hidden initial element in your dom - or at least not with a lot of trouble. So I don't really think it's a great solution. It seems like the most complete solution would allow the user to specify a "hidden initial auto id" when they instantiate a form, similar to the way they can specify the auto_id argument when they instantiate a form.
comment:8 by , 16 years ago
Sorry, the above comment by "anonymous" is by me, forgot to log in before making it.
by , 16 years ago
| Attachment: | show_hidden_initial.diff added |
|---|
follow-up: 16 comment:9 by , 16 years ago
| Has patch: | set |
|---|
While it's a shame that it doesn't help your jQuery selector I think using the 'initial-' prefix is a consistent with how the field names are generated. I added a patch that generates prefix to the id on the hidden initial field. I think as long as it is clear and consistent how the names and ids will be generated people can make the rest of their project work as far as selecting the elements and not generating conflicting names/ids.
comment:10 by , 16 years ago
| Owner: | set to |
|---|
comment:11 by , 16 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
comment:12 by , 16 years ago
comment:13 by , 16 years ago
| Resolution: | fixed |
|---|---|
| Status: | closed → reopened |
Reopening to fix broken test resulting from this fix.
comment:14 by , 16 years ago
| Resolution: | → fixed |
|---|---|
| Status: | reopened → closed |
comment:15 by , 16 years ago
comment:16 by , 16 years ago
Replying to mlavin:
While it's a shame that it doesn't help your jQuery selector I think using the 'initial-' prefix is a consistent with how the field names are generated. I added a patch that generates prefix to the id on the hidden initial field. I think as long as it is clear and consistent how the names and ids will be generated people can make the rest of their project work as far as selecting the elements and not generating conflicting names/ids.
Thanks for putting the patch in, Mark. I agree that prefixing with initial- is a good and consistent fix.
This bug is valid for any field with show_hidden_initial=True
Another test case:
from django import forms from django.db import models class MyModel(models.Model): # callable default sets show_hidden_initial to True field1 = models.IntegerField(default=lambda: 0) class Meta: app_label = 'dummy' class MyForm(forms.ModelForm): field2 = forms.CharField(max_length=50, show_hidden_initial=True) class Meta: model = MyModel print MyForm()Outputs duplicate ids:
Looks like
__unicode__()oras_hidden()in BoundField must be fixed.