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

Problem in the current locale switching logic #11825

Closed
mybouhssina opened this issue Mar 12, 2024 · 2 comments
Closed

Problem in the current locale switching logic #11825

mybouhssina opened this issue Mar 12, 2024 · 2 comments
Labels
bug Something isn't working low priority unconfirmed
Milestone

Comments

@mybouhssina
Copy link

mybouhssina commented Mar 12, 2024

Package

filament/spatie-laravel-translatable-plugin

Package Version

3.2.44

Laravel Version

10.47.0

Livewire Version

3.4.7

PHP Version

8.1

Problem description

Currently, the switching language doesn't (updatedActiveLocale) doesn't trigger lifecycle hooks (hydrate/dehydrate) of fields, which causes problems with several fields (like FileUploader)

Expected behavior

When you fill translatable FileUpload field and switch local and submit the form, you get an error.

Steps to reproduce

1- Go to a form containing a translatable file
2- Choose a file
3- switch locale
4- Save => you get an error because the file dehydration wasn't called:

Filament\Forms\Components\BaseFileUpload::Filament\Forms\Components{closure}(): Argument #1 ($file) must be of type string, array given

(There are several other errors, I m just trying to give an example.)

Reproduction repository

https://github.com/mybouhssina/demo-issue-filamentphp-translatable

Relevant log output

Suggested solution

As a fix, I updated the logic of updatedActiveLocale:

            $translatableAttributes = static::getResource()::getTranslatableAttributes();

            $this->otherLocaleData[$this->oldActiveLocale] = \Illuminate\Support\Arr::only($this->form->getState(), $translatableAttributes);

            try {
                $this->form->fill([ // fill() will trigger the hydrate hooks of the fields
                    ...Arr::except($this->form->getState(), $translatableAttributes), // getState trigger the dehydrate hooks of the fields
                    ...$this->otherLocaleData[$this->activeLocale] ?? [],
                ]);
                unset($this->otherLocaleData[$this->activeLocale]);
            }catch(ValidationException $e) {
               // switch back to the old locale since the swithc failed due to the current state of the form
                $this->activeLocale = $this->oldActiveLocale;
                throw $e;
            }

If you think I m on the right path it would be my pleasure to create my first PR.

@mybouhssina
Copy link
Author

I think this is related:
#11450

@danharrin
Copy link
Member

Duplicate of #10886

@danharrin danharrin marked this as a duplicate of #10886 Mar 14, 2024
@danharrin danharrin closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working low priority unconfirmed
Projects
Status: Done
Development

No branches or pull requests

3 participants