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

BelongsToManyMultiSelect not updating attributes based on reactive field #2442

Closed
mohamedmoussasoli opened this issue May 16, 2022 · 3 comments
Labels
bug Something isn't working unconfirmed

Comments

@mohamedmoussasoli
Copy link

mohamedmoussasoli commented May 16, 2022

Package

filament/filament

Package Version

v2.12.10

Laravel Version

v9.12.2

Livewire Version

No response

PHP Version

PHP 8.1

Bug description

I have multiple (BelongsToManyMultiSelect) fields and each field is visible based on the state of another reactive radio field.

When changing the state of radio field to switch between BelongsToManyMultiSelect fields, attributes of BelongsToManyMultiSelect not updated. only label attribute is updated.

Steps to reproduce

Here are my form fields.

                Radio::make('applies_to')
                    ->label(__('admin.Applies To'))
                    ->reactive()
                    ->afterStateUpdated(function ($set) {
                        foreach (Coupon::APPLIES_TO as $appliesTo) {
                            $set(str($appliesTo)->snake(), null);
                        }
                    })
                    ->options(
                        collect(Coupon::APPLIES_TO)->mapWithKeys(function ($appliesTo) {
                            return [ $appliesTo => __('admin.' . $appliesTo) ];
                        })->toArray()
                    )
                    ->dehydrated(false)
                    ->default(Coupon::APPLIES_TO_ALL)
                    ->saveRelationshipsUsing(function ($state, Model|Coupon $record) {
                        if ($state && $state == Coupon::APPLIES_TO_ALL) {
                            $record->clearOtherRelations();
                        }
                        $record->applies_to = $state;
                        $record->save();
                    }),

                BelongsToManyMultiSelect::make('products')
                    ->label(__('admin.Products'))
                    ->required()
                    ->relationship('products', 'name')
                    ->visible(fn($get) => $get('applies_to') == Coupon::APPLIES_TO_PRODUCTS),

                BelongsToManyMultiSelect::make('categories')
                    ->label(__('admin.Categories'))
                    ->required()
                    ->relationship('categories', 'name')
                    ->visible(fn($get) => $get('applies_to') == Coupon::APPLIES_TO_CATEGORIES),

Relevant log output

No response

@mohamedmoussasoli mohamedmoussasoli added bug Something isn't working unconfirmed labels May 16, 2022
@danielebarbaro
Copy link

danielebarbaro commented May 18, 2022

same situation here.

for the moment I changed the component from

    BelongsToManyMultiSelect::make(__('Establishments'))
                        ->relationship('establishments', 'name')

to

    BelongsToManyCheckboxList::make(__('Establishments'))
                        ->relationship('establishments', 'name')

Screenshot 2022-05-18 at 20 43 27

@danharrin
Copy link
Member

The code example provided is not helpful to me solving this problem. I don't have access to the Coupon class, and I don't have any of your relationships or models.

Please test this again on the latest version of Filament, run filament:upgrade as well. If this is still an issue, please send an example with minimal code that I can reproduce with, preferably use MutliSelect instead of BelongsToManyMultiSelect so that I don't have to create any models or relationships to test with.

@mohamedmoussasoli
Copy link
Author

Thanks @danharrin after updating the issue gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed
Projects
None yet
Development

No branches or pull requests

3 participants