Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array support for storing values in form data #137

Closed
Rednas83 opened this issue Jun 26, 2020 · 13 comments
Closed

Add array support for storing values in form data #137

Rednas83 opened this issue Jun 26, 2020 · 13 comments
Labels
feature request New feature or request suggestions welcome Looking for suggestions on a proposal
Projects

Comments

@Rednas83
Copy link

Add array support for storing lists/arrays

Something like this whereby the index is used for storing in the correct index

<div class="border border-black p-6">
      <FormulateInput
        v-for="(item, index) in items"
        :key="index + 1"
        type="text"
        :name="item"
        :index="index+1"
        :value="item"
        placeholder="Add item"
        class="mb-1"
        element-class="w-full"
      />
</div>

This wil now store data in the form like

item[1]='input1'
item[2]='input2'
item[3]='input3'
@Rednas83 Rednas83 added the feature request New feature or request label Jun 26, 2020
@justin-schroeder
Copy link
Member

@Rednas83
Copy link
Author

Rednas83 commented Jun 26, 2020 via email

@justin-schroeder
Copy link
Member

Ok gotchya.

If you're truly looking for simple array lists, it is supported: https://codepen.io/justin-schroeder/pen/abdyZzz

However some words of caution here, simple array lists have some faults in Vue. For one using an array index as a key on a list is not recommended. Reactivity caveats in Vue 2 also prevent easy array manipulation, so generally you have to replace the array which is a bit less performant, but it works. That's why the grouping mechanism in Vue Formulate uses objects that can have their own sub-attributes and Symbol keys.

Still, hopefully this helps!

@Rednas83
Copy link
Author

...But this wil not work together with FormulateForm because that expects a name instead of the v-model.
My database expects this:
image
I can simply bind this to a FormulateForm like below:
image

Please consider official support for adding simple arrays. Array of objects are not always practical.

@justin-schroeder
Copy link
Member

Can you propose an API? How would you like to write an array input?

@Rednas83
Copy link
Author

Rednas83 commented Jun 27, 2020 via email

@justin-schroeder
Copy link
Member

@Rednas83 thanks for following up. What I'm asking is can you write some sample code of what you want the API to look like. For example:

<FormulateInput
  v-for="(item, index) in items"
  :index="index"
  type="text"
  name="group"
/>

Is that what you want it to look like? If so there are several questions – how do we get a stable key? Using the index as a key will lead to reactivity issues, so we need each node to have its own key. How do users add new inputs or remove inputs. How does Vue Formulate know to store values as an array (maybe if there is an index prop it just does that somehow?). Would the requirement be that all inputs have to have the same input name?

What are some real-world use cases where this is a valuable tool and groups doesn't work well? My gut reaction is to say this problem is solved with a group and a simple map on your submit handler, so Im trying to figure out if its worth implementing or not and asking your help to arrive at either conclusion.

@Rednas83
Copy link
Author

Rednas83 commented Jun 27, 2020

Besides the missing key this looks fine to me. I would make the key like "item[index]" to make it perfectly clear where it comes from.

<FormulateInput
  v-for="(item, index) in items"
  :key="`items[${index}]`"
  :index="index"
  type="text"
  name="group"
/>

To simplify it even more perhaps like this, whereby names is the array of strings
<FormulateInput type="text" names="items"/>
or <FormulateInput type="text" array="items"/>
or <FormulateInput type="text" name="items"/> <!-- Normal operation on string and Loop on array -->

Or place inside a group to make it a bit more readable and more flexible

<FormulateInput type="array" name="items"/>
  <FormulateInput type="text" index="1"/>
<FormulateInput />

My preferred option would be <FormulateInput type="text" names="items"/> because it's the shortest and would be most fitting I think for just a simple array.

User case for example is a simple todo list

@justin-schroeder
Copy link
Member

Ok, I hear what you're saying. There are some really challenging implementation details that would need to be ironed out, but I can see how this could be a compelling feature. I'll mark it as open to suggestions, and maybe we'll target implementing something like this roughly the 2.6 release cycle

@justin-schroeder justin-schroeder added the suggestions welcome Looking for suggestions on a proposal label Jun 29, 2020
@justin-schroeder justin-schroeder added this to 2.x.x in Roadmap Jun 29, 2020
@chadwtaylor
Copy link

+1 Have a need for this. :)

@justin-schroeder
Copy link
Member

@chadwtaylor can you give your use case? Im still not convinced this is a feature that should be in core when we already have repeatable fields so im trying to feel it out a bit more and see what the real world applications are.

@justin-schroeder
Copy link
Member

There doesnt seem to be sufficient demand for this feature and frankly I find it a bit confusing. I'm going to close it for now, but if it becomes pressing in the future, let's feel free to resurrect it.

@itsgifnotjiff
Copy link

Would love to have it as I wish to download the data as JSON and instead of converting it o submit I would love to have a group that models to an array of strings instead of an array of objects containing random key and string value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request suggestions welcome Looking for suggestions on a proposal
Projects
Roadmap
2.x.x
Development

No branches or pull requests

4 participants