-1
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, SelectField
from wtforms.validators import DataRequired, ValidationError
from datetime import datetime
from meeting_app.models import Room


class ReservingForm(FlaskForm):
  employee = StringField("Employee", validators=[DataRequired()])
  start_date = StringField("start", validators=[DataRequired(), check_date])
  end_date = StringField("end",validators=[check_end_date])
  start_time = StringField("start-date", validators=[DataRequired(),check_time])
  end_time = StringField("end-date", validators=[DataRequired(), check_time])
  room = SelectField("Meeting Room", 
                    choices=[ ("", "Choose Meeting Room"),("Room 1", "Room 1"),
                    ("Room 2", "Room 2"),("Room 3", "Room 3"),("Room 4", "Room 4")], 
                    validators=[DataRequired()],)

I'm trying to change the room selectfield with the code that I wrote below...

  cr_room = Room.query.all()
  room = SelectField("Meeting Room", 
        choices=[ ((room.created_room),(room.created_room)) for room in cr_room ], 
        default=("", "Choose Meeting Room"), coerce=str)

but it doesn’t work, I don’t see the selectfield default value on the page.

Flask==1.1.2 Flask-WTF==0.14.3 WTForms==2.3.1

hamona
  • 23
  • 7
  • Does this answer your question? [How do you set a default value for a WTForms SelectField?](https://stackoverflow.com/questions/12099741/how-do-you-set-a-default-value-for-a-wtforms-selectfield) – Ken Kinder Jun 14 '20 at 15:37
  • no, it does not work for me. im runnig newer version wtforms (version 2.3.1) – hamona Jun 14 '20 at 16:23
  • The interface to setting a value hasn't changed in a very long time. – Ken Kinder Jun 14 '20 at 16:23
  • Does not work, I tried all these answers from the stackoverflow, then I wrote here. i can't find a problem, teried to write like that `code` form = ReservingForm() form.room.default =("", "Choose Meeting Room") form.process() `code` in the route.py file, and it didn't work... – hamona Jun 14 '20 at 16:32
  • Have you tried using an actual value instead of an empty string? i.e. `default="default"`? – PGHE Jun 15 '20 at 03:42
  • Yes it work in empty string. – hamona Jun 16 '20 at 17:27

1 Answers1

0

Not finding a solution, I solved my problem in the following way ...

cr_room = Room.query.all()
room = SelectField("Meeting Room", 
          choices=[("","Choose meeting room")]+[((room.created_room),(room.created_room)) for room in cr_room ], coerce=str)
hamona
  • 23
  • 7