DEV Community

Discussion on: Combining multiple forms in Flask-WTForms but validating independently

Collapse
 
jerrykco profile image
Jerry Kauffman

Hi Sam is their anyway to use session information to create a conditional around sections of the form?
I am using WTForms and have a long calibration data entry form. Their are essentially three parts to the form. Main (every unit we calibrate needs to have this data entered), Option 1 (this data only is needed if the equipment has this option installed), and Option 2 (same as option 1).
I can tell ahead of time by the model of the equipment if what the options are.
I have session variables which I can use to know if the option is true or not.
When I tried to check session.get('option') in the form class for my calibrate data entry, I get this.
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

I am thinking that I should either do what you did or perhaps make three separate forms and get them filled in succession conditionally.

Collapse
 
jerrykco profile image
Jerry Kauffman

I was able to fix the runtime error.
And I took a completely different approach using wtforms.readthedocs.io/en/2.3.x/sp....
So I have one form but based on the model options I have two functions (one for each option) that do a del form.variable for each of the variables that are not available because the option isn't included. Seems to work well.