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

Form field label based on page title

Solved by Seb View solution

Started by Seb 8 years ago · 1 replies · 652 views
8 years ago

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.

TWIG
{% 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!

8 years ago Solution

Done!

I must set the "name" for each itemfield_value:

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

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1140 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 63 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 138 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 114 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 132 7 months ago