Create new folder when submitting form

Hi,

I’m trying to create a new subfolder for each form submission to help me manage the data.

Currently my form is saving both the text data and the uploaded files in the same general folder.

This is my code :

title: Report problem
form:
    name: report-problem
    fields:
        street-number:
          label: Street number
          type: text
          validate:
            required: true
        
        street-name:
          label: Street name
          type: text
          validate:
            required: true             
            
        custom_file:
          name: myfile
          type: file
          label: A Label
          random_name: true
          destination: 'C:/xampp/htdocs/grav/grav-admin/user/data/report-problem'
          multiple: true
          autofocus: false
          accept:
            - image/*
            
    buttons:
        submit:
          type: submit
          value: Submit
        reset:
          type: reset
          value: Reset
          
    process:
        - message: Thank you for your feedback!
        - save:
              fileprefix: contact-
              dateformat: Ymd-His-u
              extension: txt
              operation: create
              body: "{% include 'forms/data.txt.twig' %}"
        - reset: true

So every submission is saved in the following folder : user/data/report-problem

Is there a way to save each submission in a different subfolder ? For instance, the data for one submission would be saved in the following path : user/data/report-problem/sub1

And the following in : user/data/report-problem/sub2

First, I would use user-data://report-problem/.. as the first part of the destination’s value. It’s more portable that way.

How do you want to derive the name of the subfolder?

Should it be based on another field value? Should it be random? Should it be part of a predefined pattern or sequence?

Where I am going with this is that you can use Twig expressions in forms (and other pages) if you add these header to the page at the top level (i.e. outside the form definition):

process:
  twig: true
cache_enable: false

Then find the right expression and include it like destination: 'user-data://report-problem/{{ twig-expression }}'.

1 Like

@hughbris, As far as I know, Twig is only interpreted in the ‘process’ section of the form and not in the ‘field’ section.

@douxdoux-1312, unfortunately, you haven’t been able yet to respond to @hugbris’ relevant questions. Without relevant information about the use-case I can only roughly suggest to create a custom plugin, catch the onFormUploadSettings event and set the destination to your liking.

1 Like

You’re right. I finally tested it. I got ahead of myself, wrote that, went to test it, which got unexpectedly complicated for a reason I’ve forgotten. I should have removed the comment or added a disclaimer :upside_down_face:

It’s a shame process.twig doesn’t process all frontmatter, as I expected it to.