From 81c5dbe519e7203b7f04d081ba17c5aac0848070 Mon Sep 17 00:00:00 2001 From: francoism90 Date: Fri, 11 Oct 2024 13:48:18 +0200 Subject: [PATCH] Add form translation handling --- composer.lock | 4 ++-- .../Shared/Concerns/WithFormTranslations.php | 24 +++++++++++++++++++ .../Tags/Controllers/TagEditController.php | 5 ++++ src/App/Web/Tags/Forms/GeneralForm.php | 19 +++++++-------- .../Controllers/VideoEditController.php | 5 ++++ src/App/Web/Videos/Forms/GeneralForm.php | 16 ++++++------- 6 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 src/App/Web/Shared/Concerns/WithFormTranslations.php diff --git a/composer.lock b/composer.lock index b0f3e4769..62a454dc8 100644 --- a/composer.lock +++ b/composer.lock @@ -14981,12 +14981,12 @@ ], "aliases": [], "minimum-stability": "dev", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": true, "prefer-lowest": false, "platform": { "php": "^8.2" }, - "platform-dev": [], + "platform-dev": {}, "plugin-api-version": "2.6.0" } diff --git a/src/App/Web/Shared/Concerns/WithFormTranslations.php b/src/App/Web/Shared/Concerns/WithFormTranslations.php new file mode 100644 index 000000000..afb06433d --- /dev/null +++ b/src/App/Web/Shared/Concerns/WithFormTranslations.php @@ -0,0 +1,24 @@ +all()) + ->filter(fn (mixed $value) => is_string($value) && filled($value)) + ->each(fn (?string $value, string $key) => data_set($this, $key, str($value ?: '')->squish()->value())); + } + + protected function getModelTranslations(Model $model, ?string $locale = null): array + { + $locale ??= app()->getLocale(); + + return collect($model->getTranslations()) + ->map(fn (?array $item) => data_get($item, $locale)) + ->toArray(); + } +} diff --git a/src/App/Web/Tags/Controllers/TagEditController.php b/src/App/Web/Tags/Controllers/TagEditController.php index 59e2f9e73..376c4a4c0 100644 --- a/src/App/Web/Tags/Controllers/TagEditController.php +++ b/src/App/Web/Tags/Controllers/TagEditController.php @@ -37,6 +37,11 @@ protected function authorizeAccess(): void $this->canUpdate($this->tag); } + public function updatedForm(): void + { + $this->form->validate(); + } + public function submit(): void { $this->authorize('update', $model = $this->tag); diff --git a/src/App/Web/Tags/Forms/GeneralForm.php b/src/App/Web/Tags/Forms/GeneralForm.php index 4f6e4dbe0..634e040c4 100644 --- a/src/App/Web/Tags/Forms/GeneralForm.php +++ b/src/App/Web/Tags/Forms/GeneralForm.php @@ -2,6 +2,7 @@ namespace App\Web\Tags\Forms; +use App\Web\Shared\Concerns\WithFormTranslations; use Domain\Tags\Enums\TagType; use Domain\Tags\Models\Tag; use Foxws\WireUse\Forms\Support\Form; @@ -10,6 +11,8 @@ class GeneralForm extends Form { + use WithFormTranslations; + #[Validate] public string $name = ''; @@ -22,13 +25,6 @@ class GeneralForm extends Form #[Validate(['related' => 'nullable|array', 'related.*.id' => 'exists:tags,prefixed_id'])] public array $related = []; - protected function beforeValidate(): void - { - collect($this->all()) - ->filter(fn (mixed $value) => filled($value) && is_string($value)) - ->each(fn (mixed $value, string $key) => data_set($this, $key, str($value)->squish()->value())); - } - public function rules(): array { return [ @@ -38,11 +34,14 @@ public function rules(): array ]; } + protected function beforeValidate(): void + { + $this->setTranslations(); + } + protected function beforeFill(Tag $model): array { - $translations = collect($model->getTranslations()) - ->map(fn (?array $item) => data_get($item, app()->getLocale(), '')) - ->toArray(); + $translations = $this->getModelTranslations($model); $values = [...$model->toArray(), ...$translations]; diff --git a/src/App/Web/Videos/Controllers/VideoEditController.php b/src/App/Web/Videos/Controllers/VideoEditController.php index 55202e5f6..5d48fd9d7 100644 --- a/src/App/Web/Videos/Controllers/VideoEditController.php +++ b/src/App/Web/Videos/Controllers/VideoEditController.php @@ -34,6 +34,11 @@ protected function authorizeAccess(): void $this->canUpdate($this->video); } + public function updatedForm(): void + { + $this->form->validate(); + } + public function submit(): void { $this->authorize('update', $model = $this->video); diff --git a/src/App/Web/Videos/Forms/GeneralForm.php b/src/App/Web/Videos/Forms/GeneralForm.php index fd3e6daa0..60c4c584f 100644 --- a/src/App/Web/Videos/Forms/GeneralForm.php +++ b/src/App/Web/Videos/Forms/GeneralForm.php @@ -2,14 +2,17 @@ namespace App\Web\Videos\Forms; +use App\Web\Shared\Concerns\WithFormTranslations; use Domain\Videos\Models\Video; use Foxws\WireUse\Forms\Support\Form; use Livewire\Attributes\Validate; class GeneralForm extends Form { + use WithFormTranslations; + #[Validate('required|string|min:1|max:255')] - public string $name = ''; + public ?string $name = null; #[Validate('nullable|string|min:1|max:255')] public ?string $episode = null; @@ -30,24 +33,19 @@ class GeneralForm extends Form public ?string $released_at = null; #[Validate(['tags' => 'nullable|array', 'tags.*.id' => 'exists:tags,prefixed_id'])] - public array $tags = []; + public ?array $tags = []; protected function beforeValidate(): void { - collect($this->all()) - ->filter(fn (mixed $value) => is_string($value) && filled($value)) - ->each(fn (mixed $value, string $key) => data_set($this, $key, str($value)->squish()->value())); + $this->setTranslations(); } protected function beforeFill(Video $model): array { - $translations = collect($model->getTranslations()) - ->map(fn (?array $item) => data_get($item, app()->getLocale(), '')) - ->toArray(); + $translations = $this->getModelTranslations($model); $values = [...$model->toArray(), ...$translations]; - // Convert tags to options $values['tags'] = $model->tags->options()->toArray(); return $values;