Functionality of Embedded Forms

Hi all! I used the excellent documentation and prior Form Q&A to learn how to embed a Form as a component in another form. I can do this now when the embedded forms lack any dynamic functionality, e.g., links to other forms, or user responses to questions that I want to save to the data tables. I would like to continue to use this approach (swapping out form components) rather than switching to entirely new forms for web pages. It should save me time, and it seems like an elegant way to design my website.

My problem is that I do not know how to add functionality within the component forms. I had thought that this would be easy, but the solution eludes me. Any suggestions? Thanks in advance.

Drew

Congratulations on learning those concepts in Anvil!

If I understand correctly, you may be looking for a way to access the inner components and handle events when you add your forms programmatically.

To access the components inside the added form, you can use the dot syntax on the instance of you newly added class (i.e., the nested form). For example, if you have added an instance of Form2 to Form1, and you want to “reach into” Form2 to do something with one of its components, you might do something like the following.

from Form2 import Form2

my_nested_form = Form2()
my_nested_form.link_1.url="www.google.ca"

If you want to set and respond to events programmatically, please read these docs (specifically set_event_handler).

Note that when you create your new instance, you can also pass variables to its init function if needed. For example:

from Form2 import Form2

some_variable="www.google.ca"

# positional/keyword args can be used as normal in Python
# Form2 to can now make use of your variable in its init
my_nested_form = Form2(some_variable) 
2 Likes

That looks like the solution. I will give it a try. Looks pretty essential for what I want to do. Thank you.

1 Like

Hi @drew.carson,

These tutorials and project may help you:
https://anvil.works/docs/how-to/custom-user-auth

1 Like

Thanks @Tony.Nguyen ! I had worked through that project but will go back and review it.