Form field label based on page title

Hi!

I want to create a front-end form with dynamic text fields based on pages titles.

For the moment I merge the form.fields in the twig template with my custom hashes of data.

{% extends "forms/default/form.html.twig" %}

{% set fieldset = {
  type: "fieldset",
  legend: "Sélectionnez vos quantités par article",
  fields: {}
} %}

{% for item in page.find('/home/_catalogue').collection %}
  
  {% set itemfield_name = item.slug %}
  {% set itemfield_options = {} %}  
  {% for i in range(0, item.header.qty) %}
    {% set itemfield_options = itemfield_options|merge([i]) %}
  {% endfor %}
        
  {% set itemfield_value = {
    label: item.header.label ? item.header.label : item.title,
    type: "select",
    options: itemfield_options
  } %}


  {% set fieldset_newfields = array_key_value(
    itemfield_name,
    itemfield_value,
    fieldset.fields
  ) %}  

  {% set fieldset = fieldset | merge({"fields": fieldset_newfields}) %}
  
{% endfor %}

{% set formfields = array_key_value("articles", fieldset, form.fields) %}  
{% set form = form | merge({"fields": formfields}) %}

My visitor see the correct form.

But when I submit I get an error : “Argument 1 passed to Grav\Common\Data\Data::merge() must be of the type array, string given, called in /var/www/grav/development/user/sites/shop.amatiq.fr/plugi…”

How to make it ok?

May I try another method?

Thanks!

Done!

I must set the “name” for each itemfield_value:

{% set itemfield_value = {
    **name: itemfield_name,**
    label: item.header.label ? item.header.label : item.title,
    type: "select",
    options: itemfield_options
  } %}