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?
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | webform.webform.wizard_and_computed_twig.yml | 6.51 KB | jrockowitz |
| #8 | webform.webform.wizard_and_computed_twig.yml | 6.29 KB | Waldi |
| Screenshot_1.png | 95.37 KB | Waldi |
Comments
Comment #2
jrockowitz commentedUsing 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.
Comment #4
Waldi commentedThanks 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:

Comment #5
jrockowitz commentedPlease isolate the problem to an English example that is not specific to your project.
Comment #6
Waldi commentedNo 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?
Comment #7
jrockowitz commentedI 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
Comment #8
Waldi commentedComment #9
Waldi commentedI 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?
Comment #10
jrockowitz commentedThe YAML in #8 should be enough to replicate and debug this issue.
Comment #11
jrockowitz commentedI 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.
Does this solve your problem?
Comment #12
jrockowitz commentedPlease consider joining the Webform module's Open Collective to help ensure that the Webform module is maintained.
Donating to the Webform module's Open Collective is the best way to pay it (and me) forward.
Comment #13
jrockowitz commentedComment #14
Waldi commentedGreat, it works, even if i feel a bit stupid now :p
Thx for your help. The donation to opencollective is done!
Comment #15
jrockowitz commentedThx for your contribution to the Webform module's Open Collective.