Closed jleclanche closed 9 years ago
The problem here is that the hidden widget and a non-hidden widget (HiddenMapInput
in your example) are two separate things. And it's not trivial to get from the one to the other. Imagine a <select>
widget that requires totally different attributes than a <input type="hidden" />
. Maybe it would be good to have a construct_hidden_widget
method on the form field? That would solve the problem since you could control the hidden widget entirely from there. But then we would also need to modify the BoundField
class slightly which don't do yet.
It might still be worth thinking about..
@gregmuellegger I don't see why HiddenMapInput
would not be a hidden widget since it inherits from HiddenInput
?
I just stumbled upon the same problem and I can only repeat what @Adys said with the following example:
import floppyforms as forms
class MyForm(forms.Form):
my_field = forms.CharField(widget=forms.HiddenInput(attrs={'ng-model': 'form.my_field'}))
The execution of {{ field.as_hidden }}
in templates/floppyforms/rows/p.html
then causes the field not to render the "attrs" because the "attrs" are not passed to django.forms.BoundField.as_hidden()
.
Am I missing something?
I see that this might be non-intuitive. But it is exactly the same behaviour that Django shows.
When using Django 1.7:
>>> from django import forms
>>> class MyForm(forms.Form):
... my_field = forms.CharField(widget=forms.HiddenInput(attrs={'ng-model': 'form.my_field'}))
...
...
>>> form = MyForm()
>>> print form['my_field']
<input id="id_my_field" name="my_field" ng-model="form.my_field" type="hidden" />
>>> print form['my_field'].as_hidden()
<input id="id_my_field" name="my_field" type="hidden" />
And when using django-floppyforms 1.3.0:
>>> import floppyforms as forms
>>> class MyForm(forms.Form):
... my_field = forms.CharField(widget=forms.HiddenInput(attrs={'ng-model': 'form.my_field'}))
...
...
>>> form = MyForm()
>>> print form['my_field']
<input type="hidden" name="my_field" required ng-model="form.my_field" id="id_my_field">
>>> print form['my_field'].as_hidden()
<input type="hidden" name="my_field" id="id_my_field">
I think django-floppyforms should only replicate Django's behaviour as good as possible. After all it is design as a drop-in replacement. So I'm sorry for closing this as a won't-fix as I see your point.
Maybe that is something that you would like to address in Django core?
This took me by surprise while porting an existing app to use floppyforms. All of a sudden, the input type="hidden" in my ModelForm no longer had any of their extra attributes
Slightly reduced code:
I tracked it down to templates/floppyforms/layouts/p.html, in the formconfig line for hidden fields. We then end up here in
templates/floppyforms/rows/p.html
:{% block hidden_fields %}{% for field in hidden_fields %}{{ field.as_hidden }}{% endfor %}{% endblock %}
This calls django.forms.BoundField.as_hidden(), which takes an attrs=None argument. So of course the extra attributes disappear here.
Switching to just
{{ field }}
fixes the issue but I'm not sure about the implications.