- Define a form
- Add string field
- Add radio field, set default value
- Add submit field
from flask_wtf import FlaskForm
from wtforms import StringField, RadioField, SubmitField, validators, ValidationError
class WeatherForm(FlaskForm):
location = StringField()
unit = RadioField(choices=[('metric', '\u00b0C'), ('imperial', '\u00b0F')],
default='metric')
search = SubmitField(label='Search')
history = SubmitField(label='History')
help = SubmitField(label='Help')
wrong_data = SubmitField(label='Wrong Data?')
Build-in validators
- Add InputRequired validator
- Add Length validator
from wtforms import StringField, validators
location = StringField(validators=[validators.Length(max=50)])
description = StringField(label='Description', validators=[validators.InputRequired(message='Description is required.'),validators.Length(max=50, message='Description cannot be longer than 50 characters.'))
Custom validator
- Custom validator for string field: description
- If the input description is not valid, raise error
- Valid descriptions are stored in a list
- The items in the list are queried from database with distinct, then use join to convert the tuple to string.
from wtforms import StringField, validators, ValidationError
from sqlalchemy import distinct
from weather import db
from weather.models import Description
class ValidateDescription(object):
def __init__(self, message=None):
if not message:
message = 'Description is not valid, please check help.'
self.message = message
def __call__(self, form, field):
valid_weather = []
for d in db.session.query(distinct(Description.description)).all():
d = ''.join(d)
valid_weather.append(d)
if field.data.strip() not in valid_weather:
raise ValidationError(self.message)
class UpdateForm(FlaskForm):
description_validator = ValidateDescription()
description = StringField(label='Description', validators=[validators.InputRequired(message='Description is required.'), validators.Length(max=50, message='Description cannot be longer than 50 characters.'), description_validator])