Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing attributes to formfield via formconfig and template not working #76

Closed
ghinch opened this issue Jul 27, 2013 · 2 comments
Closed

Comments

@ghinch
Copy link

ghinch commented Jul 27, 2013

I'm trying to render some formfields and pass attributes via the template, to be passed to the widget, as I understood you could from the docs. It doesn't seem to be working. For example if I create a form like

import floppyforms as forms

class TestForm(forms.Form):
    test_input = forms.CharField(required=True)
    second_input = forms.CharField(required=False)

and a template like

{% load floppyforms %}
<!doctype html>
<html>
<body>
    {% form test_form using %}
        {% formfield form.test_input with placeholder="foo" %}
        {% formconfig field with placeholder="bar" for "second_input" %}
        {% formfield form.second_input %}
    {% endform %}
</body>
</html>

I would expect each field to get a placeholder. I did this to illustrate 2 different methods of passing the attribute I read in the docs, yet neither seems to work. Am I doing it wrong?

@gregmuellegger
Copy link
Collaborator

Hi I think there is a misunderstanding of what the arguments after with are doing.
With with you can pass additional template variables into the template that is used to render the widget. The template is then responsible for rendering those template variables into actual attributes of the widget. So to make your example work, you would need to overwrite the template floppyforms/input.html with the following content:

<input type="{{ type }}" name="{{ name }}"{% if value %} value="{{ value }}"{% endif %}{% if required %} required{% endif %}{% include "floppyforms/attrs.html" %}{% if datalist %} list="{{ attrs.id }}_list"{% endif %}{% if placeholder %} placeholder="{{ placeholder }}"{% endif %}>{% if datalist %}
<datalist id="{{ attrs.id }}_list">{% for item in datalist %}
    <option value="{{ item }}">{% endfor %}
</datalist>{% endif %}

Please especially note the section: {% if placeholder %} placeholder="{{ placeholder }}"{% endif %} which I have added.

@ghinch
Copy link
Author

ghinch commented Jul 30, 2013

The documentation should probably be updated then, as both the sections about formconfig and formfield are saying that my original implementation would work.

However, even if I pass a dict called attrs that looks like {'placeholder':'foo'}, that looks like it should be getting passed to attrs.html and written out, correct? The reason I'm having trouble is that it's going to be quite difficult for me to determine in the template which formfield template I need to use (input, select, etc.), as I'm just making a generic formrow template, and want to pass through things like placeholders and classnames

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants