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

Adding custom attributes for option tags within SelectField and SelectMultipleField #81

wants to merge 2 commits into from


Copy link

@playpianolikewoah playpianolikewoah commented May 6, 2014

Currently you do not allow the user to create html attributes on <option> tags within SelectField and SelectMultipleField. For example if a user wanted <option data-id="3"> it is not possible currently.

Copy link

@crast crast commented May 6, 2014

Without any changes to the current code base, simply using the built-in feature of iterating the fields to render the options:

>>> from wtforms import SelectField, Form
>>> class F(Form):
...    a = SelectField(choices=[('a', 'Apple'), ('b', 'Banana')])
>>> i = 44
>>> form = F()
>>> for subchoice in form.a:
...     print subchoice(**{'data-id': i})
...     i += 1
<option data-id="44" value="a">Apple</option>
<option data-id="45" value="b">Banana</option>

Granted, the way to send data- params is a bit funky right now, but we're going to have a solution for that very soon.

@crast crast closed this May 6, 2014
Copy link

@playpianolikewoah playpianolikewoah commented May 6, 2014

Thank you! That does make sense.
Do I manually turn this into a field by concatenating <select> tags before and after to make a select field? As I understand it this solution gives me the <option> tags but not the surrounding <select> tags. With a regular SelectField I could just call field() to get the whole select with its options. So I guess this does not solve the issue completely but is a step in the right direction.

Copy link

@dubbl dubbl commented Jun 29, 2015

Did something change regarding this issue?
I get the workaround, but would like to keep my view clean and define all the choices in one place at the form initialization.
Will wtforms 3 change something in that regard? Thanks in advance.

@wtforms wtforms deleted a comment from roipoussiere Jun 2, 2020
@wtforms wtforms deleted a comment from trentkg Jun 2, 2020
@wtforms wtforms deleted a comment from robinclark007 Jun 2, 2020
@wtforms wtforms deleted a comment from goodwordalchemy Jun 2, 2020
@wtforms wtforms locked as resolved and limited conversation to collaborators Jun 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

Successfully merging this pull request may close these issues.

None yet

3 participants