Laravel-Backpack / addons

A place for the Backpack community to talk about possible Backpack add-ons.
5 stars 2 forks source link

[PRO] Repeatable inside Relationship is not working (js error) #36

Closed skpr-repo closed 1 year ago

skpr-repo commented 2 years ago

Bug report

What I did

I created:

The products_variants has an "attributes" json field that I want to populate with many attributes (name and value).

Inside the ProductCrudController -> setupCreateOperation I created the relationship field that shows product_variants, with inline creation and subfields.

One of the subfields is a "repeatable" field with "attributes" and subfields.

This is the code:

        // Variants
        CRUD::field('variants')->type('relationship')->inline_create([
            'entity' => 'variant',
            'force_select' => true,
        ])->force_delete(true)->subfields([
            [
                'name'    => 'description',
                'type'    => 'text',
                'label'   => 'Variante',
                'wrapper' => ['class' => 'form-group col-md-4'],
                'tab'     => 'Base',
            ],
            [
                'name'    => 'selling_price',
                'type'    => 'text',
                'label'   => 'Prezzo Vendita',
                'suffix'  => '€',
                'wrapper' => ['class' => 'form-group col-md-2'],
                'tab'     => 'Prezzo',
            ],
            [
                'name'    => 'attributes',
                'type'    => 'repeatable',
                'label'   => 'Attributes',
                'subfields' => [
                    [
                        'name'    => 'name',
                        'type'    => 'text',
                        'label'   => 'Name',
                        'wrapper' => ['class' => 'form-group col-md-6'],
                    ],
                    [
                        'name'    => 'values',
                        'type'    => 'text',
                        'label'   => 'Values',
                        'wrapper' => ['class' => 'form-group col-md-6'],
                    ],
                ]
            ]
        ])->label('Variants')->wrapper(['class' => 'form-group col-md-12'])->tab('Variants');

What I expected to happen

Being able to add a new product variant and new attributes for the variant. The docs say we should not add a relationship inside a repeatable, but the opposite is not mentioned.

What happened

When I click on "add new attribute" a new variant is added, instead of the attribute. The javascript console shows this error:

Syntax error, unrecognized expression: input[type=hidden][name=variants[0][attributes]]

What I've already tried to fix it

if I use a table instead of repeatable, it works as expected (but I would like to use a repeatable).

               [
                'name'    => 'attributes',
                'type'    => 'table',
                'label'   => 'Attributes',
                'columns' => [
                    'name'   => 'Nome',
                    'values' => 'Valori',
               ]

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP 8.1.7 (cli) (built: Jun 10 2022 12:22:48) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.7, Copyright (c) Zend Technologies with Zend OPcache v8.1.7, Copyright (c), by Zend Technologies

LARAVEL VERSION:

v9.20.0@c99868f1c9bf2f5d250993121838db905591864f

BACKPACK VERSION:

5.1.4@4539c787a2e052d2e2e2c00188c38432dc2a7ea9

Operating System and Server Setup

I encountered the problem above on my Windows 10 machine, Google Chrome latest and my web server is set up using Homestead. MySql 8. composer update to the latest versions. I have a license for backpack pro and devtools.

Anyway, great product. I'm very happy to work with it.

Thanks

pxpm commented 2 years ago

@skpr-repo hello there.

I didn't fully understood your use case, but it's not possible to use repeatable over repeatable at the moment.

But it also does not make sense to use inline_create in a repeatable container, unless it is "inside of the repeatable", in this case it seems there is a belongstoMany relation ? If that is the case you should add the inline_create when configuring your pivotSelect field.

The confusion here is that technically a relationship with subfields IS a repeatable field .

Let me know how it goes.

skpr-repo commented 2 years ago

Hi. Product has_many Product_Variants. Product_Variant belongs_to a single Product. Product_Variant has a json field with one or more "name:value" entries. Indeed it does not work :-)

pxpm commented 1 year ago

Like I replied in other thread, I want to work on this feature (repeatable over repeatable). I am sure it's a feature that I needed (worked around using a table or a custom field), and ALOT of other people like you probably need too. I know it will take some time to do, so I need to prepare a clean schedule to do it, as the repeatable have grown into a monster field with lot's of use-cases, implementation details.

I will be closing this for now,

thanks @skpr-repo for the report, and sorry we don't support it yet.

tabacitu commented 1 year ago

Heads-up: we've decided not to work on this in the very near future.

repeatable inside repeatable will SEEM easy to do, but it won't be, it'll require multiple months of work, for this one thing, and right now we have other priorities. We might do it a bit later, but again - it's not a priority. The reason for that is that... the deeper you go with repeatable inside repeatable, the more the interface breaks down, and start to become an unusable mess... So in order to achieve the same thing properly, we'd better think of a better solution that would solve that interface problem too.

Sorry to be the bearer of bad news. If more people gang up on me 😅 of course I'll reconsider. Cheers!