from wtforms.validators import ValidationError def value(min=0, max=None): message = f'Value must be between {min} and {max}.' def _length(form, field): value = field.data or 0 if value < min or max != None and value > max: raise ValidationError(message) return _length