Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Is it possible to create a dynamic/adaptive forms with WTForms? #825

Closed
hasansezertasan opened this issue Jan 6, 2024 · 2 comments

Comments

@hasansezertasan
Copy link

I'm sorry if the title wasn't clear enough but I have an example that demonstrates what I need.

I have an example that I built using json-editor/json-editor and JSON Schema.

Here is the direct link to that example: click

Here are the screenshots:

image

As you can see, there is a dropdown called "Content" which lets you choose the type of content. Depending on the content type, the form changes.

image

In this image, I chose "Attachment", which requires some fields that other types don't have.

image

In this image, I chose the "Text" type, and the form automatically transforms.

image

As you can see, if I chose the "Image" type, the form adopts its fields.

The best workaround I can think of to implement something similar to this is to create three different forms and put them in separate tabs but if someone fills the form and decides to change the type of the form, the filled part will not be in other type. Is there any implementation or a way to make this kind of form in WTForms?

@hasansezertasan hasansezertasan changed the title Question: Is it possible to create a dynamic forms with WTForms? Question: Is it possible to create a dynamic/adaptive forms with WTForms? Jan 6, 2024
@davidism
Copy link
Member

davidism commented Jan 6, 2024

Yes, it is possible to process dynamic forms using field lists and nested form fields. You still need JavaScript to handle building the dynamic changes to the forms.

@davidism davidism closed this as completed Jan 6, 2024
@hasansezertasan
Copy link
Author

I wanted to stay in *.py files and do the things I wanted to do without creating any front-end files.

For anyone who met this issue: this can be easily done without writing a single line of JavaScript using AlpineJS or hyperscript.

Just use the render_kw argument of the fields, then you are good to go!

As for the validation, use If validator from wtforms-components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants