ziffmedia / nova-select-plus

A Laravel Nova Select Field
MIT License
91 stars 26 forks source link

Support dependsOn #49

Closed RomkaLTU closed 1 year ago

RomkaLTU commented 1 year ago

Would be nice to have ->dependsOn() support.

ralphschindler commented 1 year ago

Is it really nothing more than what is in #50 ? can you try that branch out? feature-supports-dependent-fields

edit: tagging @RomkaLTU

Bibesko commented 1 year ago

It doesn't work for me.

When i create field like this:

SelectPlus::make('Checklists', 'checklists', Checklist::class)->dependsOn('tenant_id', function ($field, NovaRequest $request, FormData $formData) { if ($formData['tenant_id']) { dd($formData); } }),

And change tenant_id field, dependsOn doesn't want to be triggered.

If SelectPlus change with simple nova Select field type then it is triggered and works as it should.

Maybe you must replace this in mixin and vue template https://nova.laravel.com/docs/4.0/customization/fields.html#dependent-form-field

miagg commented 1 year ago

Me too. It seems there is no dependsOn support.

ralphschindler commented 1 year ago

Please try v2.0.2 and report back. Thanks!

miagg commented 1 year ago

Please try v2.0.2 and report back. Thanks!

@ralphschindler is it possible to publish it on packagist?

ralphschindler commented 1 year ago

Try now, had to update the webhook settings.

miagg commented 1 year ago

@ralphschindler I just tried v2.0.2.

Sadly it does not work for me 😥

Here is my field for reference

SelectPlus::make('Product Varieties', 'productVarieties', ProductVariety::class)
    ->usingDetailLabel('name')
    ->resolveUsing(fn($models) => $models->sortBy('pivot.order')->values())
    ->reorderable('order')
    ->required()
    ->onlyOnForms()
    ->dependsOn('productType', function (SelectPlus $field, NovaRequest $request, FormData $formData) {
        if (!$formData->productType) {
            return;
        }
        $field->optionsQuery(
            fn(Builder $query) => $query->where('product_type_id', $formData->productType)
        );
    })

The callback is indeed triggered on every update of productType field (tested with ray) but any changes to the field are not reflected.

ralphschindler commented 1 year ago

oh wow. that is much trickier. in that particular case, the controller also need to know about the dependsOn state, and needs to be able to re-setup the initial options values (if it is not autocomplete).

I will take a stab at this.

ralphschindler commented 1 year ago

Please try v2.0.3-beta.1 it is built off the fix-dependson-in-controller branch

miagg commented 1 year ago

Please try v2.0.3-beta.1 it is built off the fix-dependson-in-controller branch

Yes! It works! 👏

ralphschindler commented 1 year ago

It has been released as v2.0.3 and I removed that beta tag/release.

Bibesko commented 1 year ago

But still some native field options don't work, for example:

SelectPlus::make('Product Varieties', 'productVarieties', ProductVariety::class)
    ->usingDetailLabel('name')
    ->resolveUsing(fn($models) => $models->sortBy('pivot.order')->values())
    ->reorderable('order')
    ->required()
    ->onlyOnForms()
    ->dependsOn('productType', function (SelectPlus $field, NovaRequest $request, FormData $formData) {
        if (!$formData->productType) {
           $field->show();
        } else {
          $field->hide();
       }
    })

I would like to be able to add for example: ->required() or disable it depending on the other field. On all others basic Nova Fields all this methods work as they should.