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

Add empty option in SelectField #348

Closed
anselal opened this issue Oct 5, 2018 · 8 comments
Closed

Add empty option in SelectField #348

anselal opened this issue Oct 5, 2018 · 8 comments

Comments

@anselal
Copy link

anselal commented Oct 5, 2018

Hi there,

is there a way to add an empty option to a Selectfield ?
The only way I found is to add this to the choices before your actual choices:

[("", "-- select an option --")]

But this requires validation on the backend since this option should be disabled by default.

How can I disable this option so that the rendered tag would be like:

<option disabled="" selected="" value=""> -- select an option --</option>
@shaoeChen
Copy link

my way is:
first: set default on form filed
second: use '+' to add other option.

# form.py
class TestForm(FlaskForm):
    dropdownlist = SelectField('dropdownlist')

# view.py
@app.route('/your_route')    
def your_function():
    form = TestForm()
    select_option = Model.query.with_entities('key', 'value').all()

    form.dropdownlist.default = [('0', '-- select an option --')] + select_option
    return ...

of course you can add SelectField's option in Form init.
Then while i insert into database, i will change 0 to None, if you set column null=True.

@anselal
Copy link
Author

anselal commented Feb 22, 2019

Not exactly what I was hoping to...

@peterschutt
Copy link

peterschutt commented May 2, 2019

If you are using an ORM (sqlalchemy/django) then the wtforms QuerySelectField from the relevant wtforms extension have an allow_blank argument, where if True adds a blank field automatically.

E.g.:

from wtforms.ext.sqlalchemy.fields import QuerySelectField
class AForm(Form):
    category = QuerySelectField(
        query_factory=lambda: Category.query, 
        allow_blank=True
    )

It hard to look past if you are already querying through ORM to get select options.

Although the sqlalchemy extension is on the way out, to be replaced by WTForms-Alchemy in 3.0.

@pallets-eco pallets-eco deleted a comment from andy-takker Jan 11, 2021
@Uzay-G

This comment has been minimized.

@marrocelli
Copy link

marrocelli commented May 17, 2022

My workaround for this was to include a custom validation function to not allow the first option, which might be something like "-- select an option --". I know this is not exactly what you were looking for and also 4 years late, but I hope this helps somebody haha

# form.py
class MyForm(FlaskForm):
    def validate_option(self, option_to_check):
        if option_to_check == "-- select an option --":
            raise ValidationError("Please select a role.")

     option = SelectField(label="Choose an option", choices=[('-- select an option --'), ('Option 1'), ('Option 2'), ('Option 3'), 
    ('Other')])

@PanderMusubi
Copy link

Note that the text shown should also be supported by translation to other languages.

@vvalenzuela96
Copy link

The best way for disable an option or 'validate' a select field its the following:

the_field = SelectField(
    'The Field',
    choices=[('', 'Select any'), ('more_options', 'More Options')],
    validators=[InputRequired()],
    default=''
    )

Note:
Add these imports

from wtforms import SelectField
from wtforms.validators import InputRequired

@azmeuk
Copy link
Member

azmeuk commented Jan 13, 2023

Disabling the option with a HTML disabled attribute will come with pallets-eco/wtforms#739
You should still validate your field with InputRequired() though.

@azmeuk azmeuk closed this as completed Jan 13, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

8 participants