Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, redirect, url_for, render_template
- from flask_wtf import FlaskForm
- from wtforms import StringField, FieldList, FormField, SubmitField, HiddenField, Label
- from wtforms.validators import DataRequired
- app = Flask(__name__)
- app.config['SECRET_KEY'] = 'SECRET_KEY-SECRET_KEY-SECRET_KEY'
- # subforms
- class SubForm(FlaskForm):
- # how to handle hidden id that I can use to properly commit that on submit?
- # entry_type_id = HiddenField()
- # validators for subforms don't work, but that's something I'll try to address later
- content = StringField(validators=[DataRequired()])
- # I use custom __init__ set custom label for the field - or rather I try, as it doesn't work..
- def __init__(self, custom_label=None, *args, **kwargs):
- # not sure if safe - even just for the subform! #
- # Without that, I get 'TypeError: argument of type 'CSRFTokenField' is not iterable' on main_form.validate_on_submit()
- kwargs['csrf_enabled'] = False
- FlaskForm.__init__(self, *args, **kwargs)
- if custom_label is not None:
- self.content.label = Label(self.content.id, custom_label)
- print(f'INIT // id: [{self.content.id}] // content.data: [{self.content.data}] // label: [{self.content.label.text}]')
- # main forms
- class MainForm(FlaskForm):
- title = StringField('title')
- entries = FieldList(FormField(SubForm))
- submit = SubmitField('Post')
- @app.route("/test", methods=['GET', 'POST'])
- def test_route():
- # the main form
- main_form = MainForm(title='title')
- # sub forms, created before validate_on_submit()
- sub_form_1 = SubForm(content='Default answer 1', custom_label='Question 1')
- sub_form_2 = SubForm(content='Default answer 2', custom_label='Question 2')
- main_form.entries.append_entry(sub_form_1)
- main_form.entries.append_entry(sub_form_2)
- if main_form.validate_on_submit():
- for entry in main_form.entries.entries:
- print(f'LOOP // id: [{entry.content.id}] // content.data: [{entry.content.data}] // label: [{entry.content.label.text}]')
- return redirect(url_for('test_route'))
- print(f'INSTANCE_1 // id: [{sub_form_1.content.id}] // content.data: [{sub_form_1.content.data}] // label: [{sub_form_1.content.label.text}]')
- print(f'INSTANCE_2 // id: [{sub_form_2.content.id}] // content.data: [{sub_form_2.content.data}] // label: [{sub_form_2.content.label.text}]')
- return render_template('test_form.html', title='Test Form', main_form=main_form, legend='Test Form')
- if __name__ == '__main__':
- app.run(debug=True)
- # I have a whole list of issues here
- # 1) The main problem is that I can't read the subforms data in the .validate() when I submit the form
- # 2) The other thing is that I can't force the labels to show the custom values That I want to set dynamically
- # 3) I need to do more reading on handling csfr in subforms and how to work around that as well
- # 4) And the last one - how do I validate the subforms - for required fields, length, etc
- # 1 & 2 is my main concern now and I have a feeling that the issues have the same root-cause
- # My gut feeling tells me that the broken element id is meaningful
- # ('content' for each subform string field, instead of indexed 'entries-0-content' - that I see on submit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement