How to use SelectMultipleField using wtforms

Viewed 223

I'm having trouble understanding the documentation of SelectMultipleField. it's written that

"You’ll need to specify the HTML size attribute to the select field when rendering."

and I don't understand where do I specify exactly.

attached is the code (which worked) for the select field, what changes should I do for the SelectMultipleField?

form.py

domains = SelectField(label='Domain', choices=DOMAINS, validate_choice=True, validators=[InputRequired()])

html

    <div class="form-group">
        {{ form.domains.label(class="form-control-label") }}
        {% if form.domains.errors %}
            {{ form.domains(class="form-control form-control-lg is-invalid") }}
            <div class="invalid-feedback">
                {% for error in form.domains.errors %}
                    <span>{{ error }}</span>
                {% endfor %}
            </div>
        {% else %}
            {{ form.domains(class="form-control form-control-lg") }}
        {% endif %}
    </div>
1 Answers

The 'size' attribute specifies how many options of the select field you want to show in the scrollbox. For more information look at https://www.w3schools.com/TAGS/att_size.asp

As you did, with WTF library, you can specify HTML attributes between round brackets like 'class', but many others are aviable and size is one of them, so, to fix your problem, you just need to type

{{ form.domains(class="form-control form-control-lg", size=x) }}

where x stands for the number of options you want to be visible.

Related