I am trying to create a SelectField
or SelectMultipleField
that allows me to add attributes to it's <option>
tags. I am trying to add attributes like data-id
or another data-____
. I have not been able to figure out how to do this as it only seems possible to add attributes to the <select>
tag itself and not the options.
The end result should be something like:
<select id="regularstuff-here" name="regular-name-here">
<option value="1" data-id="somedata here" >Some Name here</option>
<option value="2" data-id="somedata here" >Some Name here</option>
</select>
I assume I have to create a custom widget. If I look at the source for WTForms I see that select
widget calls:
html.append(self.render_option(val, label, selected))
If I look at that method:
@classmethod
def render_option(cls, value, label, selected, **kwargs):
options = dict(kwargs, value=value)
if selected:
options['selected'] = True
return HTMLString('<option %s>%s</option>' % (html_params(**options),
escape(text_type(label))))
So it does not seem that you can pass any extra params to the method that renders the option
tags.
WTForms
project. – Malarkey