Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

Create new folder when submitting form

Started by DouxDoux 3 years ago · 3 replies · 322 views
3 years ago

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 :

TWIG
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

last edited 11/29/23 by pamtbaau
3 years ago

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):

YAML
process:
  twig: true
cache_enable: false

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

👍 1
3 years ago

@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
last edited 12/05/23 by pamtbaau
3 years ago

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

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.

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1135 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 60 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 132 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 108 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 127 7 months ago