Validating file upload?

I’m using Meredydd’s Validator package to add form validation to the tutorial News Aggregator app as I learn along with that tutorial. Validating the various text boxes was simple, but it was tricky to validate that a file was uploaded. This was due to the fact that self.item within the ArticleEdit component does not seem to have access to the uploaded file from the FileLoader when the file has changed, even though the validator can see it just fine, and all other properties are easily accessed. It feels like I’m missing something, and there should be an easier way to do this. I would have liked to have done this kind of validation using the alert’s buttons from within the Homepage component, but I wasn’t sure how to trigger the validation function on the called component’s data. Is there something I’m misunderstanding about how all this works? (Note that this is distinctly possible as I’m new to the platform.)

https://anvil.works/build#clone:XGYSMXOID4VRXKJP=QDG2G7L636GV7G7EBQREXTJE

Mostly got things working as I’d like below. Not sure why I need to manually set the image onto the item after the form has been validated, since that is supposed to be handled by the image_uploader_change function, but otherwise it looks good!

https://anvil.works/build#clone:XGYSMXOID4VRXKJP=QDG2G7L636GV7G7EBQREXTJE

I think it may be because the current implementation of the Validator class overwrites the image_uploader_change event handler, rather than simply performing its function in addition to it.

1 Like

My pull request to the validation library adds a ‘validate’ method which can be called from within an event handler:

(my editor auto formatted the code, so the diff is tricky to read. Sorry)!

2 Likes