Check if form is in an alert

Is there a way to know if a form has been opened in an alert?

We often have forms that are re-usable - sometimes as custom components, sometimes as routed-to stand-alone forms, and sometimes as alerts.

Is there a way to check if your form is “in” an alert?

A common example: Whether to show a “Cancel” button (and sometimes a “save” button). We’ll often pass as_alert=True as an optional keyword argument to control that visibility but I’m wondering if there’s a way for a form to introspect whether it’s loaded in an alert?

I thought about using self.parent but when loaded in an alert self.parent is None… maybe we could use that fact, but I’m never sure when self.parent is actually set.

There’s no built in way to do this

But a possible approach is to check if you have the x-close-alert event handler.
Which anvil will add if you are a form in an alert.


    def form_show(self, **event_args):
        in_alert = bool(self.get_event_handlers("x-close-alert"))

2 Likes