What I’m trying to do:
So basically I have an alert that opens form full of places the user can input data into. I want to validate against these input BUT I do not want to exit out of the alert or allow the user to submit the form with bad inputs.
What I’ve tried and what’s not working:
I have tried putting the whole thing in a while loop but as soon as the save button is pressed it exits out of the alert.
I have tried calling a function when the save button is pressed but it does not call the function at all when the button is click. But instead if I put the function like this | buttons=[("Save",self.test(),("Cancel",False)] | all it does is call the function as soon as the alert is opened.
Code Sample:
def btnAdd_click(self, **event_args):
# Initialise an empty dictionary to store the user inputs
self.newDamInputs = {}
validationFailed = True
while validationFailed:
# Open an alert displying the 'NewDam' Form
self.saveClicked = alert(content=NEDam(item=self.newDamInputs),
title="Add Dam",
large=True,
buttons=[("Save",True),("Cancel",False)],
)
if self.saveClicked:
if self.newDamInputs == {}:
alert("Quack")
elif self.newDamInputs["Name"] == "":
alert("Name cannot be empty. Please try again.")
else:
validationFailed = False
NEDam Form:
What I want:
If anyone has any alternatives for doing this other than a bunch of if / elif statements for each case, I would love to know. Especially if you know a way to validate within the form the alert uses!
Clone link: Click here for the clone link. The code is line 124 in the Dams form.
If you need any additional information please ask.
Zod sounds interesting, but I am still a little bit confused of how I am to use it as I have never used it before. Perhaps you could give me an example?
You probably don’t want to use the built in alert buttons here. Instead you will likely want to add buttons on the form you’re using as an alert. These buttons can then have custom event handlers.
If you search the forum for x-close-alert you’ll get some relevant hits and examples.
An alternative that works for simple cases (and covers 95% of my alerts) is using alert2.
This uses Validator for the validation, which uses Popovers from Anvil Extras.
You can clone both Validator and InputBox, make sure Validator depends from the third party Anvil Extras, make sure that InputBox depends from Validator and add InputBox as a dependency of your app. You don’t even need to add Validator or Anvil Extras.
This will not cover complex forms, but will allow you to use fairly complex input forms with validation without even creating the forms! This is how I manage all the input alerts in all my production apps.