Stop callback if fields in form are empty


I created a form with the html.Form function and a callback to catch the click on the submit button, however I need to validate that the fields have information because they will be stored in fields marked as “not null” in the database. I used the “required” property in the inputs and, as expected, it shows a message asking to fill the fields. However, that doesn’t stop the callback from running and I got an error from the insert to the database.

How can I force the callback to stop when the fields are empty? I suspect that I can check for the values of the fields and if they are empty raise a PreventUpdate exception but I’m not sure this is the best approach.

Has someone made something similar? Could you suggest or share what you did?

Thank you!

PreventUpdate is exactly what I’d use here, is there some issue with it? It initially seems weird to be raising an error for something that isn’t fatal, but it just silently stops the callback from trigering, which is what I generally want.

1 Like

No issue. As you said, it seemed weird to raise an exception and I wanted to know if there was another way. But in the end, I wrote the raise exception and is working fine.

Thank you!

Preaching to the choir, I just want to agree that this is indeed the recommended approach :slight_smile: