Converting repeated inputs (i
) and forms (f
) into arrays
#5
Replies: 3 comments 5 replies
-
Couldn't you assign a unique ID/index to the form/form wrapper - and the children's mapping would then be unique due to the uniqueness of their parent elements? Still learning wized, so might not fully grasp the context... :) |
Beta Was this translation helpful? Give feedback.
-
Ok, I see the issue with this... What if we don't allow multiple forms with the same attribute? What would be the downside here? You can still apply the same action to multiple elements. For example: This way, only rendered lists will be an array. You can still display the correct step, by applying this action on the form_wrapper element: In this case, the container that has all of the forms has also a Wized attribute applied. This is how I have it set up currently, and it works really well. If your multistep form is in a rendered list, you can just replace |
Beta Was this translation helpful? Give feedback.
-
What's the state on this @alexiglesias93 |
Beta Was this translation helpful? Give feedback.
-
The Data Store stores inputs data (
i
) and forms data (f
) in multiple ways:Single occurrences
When you add a
wized="element_name"
attribute to an input or a form, it's stored like this:List occurrences
When you render an input or a form inside a list, Wized knows that there are multiple occurrences of that element and converts the field to an array:
Each element's value can be accessed using the index variable that was defined in the
Render list
action.For example:
Multiple (non list) occurrences
This is the case scenario that we need to improve.
Currently, Wized only accepts 1 unique element for each static (non list)
i
orf
. If the user adds the samewized="element_name"
attribute to two or more elements, Wized will only keep the last one.For example, in this scenario:
Wized will store the value for the last instance, overriding any other:
This is probably not the desired result for most people. If they add multiple inputs or forms with a same name, they would expect to be able to access their value.
So I think we need to update the behavior to this:
Now, the problem is that we currently have no way to access each element's value from inside an Action.
Let me put a real live example:
A developer wants to create a multi-step form, breaking down each step into an individual form.
The developer would apply
wized="step_form"
to each<form>
element, creating the following output (I just made up the fields):A total of 3 steps (forms), each one containing the inputted data.
The developer wants to display the next step when the user successfully submits each form, so he adds the following Actions:
Action 1 - displaying the current step form
step_form
Set visibility
return v.step_index === FORM_INDEX
In this Action
v.step_index
is a variable that defines the current step to display, andFORM_INDEX
is what we need to solve, it must define each form's index so each form will only be displayed whenv.step_index
is equal to the form's index in thef
array.Action 2 - incrementing the current step index
step_form
On Event
submit
Set variable
v.step_index
return FORM_INDEX + 1
In this Action
FORM_INDEX
is again what we need to solve, it must define the submitted form's index so we can increment it by 1 after submission to proceed to the next step.The potential solutions
Each form element's index could be exposed via
t.index
.The
t
keyword belongs to the target element of each action, so in this case it belongs to each form element.t.index
would be:t.index = ELEMENT_INDEX
when the element is repeated multiple times (non list) on the page.t.index = undefined
when the element is unique on the page.t.index = ??
when the element is rendered in a list. This scenario is tricky to define as the element could be rendered in a nested list, which means that it could potentially have multiple indexes, not just one. I need to give this scenario some further thoughts, feedback is appreciated.If we update the previous example to use
t.index
, it would look like this:return v.step_index === t.index
return t.index + 1
I think this could work, but I am not 100% convinced because of the uncertainty for
t.index
in rendered lists (3rd bullet in the previous list).Hence this RFC, I would love to read the community's comments and thoughts and see if we can find an ultimate best solution to this problem.
Beta Was this translation helpful? Give feedback.
All reactions