<p>I'm building a custom plugin for Craft CMS 4 that has a form where users can add and remove a list of products.
Every product has a title and a price field.</p>
<p>Other fields have been created like this:</p>
<pre><code>{{ forms.textField({
        label: &quot;Note&quot;|t('_receipts'),
        id: 'note',
        name: 'note',
        value: receipt.note,
        required: true,
        errors: receipt.getErrors('note'),
    }) }}
</code></pre>
<p>I can write some JS to add and remove fields.
That way a user can add more products.
But what should the names of the fields be?
Every field needs to be validated on save.</p>
<p>I will probably also need to create another DB table which will store all products and I guess I will need to store the references to those products in the main table.</p>
<p>Does anyone have any pointers on how to tackle this in the best way possible?</p>

CC BY-SA 4.0
2024-03-19T07:32:17.847Z
Welcome! Can you elaborate a little bit - you mention products but is this a craft commerce product or a data object you made that you're calling a product? What is the product in your setup (an entry? custom database row? Is this part of what you're asking?) Thanks! - Your Friendly Neighborhood Mod
cherrykoda
CC BY-SA 4.0
2024-03-20T09:06:40.697Z
@cherrykoda This is something unrelated to Commerce products. It could just as well be notes or addresses. I went the easy route by rendering a new line of form elements for each product with JS and I store all data in a single hidden field as JSON. I should probably have created another DB table and used references but that would have made the whole thing more complex without the need for it.
Matthias
<plugin-development><craft4><custom>
How to create a matrix style field in a custom plugin
# How to create a matrix style field in a custom plugin

(Asked by: username_0 on 2024-03-05)

I'm building a custom plugin for Craft CMS 4 that has a form where users can add and remove a list of products.
Every product has a title and a price field.
Other fields have been created like this:
{{ forms.textField({
        label: "Note"|t('_receipts'),
        id: 'note',
        name: 'note',
        value: receipt.note,
        required: true,
        errors: receipt.getErrors('note'),
    }) }}

I can write some JS to add and remove fields.
That way a user can add more products.
But what should the names of the fields be?
Every field needs to be validated on save.
I will probably also need to create another DB table which will store all products and I guess I will need to store the references to those products in the main table.
Does anyone have any pointers on how to tackle this in the best way possible?

**Tags:** plugin-development, craft4, custom

> **Comments:**
> **username_1**: Welcome! Can you elaborate a little bit - you mention products but is this a craft commerce product or a data object you made that you're calling a product? What is the product in your setup (an entry? custom database row? Is this part of what you're asking?) Thanks! - Your Friendly Neighborhood Mod  
> **username_0**: @username_1 This is something unrelated to Commerce products. It could just as well be notes or addresses. I went the easy route by rendering a new line of form elements for each product with JS and I store all data in a single hidden field as JSON. I should probably have created another DB table and used references but that would have made the whole thing more complex without the need for it.  

---

## 0 answers

