Skip to content
Advertisement

Deleting FlaskForm Fields depending on userinput in other forms

I have 3 forms with Checkboxes to configure the desired form (Final_Form). After the user chooses the desired fields (in form1, form2 and form3), i want to delet all fields that are not required in the final form and render the final form. The reason for that is, that i have 3 Subkategories with around 12 possible values, in each form (form1-form3) the user can choose one ore more subkategories. The subcategories are standardized and are used to describe a clinical incident. The users wished to have the subcategories (1-3; form1-form3) seperated and always with an example (right-side of the screen in an anther bootstrap col). The finalform is than a combination of the the subcategories that matches best to describe the clinical incident. All fields in the Final_Form are TextAreaFields. The Input for the TextAreaFields is stored in a sqlite-db.

Here is how i tried it:

app.py:

if request.method == 'POST' and form1.form1Submit.data:
     OnePointOne = form1.OnePointOne.data
     if not OnePointOne:
         del Final_Form.OnePointOne
     return render_template('Form2.html', form2 = form2)
if request.method == 'POST' and form2.form2Submit.data:
     TwoPointTwo = form2.TwoPointTwo.data
     if not TwoPointTwo:
         del Final_Form.TwoPointTwo
     return render_template('Form3.html', form3 = form3)

if request.method == 'POST' and form3.form3Submit.data:
     ThreePointThree = form3.ThreePointThree.data
     if not ThreePointThree:
         del Final_Form.ThreePointThree
     return render_template('Final.html', Final_Form = Final_Form)

forms.py:

class form1(FlaskForm):
    OnePointOne = BooleanField('Effect')
    form1Submit = SubmitField('Submit Category')

class form2(FlaskForm):
    TwoPointTwo = BooleanField('Measure')
    form2Submit = SubmitField('Submit Category')

class form3(FlaskForm):
    ThreePointThree = BooleanField('Result')
    form3Submit = SubmitField('Submit Category')

class Final_Form(FlaskForm):
    OnePointOne = TextAreaField('Example Effect')
    TwoPointTwo = TextAreaField('Example Measure')
    ThreePointThree = TextAreaField('Example Result')
    Final_FormSubmit = SubmitField('Submit incident')

The problem is, that the formfields of the Final_Form objects dont get deleted (only inside the if statements). I am very thankful for every hint or explanation.

Advertisement

Answer

As you are showing three separate pages, there are three separate requests. You Final_Form object cannot be simply kept between these requests.

I don’t fully understand why you configure your third form this way, it would be helpful to explain your use-case for better advice.

Without more information, I’m thinking of some ways to do this:

  • You make it one page/request, where you go from form to form using AJAX.
  • You make it one page with all forms, controlling visualisation with JS + CSS
  • You save your desired value somewhere
    • probably you can keep it in cookie (session object)
    • or in database, if that makes sense in your context

Also, please include whole code of this function – it’s not clear how you create those forms you use.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement