Changing form image/file upload destination dynamically

To allow end-users to upload images (or files) when they create a new page using my Add Page by Form plugin, I’m trying to change the destination of the upload. The challenge is that the destination is unknown until the form is submitted. That’s because the destination is determined by the new page Title user input.

Changing the destination field value in Javascript has crossed my mind but it feels like a hack. I would rather do it ‘the Grav way’, that is within PHP using the events. The onFormProcessed event is too late since the image by then has been placed alongside the page containing the Add Page form by the Form plugin. So I was looking for some kind of onBeforeFormProcessed event. The most promising event which is fired before the image is moved from the temporary upload location to it’s destination folder is the onFormValidationProcessed event.

The approach is to find the destination variable in the $event['form'] array and when it’s value is @self change it to the desired location (being the new page folder). The finding part went okay but I’m stuck on how to change the value of destination.

Any hints are greatly appreciated!

Wonder why you want to change the media location?
Anyways take a look at this documentation: https://learn.getgrav.org/forms/forms/fields-available#the-file-field
You can change the destination to anywhere in the Grav installation and recall it since the unique location will be stored in the post/page submitted using https://learn.getgrav.org/content/media.

Thanks for pointing to the documentation. The normal usage, as described in the documentation, is to set the destination at the time of creating the form. So it is set in the form definition. My use case requires that the destination is set by the value of a Text field.
The end-user enters the Title of the new page in that text field. The destination should then be constructed form the value of the parent frontmatter variable and a slug based upon the entered page title. So the destination is yet unknown when the form is displayed to the end-user.
To better understand this use case please see the docs on my Add Page by Form plugin.

Holy moly! Did not connect you to the author of your plugin untill now… Haha, my apology:)

In my understanding a page has to be saved before any media can be attached to it. If this is the case with your plugin too then the media is first stored in temp then saved at the page location automatically. Then there is no need to specify a location for the media as I assume it is alot more organized to store all media within the page folder.
Does your project contain allow for registration to post? Or anyone?
If members can have access to a limited admin panel, i would guess it would me easier to change it?
Realize that this will most likely not be as useful as you need it to be, but maybe it can give another perspective? Or keep it simpler? :=) good luck! And thank you for your contributions! I will make sure to test out your plugin!