Hi,
I’m using an alert with a custom form to provide an edit view as per https://anvil.works/learn/tutorials/database-backed-apps/chapter-4
I’m trying to work out how I can do validation on the data entry so that it’s not possible to ‘save’ a record unless all validation passes. I know I can do validation in line using events on the text boxes, but what I’m really after is a way to do blocking validation when they click ‘Save’.
I can get it validating on Save, and popping up an alert to say it doesn’t pass etc. but it still ends up closing the alert, so they just lose the data they’ve already entered.
Is there a way to do data validation when someone clicks the Save button, either before the form is dismissed and the Save is processed OR to somehow do the validation and then re-display the form with the entered data if it doesn’t pass?
Here’s the code launching the alert, and processing the Save click. I’m launching this from a button click in a DataRowPanel within a DataGrid.
def btn_edit_click(self, **event_args):
# Get the index of the data item in the list, so that we can easily replace it by index on save
item_index = next((index for (index, d) in enumerate(Globals.data_list) if d["id"] == self.item['id']), None)
self.current_index = item_index
data_item = self.item
save_clicked = alert(
content=SiteEdit(item= data_item),
title="Edit Site",
large=True,
buttons=[("Save", True), ("Cancel", False)],
)
if save_clicked:
# I'd like to do data validation here, and only save the item if it passes.
# If it fails, I'd like to re-display the custom form with the data they've already entered so they can correct it
if data_valid == True:
self.item = data_item
else:
redisplay_form()
I did wonder if I could use https://anvil.works/library/form-validation however I can’t work out how to disable the “Save” button on the alert form as they are built in to the alert?