Is it possible to display form results in admin or a frontend page?

@eskah, The page’s frontmatter contains quite a bit of sloppy copy/paste…

Apart from that, you are resetting the form, which empties the fields, before you save it…

Yes i need to clean up a little bit this md.

About the reset, with or without, before or after the save action : no changes, always “null” in file saved … :pensive:

In .txt format the data are correct, but json still null

When I place dump($form, $action, $params);die(); in onFormProcessed, strip your page down to:

---
title: Test
form:
  name: flex-objects
  fields:
    columns:
      type: columns
      fields:
        column1:
          type: column
          classes: "flex flex-auto justify-center gap-10"
          fields:
            - name: presence
              default: "yes"
              label: Presence
              display_label: false
              type: radio
              options:
                "yes": "Je serai présent.e"
                "no": "Je ne serai malheureusement pas présent.e"
        column2:
          type: column
          classes: "flex flex-auto justify-center gap-10"
          fields:
            - name: last_name
              label: Nom
              type: text
              placeholder: " "
              validate:
                required: true
            - name: first_name
              label: Prénom
              type: text
              placeholder: " "
              validate:
                required: true
            - name: email
              label: "Adresse email"
              placeholder: " "
              type: text
              validate:
                required: true
        column3:
          type: column
          classes: "flex field-yes flex-auto justify-center gap-10"
          fields:
            - name: more
              label: "Invités supplémentaires"
              type: select
              options:
                - 0
                - 1
                - 2
                - 3
        column4:
          type: column
          classes: "flex field-yes hidden more-fields more-fields-1 more-fields-2 more-fields-3 flex-auto justify-center gap-10"
          fields:
            - name: last_name_1
              label: Nom
              type: text
              placeholder: " "
            - name: first_name_1
              label: Prénom
              type: text
              placeholder: " "
        column5:
          type: column
          classes: "flex field-yes hidden more-fields more-fields-2 more-fields-3 flex-auto justify-center gap-10"
          fields:
            - name: last_name_2
              label: Nom
              type: text
              placeholder: " "
            - name: first_name_2
              label: Prénom
              type: text
              placeholder: " "
        column6:
          type: column
          classes: "flex field-yes hidden more-fields more-fields-3 flex-auto justify-center gap-10"
          fields:
            - name: last_name_3
              label: Nom
              type: text
              placeholder: " "
            - name: first_name_3
              label: Prénom
              type: text
              placeholder: " "
        column7:
          type: column
          classes: "flex field-yes flex-auto justify-center gap-10"
          fields:
            - name: alergie
              label: "Préférences alimentaires"
              type: select
              validate:
                required: true
              options:
                aucune: Aucune
                vegetarien: Végétarien
                vegan: Vegan
                hallal: Hallal
  buttons:
    - type: submit
      value: Envoyer
  process:
    - save:
        filename: contacts.json
        dateformat: Ymd-His-u
        extension: json
        body: "{% include 'forms/data.json.twig' %}"
        operation: add
    - message: "Merci pour votre réponse !"
---

enter some nonsense content and submit the form, I get the following output dump:

