I'm building a system which allows admin users to add "questions" to the database. Each type of question has a WTForms object associated with it. To display a page, I loop over all questions and generate a form consisting of form fields for each question.
class TextQuestionForm(Form):
value = TextField("Value", validators=[])
class Question(db.Model):
# sqlAlchemy model using single table inheritance
def field_name(self):
return "question_%s" % self.id
class TextQuestion(Question):
form = TextQuestionForm
def get_form(page_id):
questions = Question.query.filter(Question.page_id == page_id).all()
class F(Form):
pass
for q in questions:
setattr(F, q.field_name(), FormField(q.form))
return F()
This works well for simple cases where all validation is the same for a given question type, but I need to provide configurable validation options for each instance of Question, for example imagine if my Question model was extended:
class Question(db.Model):
# ... other fields
min_length = db.Column(db.Integer, nullable=True)
max_length = db.Column(db.Integer, nullable=True)
What would be the appropriate way to get a WTForms length validator on to the value field within my Form field, given that min/max lengths would be different (or missing) for each question?