Problem/Motivation

I've created a webform to calculate the personal footprint with 18 questions in 4 categories. I want to use 4 wizard pages for the catgories to make the calulator more clear. Every wizard page is equivalent to one of the catgorie. In every category I need a subtotal, and at the and I need a total, which I want to calculate with computed twig.

Here the bug/problem starts:

- when I use other page elements like group, details or container everything is perfect.
- when I use page wizard elements, I get an error in this moment I want to page two.

Steps to reproduce

The structure I want to use:

wizar page 1
_question_1
_question_2
_question_3
_subtotal_1
wizar page 2
_question_4
_question_5
_question_6
_subtotal_2
wizar page 3
_question_7
_question_8
_question_9
_subtotal_3
wizar page 4
_question_10
_question_11
_question_12
_subtotal_4
total

My computed value markup:
{{ (data.fahrraeder + data.autos + data.motorraeder) }}

I also tryed this structure:

wizar page 1
_question_1
_question_2
_question_3
wizar page 2
_question_4
_question_5
_question_6
wizar page 3
_question_7
_question_8
_question_9
wizar page 4
_question_10
_question_11
_question_12
subtotal_1
subtotal_2
subtotal_3
subtotal_4
total

What I also tryed:
- change the page element type (with other element types it worked)
- change AJAX settings (for the whole form and for the single computed fields)
- change settings of stored values in database
- change display mode of the field
- change modus of the field
- change whitespace

Is this a bug or am I a fool?

Comments

Waldi created an issue. See original summary.

jrockowitz’s picture

Status: Active » Postponed (maintainer needs more info)

Using cascading computed twig elements with Ajax can cause significant performance problems and racing conditions.

A good solution is to use a custom Twig function to centralize all the business logic.

You could also use JavaScript to visually calculate the sub-totals but still use Twig to calculate the final total securely.

If you can post an example webform, I can probably help you track down the issues but the above recommendations will probably still apply.

Waldi’s picture

Thanks a lot for your fast response! Of course I can send you the example:

Not working calculator with wizard elements:
https://konsum-kollektiv.de/voting/oekologischer-fussabdruck

Working calculator with section elements:
https://konsum-kollektiv.de/voting/oekologischer-fussabdruck-test

Settings:
- form: use AJAX: off
- field: use AJAX: off
- field: store value in database: on

subtotal1 {{ (data.bus_und_bahn + data.auto + data.flugzeug) }}
subtotal2 {{ (data.fahrraeder + data.autos + data.motorraeder + data.einrichtung + data.konsumgueter) }}
total {{ (data.fahrraeder + data.autos + data.motorraeder + data.einrichtung + data.konsumgueter + data.bus_und_bahn + data.auto + data.flugzeug) }}

Many thanks in advance for your help!

structure of fields:
structure of fields

jrockowitz’s picture

Please isolate the problem to an English example that is not specific to your project.

Waldi’s picture

No problem!

not working:
https://konsum-kollektiv.de/voting/wizard-and-computed-twig

working:
https://konsum-kollektiv.de/voting/section-and-computed-twig

additional info for the select fields:
0.5 = please select (empty value)
0 = nothing
1 = 1
2 = 2

Many thanks again for your help! Is there a way to spend you some coffee?

jrockowitz’s picture

I need to be able to replicate this issue locally.

Can you please export the Webform as YAML and attach it here?

@see https://www.youtube.com/watch?v=4xI-T1OuHn4

Waldi’s picture

Waldi’s picture

I have one more idea:

I use materialize theming and some forms need AJAX for perfect function. So I load the AJAX library in the .theme file every time when materialize forms appears.

May this a reason that webforms with wizard pages dont work for me? If yes: is there an other solution for me?

function materialize_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    $form['#attached']['library'][] = 'materialize/ajax';
}
jrockowitz’s picture

Status: Postponed (maintainer needs more info) » Active

The YAML in #8 should be enough to replicate and debug this issue.

jrockowitz’s picture

I see the problem now, you are running into an error where twig is trying to do a calculation on an empty string. A single page form would not have as many empty values as a multistep form.

You need to make sure the expected data exists before doing any twig calculations. I usually wrap my computation in an if statement. (i.e. {% if data.select_a|length and data.select_b|length %})

Here is an example from the attached webform.

   subtotal_one:
      '#type': webform_computed_twig
      '#title': 'Subtotal One'
      '#display_on': view
      '#template': '{% if data.select_a|length and data.select_b|length %}{{ (data.select_a + data.select_b) }}{% endif %}'
      '#store': true

Does this solve your problem?

jrockowitz’s picture

Webform logo This ticket was completed using Open Collective funds.
Please consider joining the Webform module's Open Collective to help ensure that the Webform module is maintained.

Many thanks again for your help! Is there a way to send you some coffee?

Donating to the Webform module's Open Collective is the best way to pay it (and me) forward.

jrockowitz’s picture

Status: Active » Needs review
Waldi’s picture

Great, it works, even if i feel a bit stupid now :p

Thx for your help. The donation to opencollective is done!

jrockowitz’s picture

Status: Needs review » Closed (works as designed)

Thx for your contribution to the Webform module's Open Collective.