^ Grav\Plugin\Form\Form {#1114 ▼
  +message: null
  +response_code: null
  +status: "success"
  #header_data: []
  #rules: []
  #items: array:9 [▶]
  #values: Grav\Common\Data\Data {#1124 ▶}
  #page: "/test"
  #nestedSeparator: "/"
  +messages: []
  -name: "flex-objects"
  -id: "flex-objects"
  -enabled: true
  -uniqueid: "hgte2jf5kld9u8wbcwea"
  -sessionid: "pfud6t7ln1a09m5c50366dip4u"
  -submitted: false
  -data: Grav\Common\Data\Data {#1127 ▼
    #gettersVariable: "items"
    #items: array:12 [▼
      "presence" => "yes"
      "last_name" => "abc"
      "first_name" => "safwa"
      "email" => "abc@d.de"
      "more" => "1"
      "last_name_1" => "eafwae"
      "first_name_1" => "afWA"
      "last_name_2" => "AEF"
      "first_name_2" => "AFE"
      "last_name_3" => "AF"
      "first_name_3" => "ADF"
      "alergie" => "aucune"
    ]
    #blueprints: Grav\Common\Data\Blueprint {#1119 ▶}
    #storage: null
    -missingValuesAsNull: false
    -keepEmptyValues: true
    #nestedSeparator: "."
  }
  -files: []
  -flash: Grav\Common\Form\FormFlash {#1120 ▶}
  -flashFolder: "tmp://forms/[SESSIONID]"
  -blueprint: Grav\Common\Data\Blueprint {#1119 ▶}
}

^ "save"

^ array:5 [▼
  "filename" => "contacts.json"
  "dateformat" => "Ymd-His-u"
  "extension" => "json"
  "body" => "{% include 'forms/data.json.twig' %}"
  "operation" => "add"
]

So the form itself is ok and php receives the entered data (see -data: Grav\Common\Data\Data {#1127 ▼). How does your forms/data.json.twig look like?

{{ fields|json_encode|raw }} will give you null values. And if you use the default save action to add data to a json file, the json file will contain the data, but it will be invalid/malformed.

Here is my data.json.twig :

{% macro render_field(form, fields) %}
{% set array = [ ] %}
{% for index, field in fields %}
{% set key = field.name %}
{% set value = form.value(field.name) %}
{% set array = array|merge({ (key): value}) %}
{% endfor %}
{% set array = array|merge({ "moderated": 0}) %}
{% set code = random_string(15)|md5 %}
{{ { (code): array }|json_encode|raw }}
{% endmacro %}
{{ _self.render_field(form, form.fields) }}

@eskah

As your form has a very different structure compared to mine, the twig doesn’t work for you.

But, as I said multiple times: you won’t be able to generate a valid json file with the default save/add action unless someone changes the code of the form plugin. When I started, I also thought it would be possible, but it isn’t.

So, you’ll have to write your own handler in onFormProcessed and you can combine this with twig, then.

No one has done this before?
I don’t know php very well, I don’t think I can do that…

I did :wink: Just some minutes ago :rofl:
See Add custom action to store data, use 'jsonAdd' · pikim/grav-plugin-guestbook@19a022e · GitHub, lines 81-111

and use

    process:
      jsonAdd:
        operation: add

in your form. I’m not finished yet and can’t continue work before tomorrow evening.

Thanks for your help man :pray:
i’ll tried this tomorrow too

@pikim, I’ve updated my onFormProcessed function in reply #9. You might want to update your copy/pasted code too.

Replace:

$object->create();
$dir->clearCache();

with:

$object->save();

@pamtbaau thanks again and again… It finally works!

I’m very grateful thanks for your work and your time :pray:

@pamtbaau

Also works, thank you!

Is it possible to assign a name to the object? At the moment a random number is assigned:

"5ad2e7c178091a7d81d46a5b26de403b":{"author":"test name 123","text":"\u00f6fja\u00f6orf\u00e4mv\u00f6aeoij","email":"none@none.de","date":"Wed, 11 Jan 2023 20:32:42","uuid":"b4fcf835-b46b-4a98-9d88-0a9d1ab4ff4a","moderated":0}

Perhaps I’d like to customize "5ad2e7c178091a7d81d46a5b26de403b".

@eskah

In the meantime I learned that flex-objects can also work with yaml-files. At the moment I’m trying to find out how I can store the form data in a yaml file. If that is possible with a standard save/add action, one would not have to customize onFormProcessed.

Edit:
the latest version now supports json files via custom action and yaml files via default action and an according twig. See: GitHub - pikim/grav-plugin-guestbook: Grav Guestbook Plugin for details.

      save:
        operation: add
        filename: ../flex-objects/guestbook.yaml
        body: "{% include 'forms/guestbook.yaml.twig' %}"

@pikim,

Is it possible to assign a name to the object? At the moment a random number is assigned

In data-models:

  • keys must be unique
  • keys should be meaningless
    • which makes it easier to create a unique key
    • if not meaningless it is probably based on the content of the tuple,
      • which makes it more difficult to create a unique key, because the data in the tuple might not be unique
      • the key must be updated when the content of the tuple changes, which can turn into a headache when the key is also used elsewhere.

But if the gains outweigh the extra hassle and possible pitfalls, it can be done using $object->create(key) instead of $object->save().

@pamtbaau

I asked, because the guestbook plugin (more or less) used uuids up to know. But I’ll throw out the uuid when I continue development. So, it was just out of curiosity.

Thank you very much for your participation and patience! It was very interesting and helpful!