How do I trap a change to file upload field

In a form I have a “Document (File Name)” upload field.

I am aware that if I have a record that has an uploaded file associated with it, if I tick the “Delete file” check box and save the record, it effectively removes the name of the file from the record (but leaves the actual file on the server). I understand that this is by design, and that if you actually want to delete the file you need to code it yourself (which is what I want to do).

Well, that’s all fine and I am quite happy with how to code the file deletion. However, what is not clear is how I trap that change so I can insert my deletion code? I have tried in “onBeforeUpdate”, and “onAfterUpdate”, and there is no “onRecord” event for Forms at all. None of those events are triggered when you tick the “Delete File” check box and then click Save. It would seem that the “state” of that field is saved independently of the overall record that “houses” it.

So how do I detect that change?

In advance - many thanks!

That’s difficult. Fun part is that the upload first goes into a temp space and then after commit it is moved to the final location. The file is stored in the database by name and then the file is stored. Deleting the file can be done from a simple form. there you have all the events, you know the filename and relative location to your physical file so then it should work?

Thanks Albert.

Indeed - for sure I could set it all to delete from a separate form. However, it’s that pesky “delete” check box on the upload field. If a user uses that I need to know so I can clean up - or even if they upload a new file over the top - I also need to know, so again I can clean up. Essentially, I want to “do something” ONLY if that field changes (i.e. an onChange type of event).

What I was considering was to just monitor the field’s contents - so if a record (which has an upload field) is edited (via the “pencil” icon on a grid) I want to save the upload field’s {value} in a global variable. After the record is “saved” I would then examine if the field’s value has changed - is it now empty (i.e. deleted), or is a different value to the one just saved (i.e uploaded a different file) - or if unchanged then just move on.

The thing is there is not an obvious event available (either grid or form) that traps when an upload field is changed (either new file uploaded or “delete” check box used to clear the field)

The easy thing would be to use the “afterUpdate” event in the form - but changing the upload field and “saving” does not seem to trigger ANY event that I can see. That’s what I need help with - detecting if the field has changed?

Ok - kind of answered my own question…

Managed to get this working - i.e. effectively created my own onChange behaviour for the upload field.

In the form (with the upload field) I save the current value of that field in a global variable to reference later. I do this by adding the following to the form’s “onLoad” event:

[signedTS] = {SignedTimesheet};

I then added the following code to the forms “onAfterUpdate” event:

if ([signedTS] != {SignedTimesheet}) {				// i.e. filename changed or "blanked out" (i.e. deleted)
		
	if (!unlink(realpath([fp] . [signedTS]))) {		// so delete "old" named file		
			
		// if fails may be a perimissions issue on folder
		echo "<script type='text/javascript'>alert('File delete failed: " . [fp] . [signedTS] . ". " .
			"User delete cancelled.');</script>";	
	}		
}

Note: [fp] is a global var I set elsewhere that has the filepath to the uploaded documents folder